How to pass header in hasura-session?

246 Views Asked by At

I am running a hasura project and I am using compute field in hasura. How can I pass and get the header in the query? There is already an open issue, but people are talking about a workaround as well but it didn't work out for me.

Here is header: x-hasura-preferred-lang: de_de

Here is the query

CREATE OR REPLACE FUNCTION public.interest_translation_function(interest_row interest, hasura_session json)
 RETURNS text
 LANGUAGE sql
 STABLE
AS $function$
SELECT
    CASE
        WHEN (hasura_session ->> 'x-hasura-preferred-lang' = 'en_us' AND T.en_us IS NOT NULL) THEN T.en_us
        WHEN (hasura_session ->> 'x-hasura-preferred-lang' = 'de_de' AND T.de_de IS NOT NULL) THEN T.de_de
        ELSE T.source
    END
FROM i18n T
WHERE T.source = interest_row.name;
$function$

You can see for now I am using hasura_session which we will get from jwt, but I want to pass the header without jwt. any though on this

I am expecting to get translated value without passing token when the same table has permission

1

There are 1 best solutions below

0
On

I couldn't find any solution. There seems to be an open issue for this. I end up with the below approach.

  1. You can pass language code in the Hasura mutation without creating any new action.
    You can update compute field sql like below.
CREATE OR REPLACE FUNCTION public.interest_translation_function(interest_row interest, lang_code text)
 RETURNS text
 LANGUAGE sql
 STABLE
AS $function$
SELECT
    CASE
        WHEN (lang_code = 'en_us' AND T.en_us IS NOT NULL) THEN T.en_us
        WHEN (lang_code = 'de_de' AND T.de_de IS NOT NULL) THEN T.de_de
        ELSE T.source
    END
FROM i18n T
WHERE T.source = interest_row.name;
$function$ 
  1. Query will look something like this.
query GetInterest {
  interest {
    name
    translated(args: {lang_code: "de_de"})
  }
}