'graphqljs - Query root type must be provided
How do I get around this error? For this particular schema, I do not need any queries (they're all mutations). I cannot pass null and if I pass an empty GraphQLObjectType it gives me the error:
Type Query must define one or more fields.
Solution 1:[1]
If you're using graphql-tools (maybe other SDL tools too), you can specify an empty type (such as Query) using:
type Query
Instead of
type Query {}
If you're building the schema programatically you'll have to add dummy query, much like:
new GraphQLObjectType({
name: 'Query',
fields: {
_dummy: { type: graphql.GraphQLString }
}
})
Which is the programmatic equivalent of the SDL:
type Query {
_dummy: String
}
Solution 2:[2]
If you are using Node.js express -
Your schema should have both Root query and root Mutation, although you don't have any query resolvers but still you need to define the root query.
E.g. -
type RootQuery {
hello: String!
}
schema {
query: RootQuery
mutation: RootMutation
}
Solution 3:[3]
If you're using express you can do this:
const {GraphQLObjectType, GraphQLSchema, GraphQLString} = require("graphql")
const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: () => ({
message: {
type: GraphQLString,
resolve: () => "Hello World"
}
})
})
})
Use GraphQLObjectType. And the property Query should be in lowercase as query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rogier Spieker |
| Solution 2 | David Buck |
| Solution 3 | trincot |
