Graphql-engine: support multiple per-row updates in a single mutation

Created on 23 Aug 2019  ·  18Comments  ·  Source: hasura/graphql-engine

Currently we allow updating multiple rows (through where) but all of them will get the same update. We need to add support for cases where the updates are different for each row, say you want to set name to Hello for the row with id=1 and to World when id=2. Something like this maybe?

{
  update_user_many(
    updates: [
      { where: {id: {_eq: 1}, _set: {name: "hello"}},
      { where: {id: {_eq: 2}, _set: {name: "world"}}
    ]
  ) {
    affected_rows
  }
}

Notes:

  1. What happens when there is overlap across the where conditions? What would affected_rows and returning return?
  2. Constructing the updates argument would need a bit of a boilerplate.

Maybe we can simplify the api to just use the primary key/unique constraints?

{
  update_user_many(
    updates: [
      { id: 1, _set: {name: "hello"}},
      { id: 2, _set: {name: "world"}}
    ]
  ) {
    affected_rows
  }
}

We can probably use update .. from as suggested here: https://stackoverflow.com/a/18799497

server intermediate enhancement medium

Most helpful comment

Would love this too! For now I'm using upsert although it's not highly recommended in the docs. What are the drawbacks of doing that until a multi-update feature is here?

All 18 comments

@0x777 As i see, for the simpler API , we could also use where with unique constraint key ?

I would enjoy this feature

I would also really like this feature. Currently I have to update multiple records individually and this would be so much easier and more efficient.

Would love this too! For now I'm using upsert although it's not highly recommended in the docs. What are the drawbacks of doing that until a multi-update feature is here?

Currently, i use multiple mutation in one graphql query to achieve this, as Hasura allows you to use multiple mutation inside one query, and all of them will execute in one transaction.

Is there an update on this at all? Also, I am considering going the upsert way like @marcfalk has and was wondering if there are any drawbacks as well. Would love some thoughts on this. @tirumaraiselvan

Is there an update on this at all? Also, I am considering going the upsert way like @marcfalk has and was wondering if there are any drawbacks as well. Would love some thoughts on this. @tirumaraiselvan

The pk may leaving gaps unless you use uuid

e.g.
id 1
id 9
id 100

Is there an update on this at all? Also, I am considering going the upsert way like @marcfalk has and was wondering if there are any drawbacks as well. Would love some thoughts on this. @tirumaraiselvan

The pk may leaving gaps unless you use uuid

e.g.
id 1
id 9
id 100

Anything other than this? And does this have any bad effects apart from the fact that there are gaps?

+1

For anyone struggling with this, I ended up using the upsert mutation for this due to the lack of a response and it works perfectly.

Is this going to implement? I don't see why cron/schedule job has higher priority than multiple per-row updates and multiple auth role.

@praveenweb This is an important feature. You must assign this to someone. Thanks

would love to see this too 👍

@praveenweb Any updates ?

Currently, i use multiple mutation in one graphql query to achieve this, as Hasura allows you to use multiple mutations inside one query, and all of them will execute in one transaction.

@revskill10 How to dynamically add multiple mutations to a single one each with different variable values? How to make use of the aliases dynamically

@hafiztahajamil No, you can't. You have to embed query variables inside the mutations. It's fast.

@revskill10
Can you please give an example on how to do it ?

On Tue, 6 Oct 2020 at 2:02 PM, Truong Hoang Dung notifications@github.com
wrote:

>
>

@hafiztahajamil https://github.com/hafiztahajamil No, you can't. You
have to embed query variables inside the mutations. It's fast.


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/hasura/graphql-engine/issues/2768#issuecomment-704133511,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AC2ASTJS37X7X5WQPIPV3SDSJLMKTANCNFSM4IO367RA
.

@hafiztahajamil For example, i want to update first_name of multiple users in one mutation, i would do:

mutation {
update_users_1(objects:users1_objects) { affected_rows }
update_users_2(objects:users2_objects) { affected_rows }
update_users_3(objects:users3_objects) { affected_rows }
update_users_4(objects:users4_objects) { affected_rows }
}

In above mutation, i generated update_users_X from code , as well as usersX_objects, like:

usersX_objects = [{
first_name: 'test'
}]
Was this page helpful?
0 / 5 - 0 ratings