Tiếp theo chúng ta sẽ tạo một mutation mới để xoá dữ liệu trong 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
}
$utils.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
}
}
Yêu cầu không thành công vì biểu thức điều kiện đánh giá là sai: giá trị cho version của bài đăng trong DynamoDB không khớp với expectedVersion được chỉ định trong các đối số. Giá trị hiện tại của đối tượng được trả về trong trường dữ liệu trong phần lỗi của phản hồi GraphQL.
Yêu cầu đã thành công và bài đăng đã được xoá khỏi DynamoDB