apostopher
apostopher

Reputation: 293

GraphQL resolve GraphQLObjectType

I'm using express-graphql along with the following query:

invitations {
    sent 
    received
}

The schema definition (simplified) is as follows:

const InvitationType = new GraphQLObjectType({
    name: 'InvitationType',
    description: 'Friends invitations',
    fields: {
        sent: {
            type: new GraphQLList(GraphQLString),
            description: 'Invitations sent to friends.',
            resolve() {
             return ['sentA'];
            }
        },
        received: {
            type: new GraphQLList(GraphQLString),
            description: 'Invitations received from friends.',
            resolve() {
             return ['receivedA', 'receivedB'];
            }
        }
    }
});

// Root schema
const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
      name: 'RootQueryType',
      fields: {
          invitations: {
              type: InvitationType // no resolve() method here.
          }
      }
    })
});

However the resolve methods are never called for sent and received fields. The query above returns:

{data: {invitations: {sent: null, received: null}}}

Is there any way to resolve the nested fields (sent and received) without defining a resolve() method on parent (invitations) field?

Upvotes: 2

Views: 2118

Answers (1)

apostopher
apostopher

Reputation: 293

This worked for me! According to GraphQL Documentation, The execution will continue if the resolve method returns a non-scalar. So the following code works:

// Root schema
const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
      name: 'RootQueryType',
      fields: {
        invitations: {
          type: InvitationType,
          resolve: () => ({}) // Resolve returns an object.
        }
      }
    })
});

Hope this helps. Cheers!

Upvotes: 1

Related Questions