You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET中如何在GraphQL查询中设置变量?报错Expecting Name found $

问题解决:GraphQL变量无法生效(Expecting Name found $)

你的GraphQL查询存在语法结构错误,导致$pageSize变量无法被正确解析,报错Expecting Name found $的核心原因是:

  • 外层多了一对冗余的大括号,破坏了GraphQL的语法结构
  • 变量定义的位置不符合规范,被错误嵌套在额外的大括号中

修正后的代码

var query = new GraphQLRequest
{
    Query = @"
query StudentQuery($pageSize : Int) {
    studentQuery(skip:20, pageSize: $pageSize ){
        totalRecords
        pageInfo{
            hasNextPage
        }
        students{
            studentCursor
            studentCode
            firstName
            surname
            studentPlacements(placementStatuses:[""Offered"", ""Published"", ""Incomplete"", ""Cancelled"", ""Accepted"", ""Completed"", ""Withdrawn Education Provider"", ""Withdrawn Student""]){
                placementId
                startDate
                endDate
                publishedPersonnelEmail
                placementStatus
                placementAgencies{
                    agencyCode
                    agencyName
                    agencyLevel
                }
            }
        }
    }
}",
    Variables = new { pageSize = 10 },
};

关键调整点

  1. 移除最外层多余的{},让查询从命名语句query StudentQuery($pageSize : Int)开始
  2. 确保变量定义($pageSize : Int)直接紧跟在命名查询之后,符合GraphQL语法规范
  3. 保留原有的查询字段、筛选条件和变量绑定逻辑,仅修正结构问题

内容的提问来源于stack exchange,提问作者user1086377

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 12:25:17