Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schema is not configured for mutations

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 ?

like image 799
Abdennour TOUMI Avatar asked Mar 19 '17 03:03

Abdennour TOUMI


People also ask

What is schema in GraphQL?

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.

What is mutations in API?

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.


1 Answers

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
      }
    })
  })
})
like image 123
Abdennour TOUMI Avatar answered Oct 14 '22 06:10

Abdennour TOUMI