Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
/ hasura-pg-actions Public archive

Short script to enable hasura to call postgresql functions (read/write) as mutations

Notifications You must be signed in to change notification settings

mnesarco/hasura-pg-actions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hasura-pg-actions

Short script to enable hasura to call postgresql functions (read/write) as mutations.

Please read the reference article: https://mnesarco.github.io/blog/2020/02/25/hasura-postgresql-function-as-mutation

Map mutations:

mutation CallPgAction {
  insert_hpga_action_journal(
    objects: {
        action_id: "test", 
        request: { greeting: "Hello " }
    }) {
    returning {
      response
    }
  }
}

To PostgreSQL functions:

CREATE OR REPLACE FUNCTION action_test(id bigint, org text, usr text, request jsonb)
RETURNS jsonb AS $func$
DECLARE 
    result jsonb;
BEGIN

    -- You can do write operations
    CREATE TABLE my_dummy_table ( id integer not null primary key, greeting text );
    INSERT INTO my_dummy_table (id,greeting)
        SELECT x.id, request->>'greeting' || ' ' || x.id
        FROM (SELECT * FROM generate_series(1,10) as id) x;

    -- You can return any jsonb object
    SELECT jsonb_agg(t) INTO result 
    FROM my_dummy_table t;

    -- Clean
    DROP TABLE my_dummy_table;

    RETURN result;

END;
$func$
LANGUAGE plpgsql VOLATILE;

A picture is worth a thousand words

ref

About

Short script to enable hasura to call postgresql functions (read/write) as mutations

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published