'set session variable for Hasura on Query in apollo client, currently getting error "missing session variable: "x-hasura-a"

I'm currently writing a next.js app. I have got a query in apollo graphql, that I want to attach a variable to, in the header of the request. The variable is called x-hasura-a .

Trying to use it like:

const variable ='Test'

  const get_query = gql'...'

  const {
    loading,
    error,
    data,
  } = useQuery(
    get_query,
    {
      variables: { asdf: 'asdf' },
    },
    {
      options: {
        context: {
          headers: {
            'x-hasura-a': variable,
          },
        },
      },
    },
  )

Does not work. Any suggestions?



Solution 1:[1]

According to the docs, context is a property of the options config object.

You already have variables defined in there. Just add context.

Like so:

const variable ='Test'

  const get_query = gql'...'

  const {
    loading,
    error,
    data,
  } = useQuery(
    get_query,
    {
      variables: { asdf: 'asdf' },
      context: {
        headers: {
          'x-hasura-a': variable,
        },
      },
    }
  )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abraham Labkovsky