1

I have an issue with trying to update a mongoose schema using graphql and .findByIdAndUpdate.

whenever I try to update a document, the the new data is updated correctly, however the unmodified one becomes null.

So if I run this in graphiql:

mutation {
  updateProfile(id: "5b05b4cfd53486377061eab3", name: "dave") {
    id
    name
    age
    interests
  }
}

It returns with this object:

{
  "data": {
    "updateProfile": {
      "id": "5b05b4cfd53486377061eab3",
      "name": "dave",
      "age": null,
      "interests": null
    }
  }
}

The new data has been updated to the document, but the old data has been been deleted.

The idea is to be able to update just a single part of the document without the rest turning into null.

The GraphQL code responsible for updating:

updateProfile: {
            type: profileType,
            args: {
                id:        { type: new GraphQLNonNull(GraphQLID) },
                name:      { type: GraphQLString },
                age:       { type: GraphQLString },
                interests: { type: GraphQLString },
            },
            resolve(parent, args) {
                return Profile.findByIdAndUpdate(args.id, {
                        name:      args.name,
                        age:       args.age,
                        interests: args.interests
                }, {new: true}, (err, Profile) => {
                    if (err) {
                        return res.status(500).send(err);
                    }
                    return;
                }); 
            }
        }

And here's the schema:

const profileSchema = mongoose.Schema({
    name:      String,
    age:       String,
    interests: String,
});

Any and all help is appreciated, thanks in advance!

FiliBoii
  • 23
  • 5
  • check your `Profile`... `console.log(Profile)` – Ashh May 26 '18 at 07:41
  • Mongoose methods only return a promise if you omit the callback. See Common Scenario #6 in [this answer](https://stackoverflow.com/questions/56319137/why-does-a-graphql-query-return-null/56319138#56319138) for more details. – Daniel Rearden Jun 01 '19 at 16:10

1 Answers1

0

This can be solved like below example.

Previously you are setting or updating the fields by the values of args params individually.

You need to pass the args params directly in "findByIdAndUpdate(args.id,args)" like the below code

It will update only the fields from the request.

In mutations:

   updateProfile: {
        type: profileType,
        args: {
            id:        { type: new GraphQLNonNull(GraphQLID) },
            name:      { type: GraphQLString },
            age:       { type: GraphQLString },
            interests: { type: GraphQLString },
        },
        resolve(parent, args) {
            return Profile.findByIdAndUpdate(args.id,args, (err, Profile)=> {
                 if(err) {
                     return res.status(500).send(err);
                 }
                 return
            }) 
        }
    }

I hope this will be useful for others, Thank you.

Venkatesh Somu
  • 4,710
  • 4
  • 23
  • 22