Schema is not configured for mutations

2019-06-26 08:23发布

问题:

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 ?

回答1:

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
      }
    })
  })
})