首页 > 解决方案 > 我只能在 Appync 上使用 GraphQL 中的主键进行查询?

问题描述

这个问题让我大吃一惊。

我正在尝试在 graphql 查询中使用非主键查询 RDS 表。

这是我在 appsync 中的两个查询的示例

query getloc{
  getLocationByCounty(County: "County"){
    LocationsID
    State
    County
  }
}
query getLocations{
  getLocation(LocationsID: 1){
    LocationsID
    State
    County
  }
}

此处的第二个查询有效,并准确返回具有该 LocationID 的县。第一个返回错误消息:“不能为不可为空的类型返回 null:父 'Locations' (/getLocationByCounty/LocationsID) 内的 'Int'”

如果我更改位置的架构并使 LocationsID 不可为空,则错误消失,但它返回 null。

{
  "data": {
    "getLocationByCounty": {
      "State": null
    }
  }
}

我的 getLocationsByCounty 请求解析器是:

"version": "2018-05-29",
"statements": ["select * from Locations where County = '$ctx.args.County'"]

这是响应解析器:

## Raise a GraphQL field error in case of a datasource invocation error
#if($ctx.error)
    $utils.error($ctx.error.message, $ctx.error.type)
#end

$utils.toJson($utils.rds.toJsonObject($ctx.result)[0])

这在graphQL中是不允许的还是我做错了???

值得注意的是,如果我只是做这个查询:

select * from Locations where County = 'County'

在 mySQL 中,它完全按预期工作。

标签: graphqlaws-amplifyaws-appsync

解决方案


推荐阅读