hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.08k stars 2.76k forks source link

document usage of current_setting('hasura.user') for mutations #4738

Open tirumaraiselvan opened 4 years ago

tirumaraiselvan commented 4 years ago

It is possible to customise behaviour of mutations using postgres triggers. Also see: https://github.com/hasura/graphql-engine/issues/2940 for a use-case and auditing tables which is built using this mechanism.

In a trigger function, you might want to access hasura session variables as well. This is possible by accessing current_setting function:

 DECLARE
   session_variables json;
 BEGIN
   session_variables := current_setting('hasura.user', 't');
   ...
 END;

current_setting('hasura.user') will return a json where each key is a session variable.

riturajborpujari commented 3 years ago

Be sure to cast the value from current_setting to type json like

session_variables := curent_setting('hasura.user', 't')::json

I forgot this and it drove me nuts.