postgresqlgraphqlhasura

Hasura native SQL Query with parameters


I am trying to execute a native SQL query. It has one integer query parameter, number_of_days.

In SQL I have included it in WHERE clause

d.time >= CURRENT_DATE - interval '%({{number_of_days}})s days' 

I am using GraphQL to perform a query:

query MyQuery {
  PR_Timelinedaily_site_locations(args: {number_of_days: 3}) {
    max_val
    min_val
    forecast_val
    date
  }
}

When I try to run this query, I get the following error:

{
  "errors": [
    {
      "message": "invalid input syntax for type interval: \"%(($2)::integer)s days\"",
      "extensions": {
        "path": "$",
        "code": "data-exception"
      }
    }
  ]
}

I tried with character varying type for number_of_days and tried re-arranging the ways it is used on SQL. But I am not able to get it right.

What am I missing?


Solution

  • Hasura seems to be using parameterized queries, so you cannot insert a variable inside a string like that. Instead, you can multiply the value with interval '1 day':

    d.time >= CURRENT_DATE - (interval '1 day' * {{number_of_days}})