I have the following schema :
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt,
GraphQLString
} from 'graphql';
let counter = 100;
const schema = new GraphQLSchema({
// Browse: http://localhost:3000/graphql?query={counter,message}
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
counter: {
type: GraphQLInt,
resolve: () => counter
},
message: {
type: GraphQLString,
resolve: () => 'Salem'
}
})
}),
mutiation: new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
incrementCounter: {
type: GraphQLInt,
resolve: () => ++counter
}
})
})
})
export default schema;
The following query works fine:
{counter, message}
However, mutation {incrementCounter}
throws the following errors :
{
"data": null,
"errors": [
{
"message": "Schema is not configured for mutations",
"locations": [
{
"line": 1,
"column": 1
}
]
}
]
}
Known that the server is :
import GraphQLHTTP from 'express-graphql';
const app = express();
app.use('/graphql',GraphQLHTTP({schema}));
What's the missing thing that makes mutation configured ?
Your GraphQL server uses a schema to describe the shape of your available data. This schema defines a hierarchy of types with fields that are populated from your back-end data stores. The schema also specifies exactly which queries and mutations are available for clients to execute.
Mutations allow you to modify server-side data, and it also returns an object based on the operation performed. It can be used to insert, update, or delete data.
I got my error, It is a typo : Instead of write mutation
inside Schema constructor, i wrote mutiation
.
const schema = new GraphQLSchema({
// Browse: http://localhost:3000/graphql?query={counter,message}
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
counter: {
type: GraphQLInt,
resolve: () => counter
},
message: {
type: GraphQLString,
resolve: () => 'Salem'
}
})
}),
mutation: new GraphQLObjectType({ //⚠️ NOT mutiation
name: 'Mutation',
fields: () => ({
incrementCounter: {
type: GraphQLInt,
resolve: () => ++counter
}
})
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With