我目前正在玩一堆新的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()。
除了为消息创建自己的类型之外,还有什么想法解决这个问题吗?
答案 0 :(得分:7)
您可以按照定义messageType
的方式定义自己的自定义theadType
,然后执行new GraphQLList(messageType)
来指定邮件列表的类型。
答案 1 :(得分:1)
我认为你不能在GraphQL中做到这一点。认为它有点反对GraphQL哲学要求领域"你需要"在每个组件中反对要求所有"他们所有"。
当应用扩展时,您的方法将提供更高的数据负载。我知道,为了测试库的目的看起来有点太多了,但似乎这就是它的设计方式。当前GraphQL库(0.2.6)中允许的类型是: