Friday, May 28, 2021

updating data using mutation

 put and patch requests difference:

put overwrites the data of the complete record.

patch will updates only the fields provided.


axios.patch(`http://localhost:3000/users/${userId}`,{"value to update"})


so add editUser mutation to the mutation fields,for reference, check delete mutation created earlier post.

editUser: {
      type: UserType,
      args: {
        id: {type: new GraphQLNonNull(GraphQLString)},
        firstName:  {type: GraphQLString},
        age: {type: GraphQLInt},
        companyId:{type: GraphQLString}
      },
      resolve(parentValue,args){
        return axios.patch(`http://localhost:3000/users/${id}`,args)
        .then(res=>res.data);
      }
    }

To test the code:

//sample 1:

mutation{
  editUser(id"40",age10){
    id
    firstName
    age
  }
 }

//sample 2:
 
 mutation{
  editUser(id"40",firstName"Samantha2"){
    id
    firstName
    age
  }
 }
 

No comments:

Post a Comment