Intro to DynamoDB Resolvers for AppSync Implementation

2023-02-20

#cloud#backend
Intro to DynamoDB Resolvers for AppSync Implementation

This is a series of articles about setting up a complex Serverless backend infrastructure with AWS SAM and CloudFormation.

Here is the index of all the articles in case you want to jump to any of them:

1. Setup of AppSync and API Gateway with Multiple AWS Cognito User Pools

2. Configuring S3 Buckets with Permissions and Access Roles in AWS Cognito AuthRole

  1. Intro to DynamoDB Resolvers for AppSync Implementation

4. Intro to Lambda Resolvers for AppSync Implementation

5. Configuring an AWS VPC to Include Lambda Resolvers with a Fixed IP

6. Intro to Pipeline Resolvers for AppSync Implementation

7. Handling Lambda Resolver Timeouts with SNS Messages


AppSync lets you wire GraphQL operations directly to DynamoDB — no Lambda in between. A resolver maps a GraphQL mutation to a PutItem. A query maps to a GetItem. The mapping template is your entire backend for that operation.

For straightforward CRUD, this is the fastest path from schema to working API.

CRUD with DynamoDB Resolvers

Create — PutItem

Request

{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  },
  "attributeValues": {
    "data": $util.dynamodb.toDynamoDBJson($ctx.args.data)
  }
}

Response

$util.toJson($ctx.result)

Read — GetItem

Request

{
  "version": "2017-02-28",
  "operation": "GetItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  }
}

Response

$util.toJson($ctx.result)

Update — UpdateItem

Request

{
  "version": "2017-02-28",
  "operation": "UpdateItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  },
  "update": {
    "expression": "set data = :data",
    "expressionValues": {
      ":data": $util.dynamodb.toDynamoDBJson($ctx.args.data)
    }
  }
}

Response

$util.toJson($ctx.result)

Delete — DeleteItem

Request

{
  "version": "2017-02-28",
  "operation": "DeleteItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  }
}

Response

$util.toJson($ctx.result)

Four operations, four mapping templates, zero compute functions. The moment your CRUD needs business logic, you'll outgrow this — but until then, there's no simpler way to stand up a GraphQL API on AWS.

Code examples are simplified to illustrate the approach. Some adjustments may be needed for production.


Next UP: Part 4. Intro to Lambda Resolvers for AppSync Implementation