Summary
We're excited to announce a new resolver for AppSync GraphQL APIs in the Event Handler utility. It simplifies routing and processing of events in AWS Lambda functions by allowing you to define resolvers for specific GraphQL types and fields.
We’ve also fixed two bugs in Logger: 1/ one causing timestamps to be incorrectly formatted when using non-UTC timezone and around midnight, and 2/ another causing temporary keys to not be properly cleared when using the injectLambdaContext()
Middy.js middleware.
Finally, starting from this release we're publishing our Lambda layers in the AWS China Beijing Region operated by Sinnet.
🌟 A huge thank you to @arnabrahman for the amazing work on the AppSync GraphQL resolver 🎉
Working AppSync GraphQL APIs
Key Features
- Route events based on GraphQL type and field keys
- Automatically parse API arguments to function parameters
- Handle GraphQL responses and errors in the expected format
To get started install the Event Handler utility by running:
npm install @aws-lambda-powertools/event-handler
Registering a resolver
You can register functions to match GraphQL types and fields with one of three methods:
onQuery()
- Register a function to handle a GraphQL Query type.onMutation()
- Register a function to handle a GraphQL Mutation type.resolver()
- Register a function to handle a GraphQL type and field.
Your resolvers receive the parsed arguments from the GraphQL request as their first parameter. We will take care of parsing the response or catching errors and returning them in the expected format.
You can register a resolver for a Query
type, you can use the onQuery()
method. This method allows you to define a function that will be invoked when a GraphQL Query is made.
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
import { Logger } from '@aws-lambda-powertools/logger';
import type { Context } from 'aws-lambda';
const logger = new Logger({
serviceName: 'TodoManager',
});
const app = new AppSyncGraphQLResolver({ logger });
app.onQuery<{ id: string }>('getTodo', async ({ id }) => {
logger.debug('Resolving todo', { id });
// Simulate fetching a todo from a database or external service
return {
id,
title: 'Todo Title',
completed: false,
};
});
export const handler = async (event: unknown, context: Context) =>
app.resolve(event, context);
Use the onMutation()
method to process GraphQL mutations:.
import {
AppSyncGraphQLResolver,
makeId,
} from '@aws-lambda-powertools/event-handler/appsync-graphql';
import { Logger } from '@aws-lambda-powertools/logger';
import type { Context } from 'aws-lambda';
const logger = new Logger({
serviceName: 'TodoManager',
});
const app = new AppSyncGraphQLResolver({ logger });
app.onMutation<{ title: string }>('createTodo', async ({ title }) => {
logger.debug('Creating todo', { title });
const todoId = makeId();
// Simulate creating a todo in a database or external service
return {
id: todoId,
title,
completed: false,
};
});
export const handler = async (event: unknown, context: Context) =>
app.resolve(event, context);
When you want to have more control over the type and field, you can use the resolver()
method. This method allows you to register a function for a specific GraphQL type and field including custom types.
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
import { Logger } from '@aws-lambda-powertools/logger';
import type { Context } from 'aws-lambda';
const logger = new Logger({
serviceName: 'TodoManager',
});
const app = new AppSyncGraphQLResolver({ logger });
app.resolver(
async () => {
logger.debug('Resolving todos');
// Simulate fetching a todo from a database or external service
return [
{
id: 'todo-id',
title: 'Todo Title',
completed: false,
},
{
id: 'todo-id-2',
title: 'Todo Title 2',
completed: true,
},
];
},
{
fieldName: 'listTodos',
typeName: 'Query',
}
);
export const handler = async (event: unknown, context: Context) =>
app.resolve(event, context);
The resolver includes helper functions for working with AppSync scalar values (basic data types like String, Int, Boolean). These helpers simplify data type conversion and validation when processing GraphQL requests. To learn more check out the documentation page.
Lambda Layers in AWS China (Beijing) Region
Powertools for AWS Lambda (TypeScript) layers are now available in the AWS China Beijing Region operated by Sinnet.
Region | Location | ARN |
---|---|---|
cn-north-1 |
Beijing | arn:aws-aws-cn:lambda:cn-north-1:498634801083:layer:AWSLambdaPowertoolsTypeScriptV2:30 |
You can use the AWS CLI to inspect the contents of the layer and read its metadata:
aws lambda get-layer-version-by-arn --arn arn:aws-aws-cn:lambda:cn-north-1:498634801083:layer:AWSLambdaPowertoolsTypeScriptV2:30 --region cn-north-1
Changes
- chore: fix typo in partitioned layers workflow (#4126) by @dreamorosi
- fix(logger): reset keys on error in middleware (#4122) by @dreamorosi
- feat(event-handler): expose event & context as object (#4113) by @dreamorosi
- fix(logger): set
hourCycle
to h23 when tz is not UTC (#4102) by @dreamorosi - feat(event-handler): add single resolver functionality for AppSync GraphQL API (#3999) by @arnabrahman
📜 Documentation updates
- chore(deps): bump @types/node from 24.0.8 to 24.0.10 (#4119) by @dependabot[bot]
- chore(deps): bump the aws-cdk group across 1 directory with 3 updates (#4118) by @dependabot[bot]
- docs(event-handler): add AppSync GraphQL docs page (#4120) by @dreamorosi
- chore(deps): bump @types/node from 24.0.7 to 24.0.8 (#4107) by @dependabot[bot]
- chore(deps): bump the aws-sdk-v3 group across 1 directory with 9 updates (#4106) by @dependabot[bot]
- chore(deps): bump mkdocs-material from 9.6.14 to 9.6.15 in /docs (#4104) by @dependabot[bot]
- chore(deps): bump squidfunk/mkdocs-material from
eb04b60
to0bfdba4
in /docs (#4105) by @dependabot[bot] - chore(deps): bump the aws-cdk group across 1 directory with 2 updates (#4108) by @dependabot[bot]
- docs: clarify version mismatch risks (#4103) by @dreamorosi
- chore(deps): bump @types/node from 24.0.4 to 24.0.7 (#4099) by @dependabot[bot]
- chore(deps): bump the aws-sdk-v3 group across 1 directory with 9 updates (#4098) by @dependabot[bot]
- docs: update layers version + simplify script (#4096) by @leandrodamascena
- chore(deps): bump the aws-cdk group across 1 directory with 2 updates (#4087) by @dependabot[bot]
- docs(kafka): provide example payloads (#4094) by @dreamorosi
- chore(deps): bump @types/node from 24.0.3 to 24.0.4 (#4088) by @dependabot[bot]
- chore(deps): bump the aws-sdk-v3 group across 1 directory with 9 updates (#4085) by @dependabot[bot]
- fix(ci): Partition workflows (#4084) by @sthulb
- docs(batch): clarify ordering/async processing (#4081) by @dreamorosi
- chore(deps): bump the aws-cdk group across 1 directory with 3 updates (#4078) by @dependabot[bot]
🔧 Maintenance
- chore(deps): bump @types/node from 24.0.8 to 24.0.10 (#4119) by @dependabot[bot]
- chore(deps): bump the aws-cdk group across 1 directory with 3 updates (#4118) by @dependabot[bot]
- chore(deps-dev): bump @aws-sdk/client-cloudwatch from 3.840.0 to 3.841.0 in the aws-sdk-v3 group across 1 directory (#4117) by @dependabot[bot]
- docs(event-handler): add AppSync GraphQL docs page (#4120) by @dreamorosi
- feat(event-handler): support
onQuery
andonMutation
handlers (#4111) by @dreamorosi - chore(deps): bump @types/node from 24.0.7 to 24.0.8 (#4107) by @dependabot[bot]
- chore(deps): bump the aws-sdk-v3 group across 1 directory with 9 updates (#4106) by @dependabot[bot]
- chore(deps): bump mkdocs-material from 9.6.14 to 9.6.15 in /docs (#4104) by @dependabot[bot]
- chore(deps): bump squidfunk/mkdocs-material from
eb04b60
to0bfdba4
in /docs (#4105) by @dependabot[bot] - chore(deps): bump the aws-cdk group across 1 directory with 2 updates (#4108) by @dependabot[bot]
- chore(deps): bump @types/node from 24.0.4 to 24.0.7 (#4099) by @dependabot[bot]
- chore(deps-dev): bump typedoc from 0.28.5 to 0.28.6 in the typescript group across 1 directory (#4090) by @dependabot[bot]
- chore(deps): bump the aws-sdk-v3 group across 1 directory with 9 updates (#4098) by @dependabot[bot]
- chore(deps): bump github/codeql-action from 3.29.0 to 3.29.2 (#4097) by @dependabot[bot]
- chore(deps): bump github/codeql-action from 3.29.0 to 3.29.1 (#4089) by @dependabot[bot]
- chore(deps): bump the aws-cdk group across 1 directory with 2 updates (#4087) by @dependabot[bot]
- docs(kafka): provide example payloads (#4094) by @dreamorosi
- chore(deps): bump @types/node from 24.0.3 to 24.0.4 (#4088) by @dependabot[bot]
- chore(deps): bump the aws-sdk-v3 group across 1 directory with 9 updates (#4085) by @dependabot[bot]
- chore(deps-dev): bump @valkey/valkey-glide from 1.3.4 to 2.0.1 (#4074) by @dependabot[bot]
- chore(deps): bump @aws-sdk/client-lambda from 3.830.0 to 3.833.0 in the aws-sdk-v3 group across 1 directory (#4073) by @dependabot[bot]
- chore(deps): bump the aws-cdk group across 1 directory with 3 updates (#4078) by @dependabot[bot]
- chore(deps-dev): bump the vitest group across 1 directory with 2 updates (#4060) by @dependabot[bot]