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.
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.
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
}
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
}
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
.
© 2022 - 2024 — McMap. All rights reserved.