Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update dynamically of multiple rows in one table using Graphql

Tags:

graphql

hasura

I am new to graphql hasura and i'm having this issue how to update multiple rows in one table, because usually i update only one row per table.

i want to update multiple rows in a dynamic way

I have for example existing rows data.

id: 1, name: 'rosie', age: 12
id: 2, name: 'jane', age: 20
id: 3, name: 'rafaela', age: 25

and I want to edit the 2 rows only.

id: 1, name: 'rosie update', age: 12
id: 2, name: 'jane update', age: 21

i have an array of users. it can be 1 or more than 1 users that will be updated.

const users = [
{id: 1, name: 'rosie update', age: 12}.
{id: 2, name: 'jane update', age: 21}
];

mutation update_article {
  update_user(
    where: {id: {_eq: users[0].id}},
    _set: {
      name: users[0].name,
      age: users[0].age
   }
  ) {
    affected_rows
  }
}

expected output should be:

want to have mutation code in dynamic way not static.

id: 1, name: 'rosie update', age: 12
id: 2, name: 'jane update', age: 21
id: 3, name: 'rafaela', age: 25
like image 868
rj.learn Avatar asked Sep 06 '19 10:09

rj.learn


Video Answer


1 Answers

Reading the documentation mutation, you could use insert instead of update that can handle multiple objects managing the conflict behaviour.

In your example the mutation could be done with:

mutation update_article {
  insert_user(objects: [
         {id: 1, name: "rosie update", age: 12},
         {id: 2, name: "jane update", age: 21}
      ],
      on_conflict: {
        constraint: user_id_key,
        update_columns: [name, age]
      }
  ) {
    affected_rows
  }
}

As id values already exist, it will update fields name and age.

like image 89
mpromonet Avatar answered Sep 19 '22 01:09

mpromonet