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

如何解决GraphQL参数传递报Syntax error: unexpected $的错误?

问题原因

你遇到的语法错误是GraphQL变量定义规则不允许的写法导致的:GraphQL操作的变量定义块中,等号后只能写静态字面量作为默认值,不允许再使用$引用其他变量。你在$start : Int = $start、$limit : Int = $limit两行里,把变量的默认值设置为同名变量本身,解析器识别到第二个$时不符合语法规范,就抛出了对应错误。

解决方案

根据你的使用场景二选一修改即可:

  • 如果你不需要给这两个变量设置默认值,直接删除等号及后面的内容,变量定义修改为:
$locale: String! = "$locale"
$searchKey: String! = "$searchKey"
$contentType: String! = "$contentType"
$start : Int
$limit : Int
) {
  contents(
    locale: $locale
    start: $start
    limit:  $limit
    sort: "priority:asc"
    where: {
      _or: [
        { title_contains: $searchKey }
        { keywords_contains: $searchKey } 
      ]
      ct_lookup: { code_contains: $contentType }
    }
  ) {
    id
    title
  }
}
  • 如果你需要给这两个变量设置默认值,直接填写静态数字字面量即可,比如设置默认查询从第0条开始、单次返回10条,对应写法如下:
$start : Int = 0
$limit : Int = 10

修改完成后,你已经传递的start=4、limit=2参数会正常生效,不需要调整参数传递逻辑。

注:你当前定义的$searchKey、$contentType为非空字符串类型,传递空字符串属于合法值,不会触发非空校验错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 11:57:03