question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

accesing hasura session from custom function fails

See original GitHub issue

I tried to follow the steps to create a custom function and access the hasura session variables from here and there are 2 issues

1- if you track the function from the console, when you execute the query it complains about not having the arguments variable

2- in order to get the session variables, I tracked the function through the API and then I get this error

{
  "errors": [
    {
      "extensions": {
        "internal": {
          "statement": "WITH \"public_get_session_role__result\" AS (SELECT  *  FROM \"public\".\"get_session_role\"()       ) SELECT  coalesce(json_agg(\"root\" ), '[]' ) AS \"root\" FROM  (SELECT  row_to_json((SELECT  \"_1_e\"  FROM  (SELECT  \"_0_root.base\".\"result\" AS \"result\"       ) AS \"_1_e\"      ) ) AS \"root\" FROM  (SELECT  *  FROM \"public_get_session_role__result\" WHERE ('true')     ) AS \"_0_root.base\"      ) AS \"_2_root\"      ",
          "prepared": true,
          "error": {
            "exec_status": "FatalError",
            "hint": "No function matches the given name and argument types. You might need to add explicit type casts.",
            "message": "function public.get_session_role() does not exist",
            "status_code": "42883",
            "description": null
          },
          "arguments": [
            "(Oid 114,Just (\"{\\\"x-hasura-role\\\":\\\"admin\\\",\\\"x-hasura-user-id\\\":\\\"15169bf8-294a-49cb-9d48-13a39a8c016e\\\"}\",Binary))"
          ]
        },
        "path": "$",
        "code": "unexpected"
      },
      "message": "postgres query error"
    }
  ]
}

I’m using hasura v1.0.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
gunarcommented, Dec 24, 2019

I was able to reproduce here too.

@tafelito fyi looking at the PR I realized a quick fix for the meantime. Just add a second argument to your function and to your queries.

CREATE OR REPLACE FUNCTION public.me(
  hasura_session json
+  , dummyarg text
) RETURNS SETOF "user" LANGUAGE sql STABLE AS $ function $
SELECT * FROM "user" WHERE   id = (hasura_session ->> 'x-hasura-user-id') :: uuid $ function $
{
  me(args: {dummyarg: ""}) {
    id
  }
}

@rakeshkky, getting information about the user currently logged in seems common enough that you might want to add this specific use-case to Hasura’s documentation.

@rakeshkky, also, would love to have the v2 of the track_function function available through the console/GUI—although this is probably already on your radar.

Thanks a bunch.

0reactions
greatwitenorthcommented, Feb 7, 2020

Yes the function was tracked via console. Here’s my schema and function definition I’m currently using:

CREATE
OR REPLACE FUNCTION public.players_nearby_me(
  hasura_session json,
  distance_kms integer DEFAULT 50
) RETURNS SETOF users LANGUAGE sql STABLE AS $ function $
SELECT
  *
FROM
  users
WHERE
  id NOT IN ((hasura_session ->> 'x-hasura-user-id') :: int)
  AND ST_DistanceSphere (
    users."location",
    (
      SELECT
        u."location"
      FROM
        users as u
      WHERE
        id = (hasura_session ->> 'x-hasura-user-id') :: int
    )
  ) < distance_kms * 1000;$ function $

users table:

-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS users_id_seq;

-- Table Definition
CREATE TABLE "public"."users" (
    "id" int8 NOT NULL DEFAULT nextval('users_id_seq'::regclass),
    "name" varchar(255) NOT NULL,
    "email" varchar(255) NOT NULL,
    "email_verified_at" timestamp(0),
    "password" varchar(255) NOT NULL,
    "remember_token" varchar(100),
    "created_at" timestamp(0),
    "updated_at" timestamp(0),
    "location" geometry,
    PRIMARY KEY ("id")
);

Let me know if you need anything else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - accesing hasura session from custom function fails -
I tried to follow the steps to create a custom function and access the hasura session variables from here and there are 2...
Read more >
Handling GraphQL Errors with Hasura & React
A guide to common GraphQL API & Hasura specific errors and how to handle them with React pages.
Read more >
Postgres: Extend schema with SQL functions - Hasura
Accessing Hasura session variables in custom functions​. Create a function with an argument for session variables and track it with the pg_track_function ...
Read more >
Action handlers | Hasura GraphQL Docs
Actions need to be backed by custom business logic. ... Hasura makes a POST request to the handler with the action arguments and...
Read more >
Functions: Accessing Session Variables | Data Models & Auth
Create a function with an argument for session variables and track it using your Hasura API. This example will look at creating a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found