Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mnesarco/hasura-pg-actions
Short script to enable hasura to call postgresql functions (read/write) as mutations
https://github.com/mnesarco/hasura-pg-actions
hasura
Last synced: 26 days ago
JSON representation
Short script to enable hasura to call postgresql functions (read/write) as mutations
- Host: GitHub
- URL: https://github.com/mnesarco/hasura-pg-actions
- Owner: mnesarco
- Created: 2020-02-27T15:02:40.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-27T18:46:52.000Z (almost 5 years ago)
- Last Synced: 2024-09-04T00:04:45.821Z (3 months ago)
- Topics: hasura
- Language: PLpgSQL
- Homepage:
- Size: 3.91 KB
- Stars: 16
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```graphql
mutation CallPgAction {
insert_hpga_action_journal(
objects: {
action_id: "test",
request: { greeting: "Hello " }
}) {
returning {
response
}
}
}
```## To PostgreSQL functions:
```sql
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](https://mnesarco.github.io/images/hasura-action-pg.png)