Logo

dev-resources.site

for different kinds of informations.

Guide to Side effects in Hasura

Published at
3/14/2023
Categories
hasura
postgres
grahpql
mutations
Author
omaiboroda
Categories
4 categories in total
hasura
open
postgres
open
grahpql
open
mutations
open
Author
10 person written this
omaiboroda
open
Guide to Side effects in Hasura

When dealing with Hasura, it can quickly come to the side-effects of doing something less trivial than using only predefined upserts/inserts.

Most often, this comes to operations with databases, calling external API, or doing hefty validations.

Hasura offers multiple ways of achieving it, though these methods have their own advantages and use cases, here are them:

Postgres triggers

Hasura heavily relies on the database functionality, and in the case of Postgres, there is a possibility to achieve desired functionality with Postgres functions and triggers.

CREATE OR REPLACE FUNCTION insert_initial_album() RETURNS TRIGGER LANGUAGE PLPGSQL AS $function$
    BEGIN
      INSERT into album (artist_id, title) VALUES (new.id, 'first album');
      RETURN new;
    END;
    $function$;


CREATE TRIGGER artist_after_insert AFTER
  INSERT ON artist
  FOR EACH ROW EXECUTE PROCEDURE insert_initial_album();
Enter fullscreen mode Exit fullscreen mode

It is also possible to write validations on a Database-level

Pros: powerful flexibility, database-native functionality.
Cons: calling a 3rd-party API is impossible. Maintainance and visibility of Functions and Triggers can be a hassle.

When to use: you are good with SQL, a believer in "you don't need an ORM".
When to avoid: at large projects where observability of SQL can be an issue. SQL is not your strength.

Multiple Mutations in a Request

Given there's a relationship established in Hasura, there's a possibility to modify nested objects, for example:

mutation {
    insert_album(objects: {title: "First album", artist: {data: {name: "Bono"}}}) {
        returning {
            id
            artist {
              id
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This way, Hasura would create Album and Artist, and assign artist_id to the newly created Artist

Pros: out-of-the-box functionality, single transaction.
Cons: lacking flexibility, if required to do more business logic on top.

When to use: fits your simple use-case.
When to avoid: rarely, if it does solve the problem - use it!

Event Triggers

Another option is an Event trigger - webhook reaction on a database change

Creating an Event Trigger

Creating an Event Trigger

Pros: out-of-the-box functionality, ability to subscribe for particular CRUD events.
Cons: it's not a transaction.

When to use: effects, that doesn't require integrity, for instance: analytics update, sending emails, notifications.
When to avoid: no atomicity - operations are executed separately.

Hasura Actions

Actions is a mechanism for customizations, where custom queries or mutations can be implemented in webhook:

Creating new Hasura Action

Creating new Hasura Action

This action would instruct Hasura to invoke handler:

Example of webhook handler

Example of webhook handler

Pros: programming language runtime, flexibility, ability to do 3rd-party calls, and access to Database.
Cons: cumbersome to set up handlers, types for every action needed.

When to use: you have to do a few manual operations, and you have a server in place (ie next) that can handle the requests.
When to avoid: you need lots of side effects (then add Remote schema) or when it's possible to use the abovementioned solutions.

Remote schema

Remote schema is a variation on Actions, but you bring your own additional Graphql Schema. Adding it to Hasura, will extend existing Schema with Queries/Mutations where you have full control.

Creating new Remote Schema

Creating new Remote Schema

Pros: easy integration with Hasura.
Cons: you'd need to maintain your own GraphQL Server.

When to use: you already have a GQL server, or the application demands lots of non-trivial code.
When to avoid: your app is small and doesn't require a standalone GQL server.

Summary

These were 5 ways of achieving similar results in Hasura:

  • Postgres triggers
  • Multiple Mutations in a Request
  • Hasura Actions
  • Event Triggers
  • Remote schema

Each of them has its strengths and weaknesses, and you can use them successfully in combination.
Hope this was helpful to pick the ones that suit you the best.

hasura Article's
30 articles in total
Favicon
Convert insert mutation to upsert
Favicon
refinedev - hasura (nested/multiple query_root)
Favicon
From Idea to Launch: My 30-Day MVP Journey
Favicon
How to Build a GraphQL API for MongoDB Using Hasura in Six Steps
Favicon
How to Build a Supergraph using Snowflake, Neon PostgreSQL, and Hasura in Five Steps
Favicon
Streamlining CI/CD Pipelines with Hasura GraphQL Engine
Favicon
Supabase over Hasura for 2024?
Favicon
Hasura and Keycloak integration with NestJS server
Favicon
Modern API Development (Part 1)
Favicon
Build a graphQL API with Hasura low-code platform
Favicon
Startup Starter Kit
Favicon
Hasura x MEWS
Favicon
Hasura vs Apollo: Comparing GraphQL Platforms
Favicon
Hasura and Nhost vs Supabase
Favicon
How to monitor URQL performance and link with database queries ?
Favicon
Build a Powerful GraphQL API with Postgres in Under 10 Minutes
Favicon
Guide to Side effects in Hasura
Favicon
Hasura: Building Scalable and Real-Time Applications - An Extensive Guide
Favicon
Hasura Cloud: Building Scalable and Secure GraphQL APIs Made Easy
Favicon
How to avoid messing up squash migration in Hasura
Favicon
Appwrite vs. Hasura vs. Apollo
Favicon
Auth0, Hasura, Social Media Login
Favicon
Deno & Hasura for app development
Favicon
SSR: clerk with hasura
Favicon
Hasura Custom Authentication Using JWT
Favicon
Unable to HASURA_GRAPHQL_JWT_SECRET in docker-compose file
Favicon
Hasura Storage in Go: 5x performance increase and 40% less RAM
Favicon
Using Authorizer with Hasura
Favicon
Hasura + Supertokens
Favicon
Creating a todo web app in < 112 lines of code with Hasura and Python

Featured ones: