Wednesday, 21 August 2019

Making update request using graphql

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

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
  }
}



from Making update request using graphql

No comments:

Post a Comment