如何在GraphQLSchema中定义多个查询或变异

时间:2017-03-06 20:15:44

标签: graphql

我是GraphQL的新手。如果这很明显,请原谅我。

除了使用buildSchema之外,有没有办法使用new GraphQLSchema定义多个查询/变异?

这就是我现在所拥有的。

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;
                    }
                }
            }
        })
    });

1 个答案:

答案 0 :(得分:8)

多个"查询"实际上只是一个Query类型的多个字段。所以只需向GraphQLObjectType添加更多字段,如下所示:

query: new graphql.GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        count: {
            type: graphql.GraphQLInt,
            resolve: function () {
                return count;
            }
        },
        myNewField: {
            type: graphql.String,
            resolve: function () {
                return 'Hello world!';
            }
        }
    }
}),
相关问题