【问题标题】:How do you define multiple query or mutation in GraphQLSchema如何在 GraphQLSchema 中定义多个查询或突变
【发布时间】:2017-07-26 20:39:52
【问题描述】:

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

【问题讨论】:

    标签: graphql


    【解决方案1】:

    多个“查询”实际上只是一种查询类型上的多个字段。因此,只需向 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!';
                }
            }
        }
    }),
    

    【讨论】:

    • 感谢上帝!
    猜你喜欢
    • 2019-08-09
    • 2018-03-24
    • 2018-09-21
    • 2020-07-30
    • 2019-11-11
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多