[英]Error while returning data from Graphql endpoint
我从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(事件)时。它实际上给了我正确需要的所有值,但在运行命令时在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
}
}
只有在创建事件后我才会看到上面的错误,但那个东西实际上在幕后工作!!
events.push(event)
console.log(events)
return events
您正在返回数组,但是在您的GraphQL typedef中
type rootmutation {
createevent(eventinput:Eventinput):Event
}
表示您打算发送单个“事件”对象。 对于数组,它应该是createevent(eventinput:Eventinput):[Event]
。 不确定它是否会完全修复你的错误,但这是问题的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.