简体   繁体   中英

more than one rootQuery object in GraphQL

Is there a way I can refactor the rootQuery object by creating more than one rootQuery objects and combine them together in the schema?

so for now we have like one rootQuery full of multiple roots and then we make a schema saying we have this one root Query. What we write is

var schema = new GraphQLSchema({
 query: RootQuery
})

What I want would be like

var schema = new GraphQLSchema({
 query: [RootQuery1, RootQuery2]
})

is there a way I can do this in GraphQL? it will help refactor.

No it can't, GraphQLSchema queryType only accepts a GraphQLObjectType which is RootQuery , it can't be of ArrayType with multiple GraphQLObjectType

All of the your Types should be declared in the main queryType which is RootQuery

Below is the Source code of GraphQLSchema

https://github.com/graphql/graphql-js/blob/master/src/type/schema.js#L58

export class GraphQLSchema {
  _queryType: GraphQLObjectType; // accepts GraphQLObjectType not array
  _mutationType: ?GraphQLObjectType;
  _subscriptionType: ?GraphQLObjectType;
  _directives: Array<GraphQLDirective>;
  _typeMap: TypeMap;
  _implementations: { [interfaceName: string]: Array<GraphQLObjectType> };
  _possibleTypeMap: ?{
    [abstractName: string]: { [possibleName: string]: boolean }
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM