Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a delete mutation in GraphQL?

StackOverflow!

One day ago I've started to learn a new thing for me - GraphQL. Now I know how to add and read objects from a database using GraphQL. I have tried to find an answer to question "how to remove an object from database". But it's no luck. Can you help me with this "hard" (for you) question?

Thanks for your answers!

like image 821
oles Avatar asked Oct 19 '25 09:10

oles


1 Answers

I found it! Maybe someone needs the answer.

Use Model.findByIdAndDelete(id, options, callback);

My code:

deleteImage: {
  type: ImageType,
  args: {
    id: { type: GraphQLID }
  },
  resolve(parent, args) {
    return Image.findByIdAndDelete(args.id);
  }
}

It works!

like image 126
oles Avatar answered Oct 22 '25 05:10

oles