Intro to DynamoDB Resolvers for AppSync Implementation

2023-02-20

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

3. 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

Introduction

AWS AppSync allows developers to set up scalable GraphQL APIs easily. One of its powerful features is the ability to integrate with DynamoDB, AWS's NoSQL database service, directly. In this guide, we'll explore how to use DynamoDB resolvers in AppSync for CRUD operations.

Understanding Resolvers

Resolvers in AppSync determine how data is fetched or modified in your data sources. When using DynamoDB as a data source, AppSync provides direct resolver mappings.

The Code

CRUD Operations with DynamoDB Resolvers

1. Create: Use the PutItem operation in DynamoDB. The resolver maps the GraphQL mutation to a PutItem request.

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)

2. Read: Utilize the GetItem operation. The GraphQL query is mapped to a GetItem request, fetching data based on a primary key.

Request

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

Response

$util.toJson($ctx.result)

3. Update: Implement the UpdateItem operation. The GraphQL mutation maps to an UpdateItem request, modifying data based on provided attributes.

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)

4. Delete: Use the DeleteItem operation. The GraphQL mutation maps to a DeleteItem request, removing the item with the specified primary key.

Request

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

Response

$util.toJson($ctx.result)

Conclusion

DynamoDB resolvers in AppSync simplify the process of setting up a GraphQL API. By directly mapping GraphQL operations to DynamoDB requests, developers can efficiently create, read, update, and delete data.

Please note that the code examples have been simplified to help understand the approach and the resources. Some adjustments might be needed.

---------

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