我目前正在玩一堆Facebook的新技术 .
我对GraphQL架构有一点问题 . 我有一个对象模型:
{
id: '1',
participants: ['A', 'B'],
messages: [
{
content: 'Hi there',
sender: 'A'
},
{
content: 'Hey! How are you doing?',
sender: 'B'
},
{
content: 'Pretty good and you?',
sender: 'A'
},
];
}
现在我想为此创建一个GraphQL模型 . 我这样做了:
var theadType = new GraphQLObjectType({
name: 'Thread',
description: 'A Thread',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLString),
description: 'id of the thread'
},
participants: {
type: new GraphQLList(GraphQLString),
description: 'Participants of thread'
},
messages: {
type: new GraphQLList(),
description: 'Messages in thread'
}
})
});
我知道首先有更优雅的方法来构建数据 . 但为了试验,我想尝试这样做 .
除了我的消息数组之外,一切正常,因为我没有指定数组类型 . 我必须指定哪种数据进入该数组 . 但由于它是一个自定义对象,我不知道将什么传递给GraphQLList() .
除了为消息创建自己的类型之外,还知道如何解决这个问题吗?
2 回答
您可以按照定义
theadType
的方式定义自己的自定义messageType
,然后执行new GraphQLList(messageType)
指定消息列表的类型 .我不认为你可以在GraphQL中做到这一点 . 认为这有点违反GraphQL的理念,即在每个组件中要求“你需要”字段而不是要求“全部” .
当应用扩展时,您的方法将提供更高的数据负载 . 我知道,为了测试库的目的看起来有点太多了,但似乎这是它的设计方式 . 当前GraphQL库(0.2.6)中允许的类型是:
GraphQLSchema
GraphQLScalarType
GraphQLObjectType
GraphQLInterfaceType
GraphQLUnionType
GraphQLEnumType
GraphQLInputObjectType
GraphQLList
GraphQLNonNull
GraphQLInt
GraphQLFloat
GraphQLString
GraphQLBoolean
GraphQLID