我正在尝试使用 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)
我做错了什么?
我认为这可能是库本身的一个错误,如果您可以将其报告为问题并通过简单的方法重现它,那就太好了!
问题是你在使用之前没有声明变量。您的代码应如下所示:
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
}
})