本文介绍了架构未配置突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下架构:
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 ?
推荐答案
我得到了我的错误,这是一个错字:我没有在 Schema 构造函数中写 mutation
,而是写了 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
}
})
})
})
这篇关于架构未配置突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!