C#GraphQLRequest:变量$max由使用,但未声明

C#GraphQLRequest:变量$max由使用,但未声明,c#,.net-core,graphql,C#,.net Core,Graphql,我正在尝试使用发送GraphQL请求。 当我尝试发送GraphQLRequest时,我得到以下错误,变量$max由使用,但未声明 GraphQLRequest graphqlRequest = new GraphQLRequest { Query = "query {boards(limit: $max) {items(limit: $max) {id}}}", Variables = new { max = "1"

我正在尝试使用发送GraphQL请求。

当我尝试发送
GraphQLRequest
时,我得到以下错误,
变量$max由使用,但未声明

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);
System.AggregateException:'发生了一个或多个错误。(分析值时遇到意外字符:{.Path'data',第1行,位置9。)”


我尝试将
$max:Int
更改为
$max:String
,并将
max=1
更改为
max=“1”

但请求未成功。

为Int类型的变量max提供了无效值
或变量$max和参数限制(字符串/Int)上的
类型不匹配
,具体取决于组合。

更新
将响应类型从
string
更改为正确的对象响应解决了问题

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

GraphQLResponse GraphQLResponse=wait_graphqlClient.SendQueryAsync(graphqlRequest);

由于某种原因,在使用
变量时,它不喜欢字符串响应类型。

感谢gunr2171的帮助,并为我指出了正确的方向

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

query($max: Int) {
  boards(limit: $max) {
    items(limit: $max) {
      id
    }
  }
}
为可读性添加了新行字符。您可以将其折叠为一行,以显示实际代码

这里的区别是
查询之后的
($max:Int)
。这让graphql引擎知道将使用一些变量及其类型。我假设您的示例中的变量是整数

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

此外,在提交变量时,请确保将数据保留为整数

Variables = new
{
   max = 1 // don't quote this
}
您有三个地方需要正确排列类型

  • GraphQLRequest的
    变量
    属性
  • query()中变量的声明
  • 您正在使用的参数的预期类型

  • 仔细检查
    boards()
    items()
    limit
    参数是否为
    Int
    ,而不是因为某种原因而为另一种数据类型(如字符串)。

    更新的问题:)更新的答案,您将变量作为字符串发送
    Variables = new
    {
       max = 1 // don't quote this
    }