如何编写用于RDS数据源的AWS AppSync响应映射模板

huangapple go评论106阅读模式
英文:

How to write a AWS AppSync response mapping template for an RDS data source

问题

Here's the translation of the code section you provided:

{
    "version": "2018-05-29",
    "statements": [
        "SELECT * FROM MyTable WHERE category='$ctx.args.category'",
        "SELECT COUNT(*) FROM MyTable WHERE category='$ctx.args.category'"
    ]
}

And the response mapping:

$utils.toJson($utils.rds.toJsonObject($ctx.result)[0])    ## For first item results
$utils.toJson($utils.rds.toJsonObject($ctx.result)[0][0]) ## For first item of the first query
$utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0]) ## For first item of the second query
$utils.toJson($utils.rds.toJsonObject($ctx.result))       ## For first & second item results

Your predicted response types are:

type MyResponse {
    MyResponseItemList: [MyResponseItem]
    Count: Int
}

type MyResponseItem {
  Id: ID!
  Name: String
  ...
}

I hope this helps with your AWS AppSync setup.

英文:

I have been following this guide for querying an Aurora Serverless database through an AppSync schema. Now I want to run a couple of queries at the same time with a request mapping like:

{
    "version": "2018-05-29",
        "statements": [
            "SELECT * FROM MyTable WHERE category='$ctx.args.category'",
            "SELECT COUNT(*) FROM MyTable WHERE category='$ctx.args.category'",
    ]
}

So, how to handle multiple selects in the response mapping? The page has a few examples, but none has two selects:

$utils.toJson($utils.rds.toJsonObject($ctx.result)[0])    ## For first item results
$utils.toJson($utils.rds.toJsonObject($ctx.result)[0][0]) ## For first item of first query
$utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0]) ## For first item of second query
$utils.toJson($utils.rds.toJsonObject($ctx.result)??????) ## ?? For first & second item results

I predicted the response type to be like follows, but is not strict as long as I can get the values.

type MyResponse {
    MyResponseItemList [MyResponseItem]
    Count Int
}

type MyResponseItem {
  Id: ID!
  Name: String
  ...
}

答案1

得分: 3

AppSync无法使用两个SELECT语句。建议将两个SQL查询拆分为两个不同的GraphQL查询操作,或将这两个SQL查询合并为一个。

英文:

Doing two selects will not work with AppSync.

I suggest you either break apart the two SQL queries into two different GraphQL query operations or combine the two SQL queries into one.

答案2

得分: 3

I faced the same issue and got this working as follows.

Instead of having Count as a direct Int type result, I converted that into another type called PaginationResult.

type MyResponse {
    MyResponseItemList [MyResponseItem]
    Count PaginationResult
}

type PaginationResult {
   Count Int
}

type MyResponseItem {
...
}

Response Velocity Template

#set($resMap = {
    "MyResponseItemList": $utils.rds.toJsonObject($ctx.result)[0],
    "Count": $utils.rds.toJsonObject($ctx.result)[1][0]
})
$util.toJson($resMap)
英文:

I faced the same issue and got this working as follows.

Instead of having Count as a direct Int type result, I converted that into a another type called PaginationResult.

type MyResponse {
    MyResponseItemList [MyResponseItem]
    Count PaginationResult
}

type PaginationResult {
   Count Int
}

type MyResponseItem {
...
}

Response Velocity Template

#set($resMap = {
    "MyResponseItemList": $utils.rds.toJsonObject($ctx.result)[0],
    "Count": $utils.rds.toJsonObject($ctx.result)[1][0]
})
$util.toJson($resMap)

答案3

得分: 0

FWIW,我刚刚成功地使用两个SELECT语句在UNION ALL Appsync/RDS请求解析器查询中工作:

{
   "version": "2018-05-29",
   "statements": ["SELECT patientIDa, patientIDb, distance FROM Distances WHERE patientIDa='$ctx.args.patientID' UNION ALL SELECT patientIDb, patientIDa, distance FROM Distances WHERE patientIDb='$ctx.args.patientID'"]
}

不确定这是否对问题有所帮助。

***注意:在我的情况下(可能是因为我在Windows上),整个["SELECT...]语句需要在一行上(没有换行符/回车),否则GraphQL会报“非转义字符...”错误(使用GraphiQL进行测试)。

英文:

FWIW, I just got working a UNION ALL Appsync/RDS Request resolver query with two SELECTs:

  {  
    "version": "2018-05-29",   
    "statements": ["SELECT patientIDa, patientIDb, distance FROM Distances WHERE patientIDa='$ctx.args.patientID' UNION ALL SELECT patientIDb, patientIDa, distance FROM Distances WHERE patientIDb='$ctx.args.patientID'"]  
}

Not sure if this will help the OP but it may.

***Note: in my case (maybe because I'm on windows) the ENTIRE ["SELECT...] statement needs to be on one line (no cr/lf) or else graphql errors with "non-escaped character..." (testing using GraphiQL)

huangapple
  • 本文由 发表于 2020年1月3日 22:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定