node.js - 使用 graphql 发出更新请求

标签 node.js express graphql express-graphql

我正在尝试使用 graphql 制作 CRUD 应用程序。但我不确定更新请求。我尝试过,但没有用。这里看我的代码。 创建帖子和查询帖子工作正常。

我正在使用express和express-graphql。 我尝试查看文档但是。我无法弄清楚。

schema

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
        }       
    `),

resolver

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 making mutation

mutation {
  updatePost(_id: "5d5a3f380930813c647cb697", postInput: {title: "update title", description: "update", content: "update content", date: "2019-08-19T06:18:06.778Z"}) {
    title
  }
}

mutation result

{
  "data": {
    "updatePost": null
  }
}

最佳答案

错误的论点:

我已经习惯了 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:

我还建议使用解析器方法的等待/异步版本,以获得响应而不是 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: ...

关于node.js - 使用 graphql 发出更新请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551958/

相关文章:

javascript - 如何路由到 sails.js 中的函数

node.js - NodeJS 和 AWS SQS

node.js - NodeJS,返回 res.json 是一种不好的做法吗

ruby-on-rails - 如何设置枚举以允许查询中使用多个枚举? GraphQL ruby

android-studio - Android Studio 中的 AWS Amplify GraphQL 未知指令

javascript - Puppeteer:获取 innerHTML

javascript - Sails JS - 添加文档并在响应中返回整个集合

node.js - 仅将我的消息发送到订阅的服务器,而不是我的其他服务器

javascript - 用express更新了文档

javascript - 将 graphql 查询与订阅相结合