Now that AWS AppSync is aware of your DynamoDB table, we can query or mutate it by defining Resolvers. In this step, we will create the addPost resolver of type Mutation, allowing us to create posts in the AppSyncTutorial-Post table.


{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id" : $util.dynamodb.toDynamoDBJson($context.arguments.id)
},
"attributeValues" : {
"author" : $util.dynamodb.toDynamoDBJson($context.arguments.author),
"title" : $util.dynamodb.toDynamoDBJson($context.arguments.title),
"content" : $util.dynamodb.toDynamoDBJson($context.arguments.content),
"url" : $util.dynamodb.toDynamoDBJson($context.arguments.url),
"ups" : { "N" : 1 },
"downs" : { "N" : 0 },
"version" : { "N" : 1 }
}
}

A Type is specified on all attribute keys and values. For example, you set the author attribute to { “S” : “${context.arguments.author}” }. Where “S” is the value type definition of the author attribute as a String, and the value is taken from the author argument provided by the user.
$utils.toJson($context.result)

Because the shape of the data in the AppSyncTutorial-Post table completely matches the shape of Post in GraphQL, the response mapping template simply passes the result straight from the table to GraphQL.

mutation addPost {
addPost(
id: 123
author: "AUTHORNAME"
title: "Our first post!"
content: "This is our first post."
url: "https://aws.amazon.com/appsync/"
) {
id
author
title
content
url
ups
downs
version
}
}

{
"data": {
"addPost": {
"id": "123",
"author": "AUTHORNAME",
"title": "Our first post!",
"content": "This is our first post.",
"url": "https://aws.amazon.com/appsync/",
"ups": 1,
"downs": 0,
"version": 1
}
}
}


