Delete Item

Next, we will create a new mutation to delete data in DynamoDB.

  1. Select the Schema tab on the left menu
  • Add the code below to the Mutation type to add a new mutation called deletePost
    deletePost(id: ID!, expectedVersion: Int): Post
  • Then click Save Schema
  • Next, in the Resolvers pane on the right, find the deletePost field of type Mutation, then select Attach

CreateAddPostResolver

  1. On the Attach resolver configuration page:
  • Resolver type: Select Unit resolver
  • Resolver runtime: Select Velocity Template Language (VTL)
  • Data source name: Select PostDynamoDBTable
  • Click the orange Create button.
  1. On the code editor page of the newly created Resolver:
  • Delete the default code and paste the content below into the Configure the request mapping template section:
{
    "version" : "2017-02-28",
    "operation" : "DeleteItem",
    "key": {
        "id": $util.dynamodb.toDynamoDBJson($context.arguments.id)
    }
    #if( $context.arguments.containsKey("expectedVersion") )
        ,"condition" : {
            "expression"       : "attribute_not_exists(id) OR version = :expectedVersion",
            "expressionValues" : {
                ":expectedVersion" : $util.dynamodb.toDynamoDBJson($context.arguments.expectedVersion)
            }
        }
    #end
}
  • Scroll down, delete the default code, and paste the content below into the Configure the response mapping template section:
#if($context.error)
  $util.error($context.error.message, $context.error.type)
#end
$util.toJson($context.result)
  • Then click Save in the corner of the page to save

CreateAddPostResolver

  1. Select the Queries tab on the left menu
  • Delete the default code, paste the script below into the Queries section, then click the Run button (the orange Play button)
mutation deletePost {
  deletePost(id:"123") {
    id
    author
    title
    content
    url
    ups
    downs
    version
  }
}

CreateAddPostResolver

  1. Click the Run button (the orange Play button) again, the returned result is NULL because you deleted the post in the previous step.

CreateAddPostResolver

  1. Delete the old code, paste the script below into the Queries section, then click the Run button (the orange Play button) to add a new post
mutation addPost {
  addPost(
    id:"123"
    author: "AUTHORNAME"
    title: "Our second post!"
    content: "A new post."
    url: "https://aws.amazon.com/appsync/"
  ) {
    id
    author
    title
    content
    url
    ups
    downs
    version
  }
}

CreateAddPostResolver

  1. Delete the old code, paste the script below into the Queries section, then click the Run button (the orange Play button) to delete the post with the condition expectedVersion being 9999
mutation deletePost {
  deletePost(
    id:"123"
    expectedVersion: 9999
  ) {
    id
    author
    title
    content
    url
    ups
    downs
    version
  }
}

CreateAddPostResolver

The request failed because the condition expression evaluated to false: the value for the post’s version in DynamoDB did not match the expectedVersion specified in the arguments. The current value of the object is returned in the data field in the error section of the GraphQL response.

  1. Change the value of expectedVersion to 1, then click the Run button (the orange Play button)

CreateAddPostResolver

The request succeeded and the post has been deleted from DynamoDB