【发布时间】:2019-12-29 19:39:19
【问题描述】:
我从非常基本的 Graphql 示例开始。所以我面临的错误是
{
"errors": [
{
"message": "Cannot return null for non-nullable field Event.title.",
"locations": [
{
"line": 34,
"column": 5
}
],
"path": [
"createevent",
"title"
]
}
],
"data": {
"createevent": null
}
}
我的 graphql 端点的代码是
const express = require("express");
const bodyparser = require("body-parser");
const graphqlhttp = require("express-graphql");
const { buildSchema } = require("graphql");
const app = express();
app.use(bodyparser.json());
const events = [];
app.use(
"/graphql",
graphqlhttp({
schema: buildSchema(`
type Event {
_id:ID!
title:String!
description:String!
price:Float!
date:String!
}
input Eventinput{
title:String!
description:String!
price:Float!
date:String!
}
type rootquery {
events:[Event!]!
}
type rootmutation {
createevent(eventinput:Eventinput):Event
}
schema{
query:rootquery
mutation:rootmutation
}
`),
rootValue: {
events: () => {
return events;
},
createevent: args => {
const event = {
_id: Math.random().toString(),
title: args.eventinput.title,
description: args.eventinput.description,
price: +args.eventinput.price,
date: args.eventinput.date
};
events.push(event);
console.log(events)
return events
}
},
graphiql: true
})
);
app.listen(3000);
现在,当我使用 console.log(events) 时,它实际上给了我正确需要的所有值,但是在运行命令时在 localhost:3000/graphql 上
mutation {
createevent(eventinput:{title:"Test",description:"dont",price:23.4,date:"2019-08-25T06:47:10.585Z"}){
title
description
}
}
我收到了上面所说的错误,即使我已经检查了两次我无法找到问题,但是当我尝试通过以下方式获取事件时我的代码可以工作
query{
events{
title
price
}
}
只有在创建事件后我才看到上面的错误,但那东西实际上在幕后工作!!
【问题讨论】:
标签: javascript node.js graphql