Search code examples
amazon-web-servicesamazon-rdsaws-appsyncvtl

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


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
  ...
}

Solution

  • 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.