How to write Upsert mutation queryinsert or update in AWS DynamoDB AppSync resolver
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Writing an upsert mutation query for AWS DynamoDB AppSync resolver involves understanding how to work with both the DynamoDB DocumentClient API and AWS AppSync's resolver interface. Upserts, which are a combination of insert and update operations, are commonly used to maintain idempotency in applications where you'll either insert a new item or update an existing one if it already exists. This guide will help you construct such a mutation with detailed explanations and examples.
Understanding Upsert in DynamoDB
In DynamoDB, an upsert can be achieved using a conditionally-updated PutItem operation. The PutItem call allows you to specify conditions under which an item will be updated, if certain conditions are met, or inserted if no item matches the specified conditions.
Core Concepts
- Primary Key: Your table requires a primary key (partition key and optionally, a sort key). This is critical because DynamoDB needs to uniquely identify the items for an upsert.
- UpdateExpression: A query to set attribute values, remove attributes, and perform mathematical operations.
- ConditionExpression: An expression to ensure that the update or insert happens only under specified conditions.
Working with AppSync
AWS AppSync enables seamless integration with AWS Lambda and DynamoDB through resolver mappings. Here are the steps to create an Upsert mutation:
Step-by-Step Guide
- Create GraphQL Schema: Define your
Upsertmutation in the GraphQL schema.
- Configure DynamoDB Table: Ensure your DynamoDB table has a primary key set up, e.g.,
id. - Set Up VTL Resolver: Create a VTL (Velocity Template Language) resolver to translate the GraphQL mutation into a DynamoDB operation.
- Request Mapping Template (Upsert Logic)
- Response Mapping Template
Adding Conditional Logic
To perform an upsert, conditional logic can be built into the request template. If custom constraints need to be checked before allowing an update, you can enhance the request mapping template.
- Ensure New or Modify Condition
Testing the Upsert Mutation
After deploying your schema and setting up the resolver, test your mutation with the AppSync console or a client:
Summary Table
| Key Concept | Description | Example |
| Primary Key | Composite key necessary for conditional operations | id as partition key |
| Upsert Logic | Use PutItem with conditional checks | $util.dynamodb.toDynamoDBJson() to format data |
| VTL Resolvers | Translates GraphQL to DynamoDB queries | Define in AppSync with request and response templates |
| UpdateExpression | Specifies update parameters | SET #D = :d for updating attributes |
| ConditionExpression | Ensures condition validity before updating | attribute_not_exists(id) for new items |
Additional Tips
- Proper Error Handling: Include error handling in your resolvers to gracefully manage unexpected states or failures. Utilize
$util.error. - Local Development: Use the AWS SAM or the Amplify CLI for local testing and deployments, which can foster a faster development cycle.
- Optimization: Regularly analyze your DynamoDB costs and query performance metrics to identify optimization opportunities related to table throughput and scaling.
The upsert mutation pattern is essential for scenarios where item insert or update decisions must be made dynamically. By crafting an efficient resolver using these techniques, you can ensure robust data management in your AppSync applications.

