Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update data with GraphQL

I am studying graphql.

I can retrieve data from my mongo database with queries, I can create data with mutations.

But how I can modify existing data?

I am a bit lost here...

I have to create a new mutation?

like image 325
user3765595 Avatar asked Jun 19 '16 10:06

user3765595


People also ask

Can you use GraphQL to update data?

In GraphQL, you insert, update or delete data with mutations. A Mutation is a GraphQL Operation that allows you to insert new data or modify the existing data on the server-side. You can think of GraphQL Mutations as the equivalent of POST , PUT , PATCH and DELETE requests in REST.

How do I update my schema in GraphQL?

Select the database you created in previous articles and go to the GRAPHQL section from the left menu bar. Click on the UPDATE SCHEMA button and select the file containing the updated schema. At the database layer, the update process creates any missing collections, indexes, and functions.

How do you update cache after mutation?

The steps we need to update the cache are: Read the data from Apollo cache (we will use the same GET_ITEMS query) Update the list of items pushing our new item. Write the data back to Apollo cache (also referring to the GET_ITEMS query)


1 Answers

Yes, every mutation describes a specific action that can be done to a bit of data. GraphQL is not like REST - it doesn't specify any standard CRUD-type actions.

When you are writing a mutation to update some data, you have two options. Let's explain them in the context of a todo item that has a completed status, and a text field:

  1. Write mutations that represent semantic actions - markTodoCompleted, updateTodoText, etc.
  2. Write a generic mutation that just sets any properties passed it, you could call it updateTodo.

I prefer the first approach, because it makes it more clear what the client is doing when it calls a certain mutation. In the second approach, you need to be careful to validate the values to be set to make sure someone can't set some invalid combination.

In short, you need to define your own mutations to update data.

like image 71
stubailo Avatar answered Sep 28 '22 08:09

stubailo