7

I am currently playing around with a bunch of new technology of Facebook.

I have a little problem with GraphQL schemas. 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. 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().

Any idea how to resolve this besides creating an own type for messages?

2 Answers 2

7

You can define your own custom messageType the same way you defined theadType, and then you do new GraphQLList(messageType) to specify the type of your list of messages.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, i get a problem "..must have a sub selection." when i request for sub-element. can you give an example of get query to fetch messages.
2

I don't think you can do this in 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".

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:

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

1 Comment

Why the downvote? As Peter Hilton said you have to use a GraphQLList to achieve what you wanted but you explicitly said that you didn't want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.