GraphQL: Variable is used by anonymous query but not declared

Viewed 2348

I am new to GraphQL. I have a query, but it shows error message of "Variable is used by anonymous query but not declared".

{
   "query":"{customers(first: 1, query: $input) {edges{node {addresses{ id }}}}}",
   "variables":{
      "input":{
         "id":"gid://shopify/Customer/5044061470926"
      }
   }
}

Can I get some help what I did wrong?

Thanks!

1 Answers

The error is correct. Your query is

{
  customers(first: 1, query: $input) {
    edges{
      node {
        addresses{ 
          id 
        }
      }
    }
  }
}

and $input is indeed not declared, so GraphQL has no idea what it is supposed to be or how to link it up with your variables values.

You'll need to do

query ($input: <THE_TYPE>!) {
  customers(first: 1, query: $input) {
    edges{
      node {
        addresses{ 
          id 
        }
      }
    }
  }
}

I don't know your API schema, so you'll have to replace <THE_TYPE> with whatever type is defined in your API schema.

Related