Currently, I am in the midst of an AWS AppSync project utilizing CDK (Cloud Development Kit) to define my resolvers. While working on this project, I have stumbled upon two distinct approaches for writing DynamoDB query operations within my resolver functions. However, I find myself uncertain about which one is considered the best practice. Here are the two approaches I have come across:
Approach 1: Utilizing ddb.query
import * as ddb from "@aws-appsync/utils/dynamodb";
export function request(ctx) {
return ddb.query({
// query parameters
});
}
Approach 2: Leveraging Operation Object
export function request(ctx) {
return {
operation: "Query",
// other parameters
};
}
This has led me to ponder over a few questions:
Which approach should be deemed as the best practice when using CDK for AppSync? Are there notable performance discrepancies between these approaches? How does each method affect the maintainability and readability of the codebase? Can you pinpoint specific scenarios where one approach outshines the other?
Additional information to consider:
I am incorporating TypeScript into my CDK project. The resolvers I am developing will be deployed as part of an AppSync API within AWS. My primary data source revolves around DynamoDB usage. Any insights or experiences related to these methodologies in a CDK environment would be immensely valuable!
In my experimentation with Approach 1 through the AWS Console, I encountered an error message:
This syntax requires an imported helper but module 'tslib' cannot be found.
Remarkably, despite this error, my queries still execute without any hindrance. Conversely, Approach 2 operates smoothly without any errors, delivering the desired outcomes.