架构未配置为突变 [英] Schema is not configured for mutations

查看:77
本文介绍了架构未配置为突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下架构:

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;

以下查询工作正常:

{counter, message}

但是,mutation {incrementCounter}会引发以下错误:

However, mutation {incrementCounter} throws the following errors :

{
  "data": null,
  "errors": [
    {
      "message": "Schema is not configured for mutations",
      "locations": [
        {
          "line": 1,
          "column": 1
        }
      ]
    }
  ]
}

服务器是:

import GraphQLHTTP from 'express-graphql';
const app = express();
app.use('/graphql',GraphQLHTTP({schema}));

使突变配置的缺失之处是什么?

What's the missing thing that makes mutation configured ?

推荐答案

我遇到了错误,这是一个错字:我写了mutiation,而不是在Schema构造函数中写了mutation.

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

这篇关于架构未配置为突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆