Next, we will create a new mutation to delete data in DynamoDB.
deletePost(id: ID!, expectedVersion: Int): Post

{
"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
}
#if($context.error)
$util.error($context.error.message, $context.error.type)
#end
$util.toJson($context.result)

mutation deletePost {
deletePost(id:"123") {
id
author
title
content
url
ups
downs
version
}
}


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

mutation deletePost {
deletePost(
id:"123"
expectedVersion: 9999
) {
id
author
title
content
url
ups
downs
version
}
}

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.

The request succeeded and the post has been deleted from DynamoDB