Python GraphQL 变量未定义

问题描述 投票:0回答:2

我正在尝试使用 Flask + Flask-GraphQL + Graphene 服务器上的

apollo-boost
客户端执行包含一些变量的 GraphQL 请求。

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
  }
})

但是我收到“变量 X 未定义”错误。

[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

我在

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])
# ...

但是从我得到的输出来看,一切似乎都很好。

graphql_server.run_http_query()
确实接收了所有变量。

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)

我做错了什么?

python graphql apollo-client graphene-python
2个回答
0
投票

我认为这可能是库本身的一个错误,如果您可以将其报告为问题并通过简单的方法重现它,那就太好了!


0
投票

问题是你在使用之前没有声明变量。您的代码应如下所示:

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
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.