0
votes

When attempting to delete an item using the following request mapping:

{
  "version" : "2017-02-28",
  "operation" : "DeleteItem",
  "key" : {
      "id": { "S" : "$ctx.args.id"},
      "sortKey" : { "S" : "$ctx.args.sortKey"}
  }
}

If the item exists it will process the result through the response template, however when the item does not exist the response template is never run.

Response template:

#set($ctx.result.status = "SUCCESS")
#set($ctx.result.message = "This was a success!")
$utils.toJson($ctx.result)

I am aware that when an item does not exist in Dynamo it will perform no action but I would expect that it would still process through the template.

Is there anything I am missing or is it impossible for AppSync to processed a DeleteItem request through the response mapping when the document does not exist?

1

1 Answers

2
votes

This the expected execution behavior for the version of the template you are using (2017-02-28).

You can switch your request mapping template version to 2018-05-29 and your response mapping template will be executed, with the following characteristics:

  • If the datasource invocation result is null, the response mapping template is executed.

  • If the datasource invocation yields an error, it is now up to you to handle the error. The invocation error is accessible using $ctx.error.

  • The response mapping template evaluated result will always be placed inside the GraphQL response data block. You can also raise or append an error using $util.error() and $util.appendError() respectively.

More info https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-changelog.html#aws-appsync-resolver-mapping-template-version-2018-05-29

So for your example:

{
  "version" : "2018-05-29", ## Note the new version
  "operation" : "DeleteItem",
  "key" : {
      "id": { "S" : "$ctx.args.id"},
      "sortKey" : { "S" : "$ctx.args.sortKey"}
  }
}

and response template

#if ( $ctx.error )
    $util.error($ctx.error.message, $ctx.error.type)
#end
#set($ctx.result.status = "SUCCESS")
#set($ctx.result.message = "This was a success!")
$utils.toJson($ctx.result)