graphql - 验证后失败:标题:需要路径“标题”。”,在 graphql
问题描述
我试图在 graphql-yoga 中添加突变,但每次我尝试突变时,我都会收到错误消息
验证后失败:标题:
title
需要路径。",
我不知道为什么。
这是我的代码
解析器
Mutation: {
createPost: async(root, args, ctx) => {
console.log(args)
try {
const post = new Post({
title: args.title,
description: args.description,
content: args.content
});
const result = await post.save();
console.log(result);
return result
}catch(err) {
throw err
}
}
}
图式
input postInput{
title: String!
description: String!
content: String!
}
type Mutation {
createPost(input: postInput): Post!
}
如果我删除输入类型并直接这样做,这很好用
type Mutation {
createPost(title: String!,description: String!,content: String!): Post!
}
记录结果
{ input:
[Object: null prototype] {
title: 'with input',
description: 'this is de',
content: 'this is conte' } }
为什么我会在这里[Object: null prototype]
?
解决方案
如果您在架构上提供这样的输入类型,则必须像这样在解析器中发送数据:
const post = new Post({
title: args.input.title,
description: args.input.description,
content: args.input.content
});
这意味着,在 args 中,我们需要一个名为input的参数,它的类型为Post。
在 graphql gui 上提供数据时,发送数据如下:
mutation {
createPost(input:{
title: 'with input',
description: 'this is de',
content: 'this is conte'}) {
//return your id or others
}
}
推荐阅读
- laravel - 如何更改“记住我”的 Laravel cookie 生命周期?
- python - 如何访问具有特定日期时间的行?
- javascript - Vue.js 是否可以在更新第三方 JSON 时自动更新视图?
- java - 连接池不适用于通过 UCP 的 DB2 数据库
- python - URL 在 Django 中显示错误的表单和视图
- c# - MasterDetailPage 中的 BindingContext
- jquery - Ckeditor 未验证,使用 jquery 验证,在表单向导设置中
- python - 如何在单独的进程中并行运行 Scrapy Crawler Process?(多处理)
- java - 从命令行执行包的java程序时出错
- java - Maven 无法获取可执行 jar:无法加载主类