Load Existing Data from a Hasura Database using Netlify Serverless Functions

Share this video with your friends

Send Tweet

We'll load existing data from our Hasura database using serverless functions. The first step will be to create a new mutation in the Hasura GraphiQL named InserOrUpdateBoops.

We need to make sure that all the corgis are being loaded into our database. Once they are loaded, we can set the count to zero. We do this by defining a GraphQL mutation using an autogenerated variable provided by Hasura called, boops_insert_input!.

Our application loads the existing data but the functionality to save the values from the client-side is still not available.

Antal Tony Tettinger
Antal Tony Tettinger
~ 3 years ago

Could you please help and elaborate how is this working? mutation MyMutation($likes: [likes_insert_input!]!) { likes: insert_likes(on_conflict: {constraint: likes_pkey, update_columns: id}, objects: $likes) { returning { count id } } } I understand what it is doing, but its confusing a bit. Thank you in advance!

Jason Lengstorf
Jason Lengstorf(instructor)
~ 3 years ago

sure!

Hasura supports "upserts", meaning it will create OR update something in the database, which is a nice convenience method to avoid needing a strict "create" and "update" function and needing to reason about when to use each one

to support this, we need to know what to update when a conflict (an existing entry with the same key) is found

so the constraint identifies the key that means a conflict exists, and the update_columns identifies which fields should be updated. any fields that are not included in that will be ignored

this is especially useful if you have something with partially sensitive data. imagine a User and they have permission to edit their name and email, but not their permission level — the constraint would be their user ID, and the update_columns would be name and email

let me know if that doesn't make sense!