graphqljs - Query root type must be provided
Asked Answered
C

3

5

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.
Cymar answered 23/1, 2019 at 7:27 Comment(0)
S
18

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
}
Sil answered 23/1, 2019 at 7:45 Comment(2)
kind of odd how you need to specify Query even though you don't need it. I'll mark this as answer after I give it another while.Cymar
@A.Lau It's safe to mark this as accepted. If you check the spec, it specifically spells out that "[t]he query root operation type must be provided and must be an Object type." Only the mutation and subscription root operation types are optional.Karlin
L
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
}
Lozada answered 19/4, 2020 at 11:2 Comment(0)
V
0

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.

Ventriloquism answered 23/3, 2022 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.