Các phần trước chúng ta đã tạo các đối tượng theo mô hình khoá - giá trị. Trong bước này chúng ta sẽ tạo các đối tượng phức tạp hơn với AWS AppSync DynamoDB như là sets, list và maps.
type Post {
id: ID!
author: String
title: String
content: String
url: String
ups: Int!
downs: Int!
version: Int!
tags: [String!]
}
allPostsByTag(tag: String!, count: Int, nextToken: String): PaginatedPosts!
type Mutation {
addTag(id: ID!, tag: String!): Post
removeTag(id: ID!, tag: String!): Post
.....
}


{
"version" : "2017-02-28",
"operation" : "Scan",
"filter": {
"expression": "contains (tags, :tag)",
"expressionValues": {
":tag": $util.dynamodb.toDynamoDBJson($context.arguments.tag)
}
}
#if( ${context.arguments.count} )
,"limit": $util.toJson($context.arguments.count)
#end
#if( ${context.arguments.nextToken} )
,"nextToken": $util.toJson($context.arguments.nextToken)
#end
}
#if($context.error)
$util.error($context.error.message, $context.error.type)
#end
{
"posts": $utils.toJson($context.result.items)
#if( ${context.result.nextToken} )
,"nextToken": $util.toJson($context.result.nextToken)
#end
}


{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : $util.dynamodb.toDynamoDBJson($context.arguments.id)
},
"update" : {
"expression" : "ADD tags :tags, version :plusOne",
"expressionValues" : {
":tags" : { "SS": [ $util.toJson($context.arguments.tag) ] },
":plusOne" : { "N" : 1 }
}
}
}
#if($context.error)
$util.error($context.error.message, $context.error.type)
#end
$util.toJson($context.result)


{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : $util.dynamodb.toDynamoDBJson($context.arguments.id)
},
"update" : {
"expression" : "DELETE tags :tags ADD version :plusOne",
"expressionValues" : {
":tags" : { "SS": [ $util.toJson($context.arguments.tag) ] },
":plusOne" : { "N" : 1 }
}
}
}
#if($context.error)
$util.error($context.error.message, $context.error.type)
#end
$util.toJson($context.result)

query allPostsByAuthor {
allPostsByAuthor(
author: "Nadia"
) {
posts {
id
title
}
nextToken
}
}

mutation addTag {
addTag(id:"10" tag: "dog") {
id
title
tags
}
}

Kết quả trả về đối tượng đã được thêm danh sách tags với giá trị là “dog”

Kết quả trả về đối tượng đã được thêm “puppy” vào danh sách tags
mutation removeTag {
removeTag(id:"10" tag: "puppy") {
id
title
tags
}
}

query allPostsByTag {
allPostsByTag(tag: "dog") {
posts {
id
title
tags
}
nextToken
}
}

type Comment {
author: String!
comment: String!
}
type Post {
id: ID!
author: String
title: String
content: String
url: String
ups: Int!
downs: Int!
version: Int!
tags: [String!]
comments: [Comment!]
}
addComment(id: ID!, author: String!, comment: String!): Post

{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : $util.dynamodb.toDynamoDBJson($context.arguments.id)
},
"update" : {
"expression" : "SET comments = list_append(if_not_exists(comments, :emptyList), :newComment) ADD version :plusOne",
"expressionValues" : {
":emptyList": { "L" : [] },
":newComment" : { "L" : [
{ "M": {
"author": $util.dynamodb.toDynamoDBJson($context.arguments.author),
"comment": $util.dynamodb.toDynamoDBJson($context.arguments.comment)
}
}
] },
":plusOne" : $util.dynamodb.toDynamoDBJson(1)
}
}
}
#if($context.error)
$util.error($context.error.message, $context.error.type)
#end
$util.toJson($context.result)

mutation testAddComment {
addComment(id: "10", author: "Người dùng mới", comment: "Bài viết rất hữu ích!") {
id
title
comments {
author
comment
}
}
}
