首页 > 解决方案 > 带有参数的graphQL突变给出错误:标量中不允许验证失败的变量

问题描述

我使用 Hasura 和 PostGres 数据库。这是 GraphQL 突变

mutation submitBeacon($thing_uid: Int, $x: Int, $y: Int, $z: Int){
            insert_conf_thing(objects:
              [{thing_uid: $thing_uid, my_coordinates: {type: "Point", coordinates: [$x, $y, $z]}}]) {
            returning {
              thing_uid
              my_coordinates
            }   } }

查询变量

{
  "thing_uid": 1744,
  "x": 2,
  "y": 3,
  "z": 4
}

这是查询响应

{
  "errors": [
    {
      "extensions": {
        "path": "$.selectionSet.insert_conf_thing.args.objects[0].my_coordinates",
        "code": "validation-failed"
      },
      "message": "variables are not allowed in scalars"
    }
  ]
}

Postgres DB 类型: thing_uid是 BigInt my_coordinatesGeometric 类型

如果我在查询中将变量 $x、$y 和 $z 替换为 1、2 和 3,则一切正常。

为什么当我使用参数时查询返回错误?

标签: postgresqlgraphqlgraphiqlhasura

解决方案


Hasura Discord chan的回答:

my_coordinates 列的类型是geometry,是标量类型,所以我们可以这样写查询

mutation submitBeacon($id: bigint, $geometry: geometry) {
  insert_conf_test(objects: {aBigInt: $id, aGeometry: $geometry}){
    returning{
      aBigInt
      aGeometry
    }
  }
}

使用查询变量

{
"id": 4,
"geometry": {"type": "Point", "coordinates": [1, 2, 3]}
}

推荐阅读