Search code examples
graphqlhasura

Hasura query nullable variable


I am trying to get superior id one by one from my database, but the last one will be allways nullable and i don´t know how to be able to ask in Int type varible for null value.

I tried this query which function untill i use the null value.

query GetSortHierarchy($device_id: Int!, $filter_id: Int!, $superior_id: Int) {
  devices_by_pk(id: $device_id) {
    filter_types(where: {id: {_eq: $filter_id}}) {
      sort_groups(where: {superior_id: {_eq: $superior_id}}) {
        id
        name
        rank
        sort_group {
          id
          superior_id
        }
        superior_id
      }
    }
  }
}

with those variables

{"device_id": 156, "filter_id": 1, "superior_id": null}

And it returns this message "message": "unexpected null value for type 'Int'".

I know about is_null check but i would prefer single query and not two, one for null and second for the rest.


Solution

  • Using the GraphiQL API explorer tool, hover over the where section and you should see a $ button like so:

    enter image description here

    This allows you to supply the entire where section as variables. This will allow you to supply either filter type with the same basic query.

    You can then have a $sortGroupsWhere variable (or whatever you choose to call it) that looks something like this:

    {
      variables: {
        // ...other variables,
        sortGroupsWhere: {
          "superior_id": {
            // either: "_eq": "some_id"
            // or: "is_null": true
          }
        }
      }
    }