我是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;
}
}
}
})
});
答案 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!';
}
}
}
}),