Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define multiple query or mutation in GraphQLSchema

Tags:

graphql

I am new to GraphQL. Forgive me if this is obvious.

Beside using buildSchema, is there a way to define more than one query/mutation using new GraphQLSchema?

This is what I have right now.

const schema = new graphql.GraphQLSchema(
    {
        query: new graphql.GraphQLObjectType({
            name: 'RootQueryType',
            fields: {
                count: {
                    type: graphql.GraphQLInt,
                    resolve: function () {
                        return count;
                    }
                }
            }
        }),
        mutation: new graphql.GraphQLObjectType({
            name: 'RootMutationType',
            fields: {
                updateCount: {
                    type: graphql.GraphQLInt,
                    description: 'Updates the count',
                    resolve: function () {
                        count += 1;
                        return count;
                    }
                }
            }
        })
    });
like image 289
Anthony C Avatar asked Mar 06 '17 20:03

Anthony C


1 Answers

Multiple "queries" are actually just multiple fields on one Query type. So just add more fields to that GraphQLObjectType, like so:

query: new graphql.GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        count: {
            type: graphql.GraphQLInt,
            resolve: function () {
                return count;
            }
        },
        myNewField: {
            type: graphql.String,
            resolve: function () {
                return 'Hello world!';
            }
        }
    }
}),
like image 109
stubailo Avatar answered Sep 30 '22 02:09

stubailo