pg_graphql
pg_graphql copied to clipboard
Upsert support
Even though Supabase supports upsert, the mutation is not available from GraphQL
Steps to reproduce the behavior:
- Create the GraphiQL page against your project using the code here: https://github.com/supabase/supabase/discussions/6144
- In the Documentation explorer, check any table's available mutations
- Mutation.deleteFrom[TableName]Collection, Mutation.insertInto[TableName]Collection, and Mutation.update[TableName]Collection are available, but Mutation.upsertInto[TableName]Collection is not
Expected behavior Would expect to see a Mutation.upsertInto[TableName]Collection operation that would allow a constraint name to be used, or alternatively insertInto[TableName]Collection could have onConflict support as in Hasura: https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/upsert/
Versions:
- PostgreSQL: [14.1]
- pg_graphql v0.3.2
upsert support is on the roadmap but the user facing API is still being spec'd
I'm familiar with Hasura and Graphile's upsert APIs and would like to avoid directly referencing the constraint names if possible
is there any update on this? :)
none yet, we'll be looking at user defined functions and views first
thanks @olirice ! do you have any educated guess on:
- will upsert eventually be implemented
- if so, a rough time line? Q2 vs 2024?
- can you suggest a workaround? as now I need to execute 2 queries iso 1 every time (and as a seperate query there is a small risk as its not a single transaction): (1)check if item exists, if so (2a) update, if not (2b) insert
many thanks!
sure
- yes, its definitely going to happen
- Currently I'd it'll be in by EO Q2 but that may change depending on who get pulled into various other priorities
Assuming:
- "check if an item exists" is based on some unique combination of colums
- once inserted, the record is not deleted
- the columns making up the unique constraint don't change in value
I'd suggest
- enforcing the unique constraint in postgres
- attempting the insert
- if a unique constraint violation occurs
- perform an update
thanks a lot for the update and suggestion!
@olirice , a note if Im correct on your proposal:
scenario, upsert in a single transaction
- multiple related tables (e.g. 1a upsert a post entity and 1b. upsert a reference in the blog_post table as well)
- multiple entities (e.g. upsert an (aggregate root), in the blog scenario assume a blog (Agg root) that contains several posts. Thus a 2a upsert for the blog table (title, body) has to take place as well as an 2b upsert for each of its posts (table)
Assume:
- I assume 1a&1b or 2a and 2b should be a transaction to ensure data integrity (agree?). Thus they should be combined in a single qraphql mutation request.
your suggestion if Im correct (not that I have a better idea) has issues A. when combining GQL queries 1a&1b or 2a and 2b to a single query, the insert will fail if a part of the data already exits (e.g. 2b where one of the blog posts is already in the db). The update will fail as well as one of the blog posts does not exist.. B to overcome A I need to write frontend code that executes a graphql (try insert()? done: update()) once for every entity (table row) & I need as its multiple queries now I need to write frontend rollback logic (whick might not be fails safe)
what are your thoughts?
Any update on this? I'm currently using custom defined upsert functions and calling them via supabase.rpc, but would much prefer standardizing to use gql mutations everywhere.
Note yet but nested inserts is the next large project we're going to tackle for GraphQL and this is pre-req for that so it should start moving soon
any update on when supabase will have update mutations?