我喜欢 Universal Relay 样板 - 总的来说,我可以看出他们对如何将整个东西组合在一起非常周到,这与大多数样板不同,文件夹组织等似乎是事后才想到的(我猜是因为你赢了一开始就做任何重要的事情,或者什么)......但不是 URB。或者至少我们对同样的事情很挑剔。
为什么same annoying thing...
...除了一件事:我不明白他们为什么这样做。
// 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?
这里是“Type”,由于别名,它实际上与原始版本有点不同。
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
和type
之间最明显的区别示例;其他的是相同的。我发现没有其他指南推荐甚至提到做 model.js
而是从 type.js
开始,这让我感到有点恼火。
我能理解
- 某些类型需要在安全性、性能、美学、设计等方面与模型不同
- 有些类型故意跨越模型
- 一些类型出于设计原因封装了模型
但他们到处都这样做,这让我觉得我在这里遗漏了一些重要的东西。
最佳答案
它们不是一回事。只是同一个名字:
- 一个是对象实例
- 另一个是对象的结构(用于记录 API 的类型)
关于javascript - 对于 GraphQL,声明一个与 GraphQL "class"没有任何明显差异的支持模型 "Type"有什么好处或必要性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38160489/