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

Hasura查询中Int类型变量传入null值的问题求助

解决GraphQL查询中可空Int变量传入null报错的问题

直接修改查询,通过条件分支在单个请求中同时处理superior_id为null和非null的情况:

query GetSortHierarchy($device_id: Int!, $filter_id: Int!, $superior_id: Int) {
  devices_by_pk(id: $device_id) {
    filter_types(where: {id: {_eq: $filter_id}}) {
      sort_groups(
        where: {
          _cond: {
            if: { _is_null: { superior_id: $superior_id } },
            then: { superior_id: { _is_null: true } },
            else: { superior_id: { _eq: $superior_id } }
          }
        }
      ) {
        id
        name
        rank
        sort_group {
          id
          superior_id
        }
        superior_id
      }
    }
  }
}

原理说明:

  • 原查询报错的核心原因是_eq操作符不接受null作为参数,即便变量$superior_id定义为可空的Int类型也无法兼容
  • 使用_cond条件分支实现逻辑切换:
    1. 先判断变量$superior_id是否为null
    2. 如果是null,执行then分支,匹配数据库中superior_id为空的记录
    3. 如果非null,执行else分支,匹配superior_id等于变量值的记录

这样无需拆分两次查询,单个请求即可覆盖两种场景,同时支持传入null值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 22:30:09