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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With