I’m working on an AWS AppSync project using CDK (Cloud Development Kit) to define my resolvers. I’ve come across two different approaches for writing DynamoDB query operations in my resolver functions, and I’m not sure which one is the best practice. Here are the two approaches:
Approach 1: Using ddb.query
import * as ddb from "@aws-appsync/utils/dynamodb";
export function request(ctx) {
return ddb.query({
// query parameters
});
}
Approach 2: Using Operation Object
export function request(ctx) {
return {
operation: "Query",
// other parameters
};
}
I’m wondering:
Which approach is considered best practice when using CDK for AppSync? Are there performance differences between these approaches? How does each approach impact maintainability and readability? Are there specific scenarios where one approach is clearly better than the other?
Some additional context:
I’m using TypeScript in my CDK project. My resolvers are being deployed as part of an AppSync API in AWS. I’m primarily working with DynamoDB as my data source. Any insights or experiences with these approaches in a CDK context would be greatly appreciated!
When testing Approach 1 in the AWS Console, I get an error:
This syntax requires an imported helper but module 'tslib' cannot be found.
However, my queries still run. With Approach 2, I don’t encounter any errors, and the queries work as expected.