3

I have an AWS AppSync pipeline function. The request mapping template makes a Query request and it looks like this:

{
  "version": "2018-05-29",
  "operation": "Query",
  "query": {
    "expression": "#user = :user AND #datetimeUpdated < :datetimeUpdated",
    "expressionNames": {
      "#datetimeUpdated": "datetimeUpdated",
      "#user": "user"
    },
    "expressionValues": {
      ":datetimeUpdated": $util.dynamodb.toDynamoDBJson($ctx.stash.get("result").get("datetimeUpdated")),
      ":user": $util.dynamodb.toDynamoDBJson($ctx.stash.get("user"))
    }
  },
  "filter": {
    "expression": "#id = :id",
    "expressionNames": {
      "#id": "id"
    },
    "expressionValues": {
      ":id": $util.dynamodb.toDynamoDBJson($ctx.stash.get("result").get("id"))
    }
  }
}

The first part of the response template looks like this:

#if( $ctx.error )
  $util.error($ctx.error.message, $ctx.error.type)
#end

#set( $result = $ctx.result )

#if( $util.isNull($result) )
  $util.error("No result", "EMPTY", {}, $util.toJson($ctx))
#end

#set( $items = $result.get("items") )

#if( $util.isNull($items) )
  $util.error("No items to delete", "EMPTY", {}, $util.toJson($items))
#end

Running this, it stops at the error with "No items to delete". The info field with $items JSON string is empty.

I know this should return results. So I moved the error up to inspect the $result variable, like so:

#if( $ctx.error )
  $util.error($ctx.error.message, $ctx.error.type)
#end

#set( $result = $ctx.result )

$util.error("OUTPUT RESULT", "EMPTY", {}, $util.toJson($result))

Now the error info field shows (truncated):

"errorInfo": "{\"items\":[{\"datetimeCreated\":\"2020-09-07T13:30:40.870Z\",\"id\":\"abc\",\"body\":\"C\",\"title\":\"Abc\",\"datetimeUpdated\":\"2020-09-07T13:30:40.870Z\",\"user\":\"e0a9f02c-dc6f-4c90-8ec0-951345d8c9a6\"}

So it appears there is data in $result.get("items"). Viewing the CloudWatch logs also show the response mapping template in question has the following context (truncated):

"context": {
  "result": {
      "items": [
          {
              "datetimeCreated": "2020-09-07T13:30:40.870Z",
              "id": "abc",
              "body": "C",
              "title": "Abc",
              "datetimeUpdated": "2020-09-07T13:30:40.870Z",
              "user": "e0a9f02c-dc6f-4c90-8ec0-951345d8c9a6"
          },

Why do the logs show a result, when the logic within the resolver does not seem to see it?


EDIT: also tried using a Scan and I'm getting exactly the same issue – $ctx.result.items is empty at the time of execution, but are present in the logs.

Fisu
  • 3,294
  • 9
  • 39
  • 61

0 Answers0