Graphql-engine: Expose a stored procedure as a mutation

Created on 30 Aug 2019  路  2Comments  路  Source: hasura/graphql-engine

Given this table:

CREATE聽TABLE聽accounts聽(
聽聽聽聽id聽INT聽GENERATED聽BY聽DEFAULT聽AS聽IDENTITY,
聽聽聽聽name聽VARCHAR(100)聽NOT NULL,
聽聽聽聽balance聽DEC(15,2)聽NOT NULL,
聽聽聽聽PRIMARY聽KEY(id)
);

With somethings

INSERT聽INTO聽accounts(name, balance) VALUES ('Bob', 10000);
INSERT聽INTO聽accounts(name, balance) VALUES ('Alice', 10000);

I've created a stored procedure:

CREATE聽OR聽REPLACE聽PROCEDURE聽transfer(INT,聽INT,聽DEC)
LANGUAGE聽plpgsql
AS聽$$
BEGIN
聽聽聽聽-- subtracting the amount from the sender's account
聽聽聽聽UPDATE聽accounts
聽聽聽聽SET聽balance聽=聽balance聽-聽$3
聽聽聽聽WHERE聽id聽=聽$1;

聽聽聽聽聽-- adding the amount to the receiver's account
聽聽聽聽UPDATE聽accounts
聽聽聽聽SET聽balance聽=聽balance聽+聽$3
聽聽聽聽WHERE聽id聽=聽$2;
COMMIT;
END;
$$;

I want expose this procedure as a graphql mutation.

I know is possible made with functions, but I thinks if the SQL view is exposed by Graphql query why not expose procedures as mutation?

Maybe create a pattern to turn possible create and expose a stored procedure.

server ideas

Most helpful comment

Yes please there's ton of use cases where simple CRUD is not enough

All 2 comments

Yes please there's ton of use cases where simple CRUD is not enough

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codepunkt picture codepunkt  路  3Comments

leoalves picture leoalves  路  3Comments

sachaarbonel picture sachaarbonel  路  3Comments

Fortidude picture Fortidude  路  3Comments

macalinao picture macalinao  路  3Comments