2
votes

I am trying to make crud application using graphql. But I am not sure about update request. I tried, but it's not working. Here see my code. Create post and query posts are working fine.

I am using express and express-graphql. I tried going through docs but. I am unable to figure out.

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
  }
}
2
you probably not passing values to your updatePost methog, show how its called - Medet Tleukabiluly
@MedetTleukabiluly show what ?? I am not getting. - aditya kumar
where do you call updatePost? - Medet Tleukabiluly
Usually in mutation, first argument is root item, second is incoming data, third is context. updatePost: (root, args, ctx) => { args is your data } - Medet Tleukabiluly
do you get any data on your "console.log(result)"? If so, can you share the data structure? - Marco Daniel

2 Answers

3
votes

Wrong arguments:

I'm used to Apollo, because of its simplified structure. In Apollo I've encountered a similar issue whereas the arguments of queries and mutations in the resolver consist of 4 different items.

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: ...

Bonus ECMA:

I would also recommend to use the await/async version of the resolver method in order to get a response and not a Promise. Read https://www.greycampus.com/blog/programming/java-script-versions in order to make your code simpler using the latest 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: ...
0
votes

I know that i am late an you probably fixed it but it might help someone else.

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

instead write:

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