英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论