简体   繁体   English

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

[英]How to create a list of custom objects in GraphQL

I am currently playing around with a bunch of new technology of Facebook. 我目前正在玩一堆Facebook的新技术。

I have a little problem with GraphQL schemas. 我对GraphQL架构有一点问题。 I have this model of an object: 我有一个对象模型:

{
        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'
            },
        ];
    }

Now I want to create a GraphQL model for this. 现在我想为此创建一个GraphQL模型。 I did this: 我这样做了:

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'
    }

  })
});

I know there are more elegant ways to structure the data in the first place. 我知道首先有更优雅的方法来构建数据。 But for the sake of experimenting, I wanted to try it like this. 但为了试验,我想尝试这样做。

Everything works fine, besides my messages array, since I do not specify the Array type. 除了我的消息数组之外,一切正常,因为我没有指定数组类型。 I have to specify what kind of data goes into that array. 我必须指定哪种数据进入该数组。 But since it is an custom object, I don't know what to pass into the GraphQLList(). 但由于它是一个自定义对象,我不知道将什么传递给GraphQLList()。

Any idea how to resolve this besides creating an own type for messages? 除了为消息创建自己的类型之外,还知道如何解决这个问题吗?

您可以使用与定义theadType相同的方式定义自己的自定义messageType ,然后执行new GraphQLList(messageType)以指定消息列表的类型。

I don't think you can do this in GraphQL. 我不认为你可以在GraphQL中做到这一点。 Think that it's a bit against GraphQL philosophy of asking for the fields "you need" in each component against asking for "them all". 认为这有点违反GraphQL的理念,即在每个组件中要求“你需要”字段而不是要求“全部”。

When the app scales, your approach will provoque higher loads of data. 当应用扩展时,您的方法将提供更高的数据负载。 I know that for the purpose of testing the library looks a bit too much but it seems this is how it is designed. 我知道,为了测试库的目的看起来有点太多了,但似乎这就是它的设计方式。 Types allowed in current GraphQL library (0.2.6) are: 当前GraphQL库(0.2.6)中允许的类型是:

  • GraphQLSchema GraphQLSchema
  • GraphQLScalarType GraphQLScalarType
  • GraphQLObjectType GraphQLObjectType
  • GraphQLInterfaceType GraphQLInterfaceType
  • GraphQLUnionType GraphQLUnionType
  • GraphQLEnumType GraphQLEnumType
  • GraphQLInputObjectType GraphQLInputObjectType
  • GraphQLList GraphQLList
  • GraphQLNonNull GraphQLNonNull
  • GraphQLInt GraphQLInt
  • GraphQLFloat GraphQLFloat
  • GraphQLString GraphQLString
  • GraphQLBoolean GraphQLBoolean
  • GraphQLID GraphQLID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM