Skip to content

v2.23.0

Latest
Compare
Choose a tag to compare
@github-actions github-actions released this 03 Jul 18:48
· 8 commits to refs/heads/main since this release
b8e6d93

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

Docs

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

Docs

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

📜 Documentation updates

🔧 Maintenance

This release was made possible by the following contributors:

@arnabrahman, @dreamorosi, @leandrodamascena, and @sthulb