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

▼魔方 西西 提交于 2021-02-07 14:55:24

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/57820410/how-to-update-dynamically-of-multiple-rows-in-one-table-using-graphql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!