如何在GraphQL中创建自定义对象列表

Moe*_*ler 7 javascript graphql

我目前正在玩一堆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'
            },
        ];
    }
Run Code Online (Sandbox Code Playgroud)

现在我想为此创建一个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'
    }

  })
});
Run Code Online (Sandbox Code Playgroud)

我知道首先有更优雅的方法来构建数据.但为了试验,我想尝试这样做.

一切正常,除了我的消息数组,因为我没有指定数组类型.我必须指定哪种数据进入该数组.但由于它是一个自定义对象,我不知道将什么传递给GraphQLList().

除了为消息创建自己的类型之外,还知道如何解决这个问题吗?

Pet*_*ton 7

您可以按照定义的messageType方式定义自己的自定义theadType,然后new GraphQLList(messageType)指定消息列表的类型.