首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在GraphQLSchema中定义多个查询或突变

问如何在GraphQLSchema中定义多个查询或突变
EN

Stack Overflow用户
提问于 2017-03-07 04:15:44
回答 1查看 4.6K关注 0票数 11

我是GraphQL的新手。如果这是显而易见的,请原谅我。

除了使用buildSchema之外,还有一种使用new GraphQLSchema定义多个查询/突变的方法吗

这就是我现在所拥有的。

代码语言:javascript
复制
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;
                    }
                }
            }
        })
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-07 15:00:11

多个“查询”实际上只是一种查询类型上的多个字段。因此,只需向GraphQLObjectType添加更多字段,如下所示:

代码语言:javascript
复制
query: new graphql.GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        count: {
            type: graphql.GraphQLInt,
            resolve: function () {
                return count;
            }
        },
        myNewField: {
            type: graphql.String,
            resolve: function () {
                return 'Hello world!';
            }
        }
    }
}),
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42634742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档