gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:46:18 31 4
gpt4 key购买 nike

我正在尝试使用 This GraphQL Library 发送 GraphQL 请求.
当我尝试发送 GraphQLRequest 时与 some variables attached我收到以下错误,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);
max从一个字符串到一个 int 没有变化。
如果我尝试在没有 Variables 的情况下发送硬编码的相同请求附上,请求成功。 query {boards(limit: 1) {items(limit: 1) {id}}} 更新
我在 gunr2171 的回答的帮助下更新了查询,但不幸的是 sdk 仍然抛出错误。
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.)'我尝试更改 $max: Int$max: String以及 max = 1max = "1"但请求不成功。 Variable max of type Int was provided invalid valueType mismatch on variable $max and argument limit (String / Int)取决于组合。

更新
string 更改响应类型到适当的对象响应已经解决了问题。例如
GraphQLResponse<string> graphQLResponse = await _graphqlClient.SendQueryAsync<string>(graphqlRequest);
GraphQLResponse<AuthorizationResponse> graphQLResponse = await _graphqlClient.SendQueryAsync<AuthorizationResponse>(graphqlRequest);
在使用 Variables 时出于某种原因或其他原因它不喜欢字符串响应类型。谢谢 gunr2171 的帮助,并指出我正确的方向

最佳答案

您需要先在顶级查询中定义变量,然后才能在查询正文中使用它。

query($max: Int) {
boards(limit: $max) {
items(limit: $max) {
id
}
}
}
添加了新行字符以提高可读性。您可以将其折叠为实际代码的一行。
这里的区别是 ($max: Int)之后 query .这让 graphql 引擎知道将使用一些变量以及它们的类型。我假设你的例子变量是一个整数。

此外,当您提交变量时,请确保将数据保留为整数。
Variables = new
{
max = 1 // don't quote this
}
您有三个类型需要正确排列的地方。
  • Variables GraphQLRequest 的属性
  • query()中变量的声明
  • 您正在使用的参数的预期类型。

  • 仔细检查 limit两者的论据 boards()items()Int ,而不是出于某种原因另一种数据类型(如字符串)。

    关于C# GraphQLRequest : Variable $max is used by but not declared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64299685/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com