3

I'm trying to do a GraphQL request that contains some variables using the apollo-boost client on a Flask + Flask-GraphQL + Graphene server.

let data = await client.query({
  query: gql`{
    addItemToReceipt(receiptId: $receiptId, categoryId: $categoryId, value: $value, count: $count) {
      item {
        id
        category { id }
        value
        count
      }
    }
  }`,
  variables: {
    receiptId: this.id,
    categoryId: categoryId,
    value: value,
    count: mult
  }
})

But I get "Variable X is not defined" errors.

[GraphQL error]: Message: Variable "$receiptId" is not defined., Location: [object Object],[object Object], Path: undefined
[GraphQL error]: Message: Variable "$categoryId" is not defined., Location: [object Object],[object Object], Path: undefined
[GraphQL error]: Message: Variable "$value" is not defined., Location: [object Object],[object Object], Path: undefined
[GraphQL error]: Message: Variable "$count" is not defined., Location: [object Object],[object Object], Path: undefined
[Network error]: Error: Response not successful: Received status code 400

I've added some debug prints to the graphql_server/__init__.py

# graphql_server/__init__.py:62
all_params = [get_graphql_params(entry, extra_data) for entry in data]
print(len(all_params))
print(all_params[0])
# ...

But from the output I get, everything seems to be OK. The graphql_server.run_http_query() does receive all the variables.

GraphQLParams(query='{\n  addItemToReceipt(receiptId: $receiptId, categoryId: $categoryId, value: $value, count: $count) {\n    item {\n      id\n      category {\n        id\n        __typename\n      }\n      value\n      count\n      __typename\n    }\n    __typename\n  }\n}\n', variables={'receiptId': '13', 'categoryId': 'gt', 'value': 0, 'count': 0}, operation_name=None)

What am I doing wrong?

Niklas R
  • 16,299
  • 28
  • 108
  • 203

2 Answers2

0

I think this might be a bug in the library itself, if you could report it as an issue with an easy way to reproduce it, it would be great!

Syrus Akbary Nieto
  • 1,249
  • 12
  • 20
0

The problem is you had not declared a variable before use. Your code should look like this:

let data = await client.query({
  query: gql`query addItemToReceiptQuery (
    $receiptId: Int, 
    $categoryId: Int, 
    $value: String, 
    $count: Int) 
{
    addItemToReceipt(receiptId: $receiptId, categoryId: $categoryId, value: $value, count: $count) {
      item {
        id
        category { id }
        value
        count
      }
    }
  }`,
  variables: {
    receiptId: this.id,
    categoryId: categoryId,
    value: value,
    count: mult
  }
})

Found an answer here

Yuriy Petrovskiy
  • 7,888
  • 10
  • 30
  • 34