Get Item

In this step, we will retrieve data from the DynamoDB table with a provided key.

  1. Select the Schema tab on the left menu
  • In the Resolvers pane on the right, find the getPost field of type Query, then select Attach

CreateGetResolvers

  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.

CreateGetResolvers

  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" : "GetItem",
    "key" : {
        "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id)
    }
}

CreateGetResolvers

  1. Scroll down, delete the default code, and paste the content below into the Configure the response mapping template section:
#if($context.error)
  $util.error($context.error.message, $context.error.type)
#end
$util.toJson($context.result)

CreateGetResolvers

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

CreateGetResolvers

  1. Select the Queries tab on the left menu
  • Delete the default code in the editor, paste the script below into the Queries section, then click the Run button (the orange Play button)
query getPost {
  getPost(id:"123") {
    id
    author
    title
    content
    url
    ups
    downs
    version
  }
}

CreateGetResolvers

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

CreateGetResolvers