【发布时间】:2023-04-10 08:46:02
【问题描述】:
我正在尝试使用 graphql 制作 crud 应用程序。但我不确定更新请求。我试过了,但它不起作用。在这里看我的代码。 创建帖子和查询帖子工作正常。
我正在使用 express 和 express-graphql。 我尝试通过文档但是。我想不通。
架构
const graphqlHttp = require('express-graphql');
const {buildSchema} = require('graphql');
app.use('/graphql', graphqlHttp({
schema: buildSchema(`
type Post {
_id: ID!
title: String!
description: String!
content: String!
date: String!
}
input PostInput {
title: String!
description: String!
content: String!
date: String!
}
type RootMutation {
createPost(postInput: PostInput!): Post
updatePost(_id: ID!, postInput: PostInput!): Post
}
schema{
query: RootQuery
mutation: RootMutation
}
`),
解析器
updatePost: args => {
console.log(args); <!-- this log gives nothing
Post.findByIdAndUpdate(args._id, {$set: {
title: args.postInput.title,
description: args.postInput.description,
content: args.postInput.content,
date: new Date(args.postInput.date)
}})
.then(result => {
console.log(result);
return {
...result._doc
}
}).catch (err =>{
throw err;
});
},
localhost:8080/graphql 进行突变
mutation {
updatePost(_id: "5d5a3f380930813c647cb697", postInput: {title: "update title", description: "update", content: "update content", date: "2019-08-19T06:18:06.778Z"}) {
title
}
}
变异结果
{
"data": {
"updatePost": null
}
}
【问题讨论】:
-
你可能没有将值传递给你的
updatePostmethog,显示它是如何被调用的 -
@MedetTleukabiluly 显示什么??我没有得到。
-
你在哪里打电话给
updatePost? -
通常在变异中,第一个参数是根项,第二个是传入数据,第三个是上下文。
updatePost: (root, args, ctx) => { args is your data } -
您的“console.log(result)”上是否有任何数据?如果可以,能否分享一下数据结构?
标签: node.js express graphql express-graphql