Schema is not configured for mutations

こ雲淡風輕ζ 提交于 2019-12-07 07:10:22

问题


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


来源:https://stackoverflow.com/questions/42882777/schema-is-not-configured-for-mutations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!