C# GraphQLRequest : Variable $max is used by but not declared

Viewed 540

I'm trying to send a GraphQL Request using This GraphQL Library.

When I attempt to send the GraphQLRequest with some variables attached I get the following error,
Variable $max is used by but not declared.

GraphQLRequest graphqlRequest = new GraphQLRequest
{
    Query = "query {boards(limit: $max) {items(limit: $max) {id}}}",
    Variables = new
    {
        max = "1"
    }
};

GraphQLResponse<string> graphQLResponse = await _graphqlClient.SendQueryAsync<string>(graphqlRequest);

Changing max from a string to an int had no change.

If I try and send the same request hardcoded without the Variables attached, the request is successful.
query {boards(limit: 1) {items(limit: 1) {id}}}

Update

I updated the query with the help of gunr2171's answer, however unfortunately the sdk is still throwing errors.

GraphQLRequest graphqlRequest = new GraphQLRequest
{
    Query = "query($max: Int) {boards(limit: $max) {items(limit: $max) {id}}}",
    Variables = new
    {
        max = 1
    }
};

System.AggregateException: 'One or more errors occurred. (Unexpected character encountered while parsing value: {. Path 'data', line 1, position 9.)'

I tried changing $max: Int to $max: String as well as max = 1 to max = "1"
but the request isnt sucessful.

Variable max of type Int was provided invalid value or Type mismatch on variable $max and argument limit (String / Int) depending on the combination.

Update
Changing the response type from string to the proper object response has solved the problem. eg

GraphQLResponse<string> graphQLResponse = await _graphqlClient.SendQueryAsync<string>(graphqlRequest);

to

GraphQLResponse<AuthorizationResponse> graphQLResponse = await _graphqlClient.SendQueryAsync<AuthorizationResponse>(graphqlRequest);

For some one reason or another when using Variables it didnt like the string response type.

Thank you gunr2171 for their help, and pointing me in the right direction

1 Answers

You need to define the variable in the top level query before you can use it inside the query body.

query($max: Int) {
  boards(limit: $max) {
    items(limit: $max) {
      id
    }
  }
}

New line characters added for readability. You can collapse this to one line for your actual code.

The difference here is the ($max: Int) after query. That let's the graphql engine know that some variables will be used and what their type is. I'm assuming for your example that the variable is an integer.


Also, when you submit your variables, make sure you keep the data as an integer.

Variables = new
{
   max = 1 // don't quote this
}

You've got three places where the types need to line up correctly.

  1. The Variables property of the GraphQLRequest
  2. The declaration of the variable in query()
  3. The expected type for the arguments you are using.

Double check that the limit arguments of BOTH boards() and items() is an Int, and not for some reason another data type (like String).

Related