Put Item

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.

  1. Select the Schema tab on the left menu
  • In the Resolvers pane on the right, find the addPost 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
  • Scroll down to the bottom and click the orange Create button.

CreateAddPostResolver

  1. After creation, you will be taken to the Resolver code editor page. Here:
  • Delete the default code and paste the content below into the Configure the request mapping template section:
{
    "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 }
    }
}

CreateAddPostResolver

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.

  1. Scroll down, delete the default code, and paste the content below into the Configure the response mapping template section:
$utils.toJson($context.result)

CreateAddPostResolver

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.

  1. Click Save in the corner of the page to save

CreateAddPostResolver

  1. Select the Queries tab on the left menu to call the API to add a post to the database
  • Delete the default code in the editor, paste the script below into the Queries section, then click the Run button (the orange Play button)
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
  }
}

CreateAddPostResolver

  1. The returned result is similar to the following:
{
  "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
    }
  }
}

CreateAddPostResolver

  1. Open the DynamoDB console

CreateAddPostResolver

  1. Select the Explore table items tab
  • Select the AppSyncTutorial-Post table, a post has been written.

CreateAddPostResolver CreateAddPostResolver