A. L
A. L

Reputation: 12649

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.

Upvotes: 5

Views: 31947

Answers (3)

therohantomar
therohantomar

Reputation: 469

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.

Upvotes: 0

Shivam Pandey
Shivam Pandey

Reputation: 104

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
}

Upvotes: 3

Rogier Spieker
Rogier Spieker

Reputation: 4187

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
}

Upvotes: 18

Related Questions