【问题标题】:Making update request using graphql使用 graphql 发出更新请求
【发布时间】: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) =&gt; { args is your data }
  • 您的“console.log(result)”上是否有任何数据?如果可以,能否分享一下数据结构?

标签: node.js express graphql express-graphql


【解决方案1】:

参数错误:

我习惯了 Apollo,因为它的结构很简单。在 Apollo 中,我遇到了类似的问题,而解析器中查询和突变的参数由 4 个不同的项目组成。

updatePost: (parent,args,context,info) => {   // <-- look at this line
            console.log(args);
            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;
            });
        },
ExampleMutation2: ...

额外的 ECMA:

我还建议使用解析器方法的 await/async 版本来获得响应而不是 Promise。阅读 https://www.greycampus.com/blog/programming/java-script-versions 以使用最新的 ECMAScript 使您的代码更简单

   updatePost: async (parent,args,context,info) => {
                try{
                  console.log(args);
                  let result= await Post.findByIdAndUpdate(args._id, {$set: {
                      title: args.postInput.title,
                      description: args.postInput.description,
                      content: args.postInput.content,
                      date: new Date(args.postInput.date)
                  }})
                  console.log(result);
                  return result._doc
                }catch (err =>{
                  throw err;
                });
            },
    ExampleMutation2: ...

【讨论】:

  • 除此之外,返回result.toObject() 而不是result._doc。理想情况下,所有_names 都应该是私有的。
【解决方案2】:

我知道我迟到了,您可能已修复它,但它可能对其他人有所帮助。

type RootMutation {
            updatePost(_id: ID!, postInput: PostInput!): Post

改为写:

updatePost(id: ID!, postInput: PostInput!): Post!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-30
    • 2019-04-07
    • 2020-03-03
    • 2020-03-05
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多