本文介绍了使用GraphQL,声明支持模型“类”的好处或必要性是什么?这与GraphQL“Type”不同。以任何明显的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢通用继电器底板 - 总的来说我可以说他们非常考虑他们如何把这整件放在一起,不像大多数的文件夹组织等等的材料似乎是一个预期的事后想法(我想是因为你赢了首先做一些重要的事情,或者别的什么)......但不是URB。或者至少我们对同样的事情很挑剔。

I like the Universal Relay Boilerplate - in general I can tell they were very thoughtful about how they put this whole thing together, unlike most boilerplates for which folder organization and so on seems to be an intended afterthought (I guess because you won't do anything important at first, or something)... but not URB. Or at least we are picky about the same things.

.. 。除了一件事:我不明白为什么他们这样做。

...except for one thing: I don't get why they do this.

// Class used by GraphQL Server
export default class User
{
  constructor( fields )
  {
    this.id = fields.id
    this.User_AccountName = fields.User_AccountName
    this.User_AccountPassword = fields.User_AccountPassword
    this.User_DisplayName = fields.User_DisplayName
    this.User_ProfilePhoto = fields.User_ProfilePhoto
    this.User_Email = fields.User_Email
    this.User_PhoneNumberMobile = fields.User_PhoneNumberMobile
    this.User_Locale = fields.User_Locale
    this.UserToken2 = fields.UserToken2
  }
}



... 两次近距离没有解释?



这是类型,实际上由于别名而与原始版本略有不同。

...two times in close proximity with no explanation?

Here is the "Type" which is actually a bit different than the original by virtue of an alias.

export default new GraphQLObjectType( {
  name: 'Viewer',
  interfaces: [NodeInterface],
  isTypeOf: object => object instanceof User,
  fields: {
    id: globalIdField('Viewer'),

    User_IsAnonymous:        { type: GraphQLBoolean, resolve: (obj) => obj.id.equals( Uuid_0 ) },
    User_AccountName:        { type: GraphQLString,  resolve: (obj) => obj.User_AccountName },
    User_DisplayName:        { type: GraphQLString,  resolve: (obj) => obj.User_DisplayName },
    User_ProfilePhoto:       { type: GraphQLString,  resolve: (obj) => obj.User_ProfilePhoto },
    User_Email:              { type: GraphQLString,  resolve: (obj) => obj.User_Email },
    User_PhoneNumberMobile:  { type: GraphQLString,  resolve: (obj) => obj.User_PhoneNumberMobile },
    User_Locale:             { type: GraphQLString,  resolve: (obj) => obj.User_Locale },
    UserToken2:             { type: GraphQLString,  resolve: (obj) => obj.UserToken2 },

    ..._ViewerFields,

  },
} );



他们打算明白



这是我可以在样板中找到 model 类型之间最显着的差异示例;其他人是相同的。我真的有点恼火,发现没有其他指南建议甚至提到做 model.js ,而是从 type.js开始

They intend it obviously

This is the most dramatic example of difference between model and type I could find in the boilerplate; the others are identical. I was actually a bit annoyed to find that no other guide recommends or even mentions doing a model.js but rather starts from a type.js.

我能理解是否


  • 某些类型需要与安全性,性能,美学,设计等模型不同

  • 有些类型故意跨越模型

  • 出于设计原因,某些类型封装了模型
  • li>
  • Some types need to differ from models for security, performance, aesthetic, design, etc
  • Some types span models deliberately
  • Some types encapsulate models for design reasons

但他们到处都是这样,这让我觉得我在这里缺少一些重要的东西。

But they do it everywhere which leads me to think I am missing something important here.

推荐答案

它们不是一回事。同名:

They are not the same thing. Just the same name:


  • 一个是对象实例

  • 另一个是对象的结构(用于记录API的类型)

  • One is the object instance
  • the other is the structure of the objects (types use to document the API)

这篇关于使用GraphQL,声明支持模型“类”的好处或必要性是什么?这与GraphQL“Type”不同。以任何明显的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:39