API Reference
    Preparing search index...

    Class for registering resolvers for GraphQL events in AWS AppSync GraphQL APIs.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    batchResolverRegistry: RouteHandlerRegistry

    A map of registered routes for GraphQL batch events, keyed by their fieldNames.

    exceptionHandlerRegistry: ExceptionHandlerRegistry

    A map of registered exception handlers for handling errors in GraphQL resolvers.

    isDev: boolean = false

    Whether the router is running in development mode.

    logger: Pick<GenericLogger, "debug" | "warn" | "error">

    A logger instance to be used for logging debug, warning, and error messages.

    When no logger is provided, we'll only log warnings and errors using the global console object.

    resolverRegistry: RouteHandlerRegistry

    A map of registered routes for all GraphQL events, keyed by their fieldNames.

    Methods

    • Register a batch resolver function for GraphQL events that support batching.

      Registers a handler for a specific GraphQL field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field, and can either process requests individually or aggregate them for batch processing.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Type Parameters

      • TParams extends Record<string, unknown>
      • TSource = Record<string, unknown> | null

      Parameters

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver<{id: number}>(async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }, {
      fieldName: 'getPosts'
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver(async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, {
      fieldName: 'getPost',
      aggregate: false
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver(async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, {
      fieldName: 'getPost',
      aggregate: false,
      throwOnError: true
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      You can also specify the type of the arguments using generic type parameters for non-aggregated handlers:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver()

      app.batchResolver<{ postId: string }>(async (args, { event, context }) => {
      // args is typed as { postId: string }
      return { id: args.postId };
      }, {
      fieldName: 'getPost',
      aggregate: false
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.batchResolver({ fieldName: 'getPosts' })
      async handleGetPosts(events) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.batchResolver({ fieldName: 'getPost', aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.batchResolver({ fieldName: 'getPost', aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch resolver function for GraphQL events that support batching.

      Registers a handler for a specific GraphQL field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field, and can either process requests individually or aggregate them for batch processing.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Type Parameters

      • TParams extends Record<string, unknown>
      • TSource = Record<string, unknown> | null

      Parameters

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver<{id: number}>(async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }, {
      fieldName: 'getPosts'
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver(async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, {
      fieldName: 'getPost',
      aggregate: false
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver(async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, {
      fieldName: 'getPost',
      aggregate: false,
      throwOnError: true
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      You can also specify the type of the arguments using generic type parameters for non-aggregated handlers:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver()

      app.batchResolver<{ postId: string }>(async (args, { event, context }) => {
      // args is typed as { postId: string }
      return { id: args.postId };
      }, {
      fieldName: 'getPost',
      aggregate: false
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.batchResolver({ fieldName: 'getPosts' })
      async handleGetPosts(events) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.batchResolver({ fieldName: 'getPost', aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.batchResolver({ fieldName: 'getPost', aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch resolver function for GraphQL events that support batching.

      Registers a handler for a specific GraphQL field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field, and can either process requests individually or aggregate them for batch processing.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Type Parameters

      • T extends boolean = true
      • R extends boolean = false

      Parameters

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver<{id: number}>(async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }, {
      fieldName: 'getPosts'
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver(async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, {
      fieldName: 'getPost',
      aggregate: false
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.batchResolver(async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, {
      fieldName: 'getPost',
      aggregate: false,
      throwOnError: true
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      You can also specify the type of the arguments using generic type parameters for non-aggregated handlers:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver()

      app.batchResolver<{ postId: string }>(async (args, { event, context }) => {
      // args is typed as { postId: string }
      return { id: args.postId };
      }, {
      fieldName: 'getPost',
      aggregate: false
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.batchResolver({ fieldName: 'getPosts' })
      async handleGetPosts(events) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.batchResolver({ fieldName: 'getPost', aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.batchResolver({ fieldName: 'getPost', aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register an exception handler for a specific error class.

      Registers a handler for a specific error class that can be thrown by GraphQL resolvers. The handler will be invoked when an error of the specified class is thrown from any resolver function.

      Type Parameters

      • T extends Error

      Parameters

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import { AssertionError } from 'assert';

      const app = new AppSyncGraphQLResolver();

      // Register an exception handler for AssertionError
      app.exceptionHandler(AssertionError, async (error) => {
      return {
      error: {
      message: error.message,
      type: error.name
      }
      };
      });

      // Register a resolver that might throw an AssertionError
      app.onQuery('createSomething', async () => {
      throw new AssertionError({
      message: 'This is an assertion Error',
      });
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import { AssertionError } from 'assert';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.exceptionHandler(AssertionError)
      async handleAssertionError(error: AssertionError) {
      return {
      error: {
      message: error.message,
      type: error.name
      }
      };
      }

      ⁣@app.onQuery('getUser')
      async getUser() {
      throw new AssertionError({
      message: 'This is an assertion Error',
      });
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register an exception handler for a specific error class.

      Registers a handler for a specific error class that can be thrown by GraphQL resolvers. The handler will be invoked when an error of the specified class is thrown from any resolver function.

      Type Parameters

      • T extends Error

      Parameters

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import { AssertionError } from 'assert';

      const app = new AppSyncGraphQLResolver();

      // Register an exception handler for AssertionError
      app.exceptionHandler(AssertionError, async (error) => {
      return {
      error: {
      message: error.message,
      type: error.name
      }
      };
      });

      // Register a resolver that might throw an AssertionError
      app.onQuery('createSomething', async () => {
      throw new AssertionError({
      message: 'This is an assertion Error',
      });
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import { AssertionError } from 'assert';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.exceptionHandler(AssertionError)
      async handleAssertionError(error: AssertionError) {
      return {
      error: {
      message: error.message,
      type: error.name
      }
      };
      }

      ⁣@app.onQuery('getUser')
      async getUser() {
      throw new AssertionError({
      message: 'This is an assertion Error',
      });
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Merges resolver registries from another router into this router.

      This method combines the resolver registry, batch resolver registry, and exception handler registry from the provided router with the current router's registries.

      Parameters

      • router: Router

        The source router whose registries will be merged into this router

      Returns void

    • Register a batch handler function for the mutation event.

      Registers a batch handler for a specific GraphQL Mutation field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Mutation type.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Type Parameters

      • TParams extends Record<string, unknown>
      • TSource = Record<string, unknown> | null

      Parameters

      • fieldName: string

        The name of the Mutation field to register the batch handler for.

      • handler:
            | BatchResolverAggregateHandlerFn<TParams, TSource>
            | BatchResolverSyncAggregateHandlerFn<TParams, TSource>

        The batch handler function to be called when events are received.

      • Optionaloptions: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation<{ id: number }>('createPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchMutation('createPosts')
      async handleCreatePosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false })
      async handleCreatePost(args, { event, context }) {
      // Process individual request
      return { id: args.id, status: 'created' };
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false, throwOnError: true })
      async handleCreatePostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, status: 'created' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the mutation event.

      Registers a batch handler for a specific GraphQL Mutation field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Mutation type.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Type Parameters

      • TParams extends Record<string, unknown>
      • TSource = Record<string, unknown> | null

      Parameters

      • fieldName: string

        The name of the Mutation field to register the batch handler for.

      • handler:
            | BatchResolverHandlerFn<TParams, TSource>
            | BatchResolverSyncHandlerFn<TParams, TSource>

        The batch handler function to be called when events are received.

      • Optionaloptions: Omit<GraphQlBatchRouteOptions<false, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation<{ id: number }>('createPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchMutation('createPosts')
      async handleCreatePosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false })
      async handleCreatePost(args, { event, context }) {
      // Process individual request
      return { id: args.id, status: 'created' };
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false, throwOnError: true })
      async handleCreatePostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, status: 'created' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the mutation event.

      Registers a batch handler for a specific GraphQL Mutation field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Mutation type.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Parameters

      • fieldName: string

        The name of the Mutation field to register the batch handler for.

      • options: Omit<GraphQlBatchRouteOptions<false, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation<{ id: number }>('createPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchMutation('createPosts')
      async handleCreatePosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false })
      async handleCreatePost(args, { event, context }) {
      // Process individual request
      return { id: args.id, status: 'created' };
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false, throwOnError: true })
      async handleCreatePostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, status: 'created' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the mutation event.

      Registers a batch handler for a specific GraphQL Mutation field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Mutation type.

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Parameters

      • fieldName: string

        The name of the Mutation field to register the batch handler for.

      • Optionaloptions: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation<{ id: number }>('createPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchMutation('createPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, status: 'created' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchMutation('createPosts')
      async handleCreatePosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, status: 'created' }));
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false })
      async handleCreatePost(args, { event, context }) {
      // Process individual request
      return { id: args.id, status: 'created' };
      }

      ⁣@app.onBatchMutation('createPost', { aggregate: false, throwOnError: true })
      async handleCreatePostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, status: 'created' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the query event.

      Registers a batch handler for a specific GraphQL Query field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Query type.

      Type Parameters

      • TParams extends Record<string, unknown>
      • TSource = Record<string, unknown> | null

      Parameters

      • fieldName: string

        The name of the Query field to register the batch handler for.

      • handler:
            | BatchResolverAggregateHandlerFn<TParams, TSource>
            | BatchResolverSyncAggregateHandlerFn<TParams, TSource>

        The batch handler function to be called when events are received.

      • Optionaloptions: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';


      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery<{ id: number }>('getPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchQuery('getPosts')
      async handleGetPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the query event.

      Registers a batch handler for a specific GraphQL Query field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Query type.

      Type Parameters

      • TParams extends Record<string, unknown>
      • TSource = Record<string, unknown> | null

      Parameters

      • fieldName: string

        The name of the Query field to register the batch handler for.

      • handler:
            | BatchResolverHandlerFn<TParams, TSource>
            | BatchResolverSyncHandlerFn<TParams, TSource>

        The batch handler function to be called when events are received.

      • Optionaloptions: Omit<GraphQlBatchRouteOptions<false, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';


      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery<{ id: number }>('getPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchQuery('getPosts')
      async handleGetPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the query event.

      Registers a batch handler for a specific GraphQL Query field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Query type.

      Parameters

      • fieldName: string

        The name of the Query field to register the batch handler for.

      • options: Omit<GraphQlBatchRouteOptions<false, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';


      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery<{ id: number }>('getPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchQuery('getPosts')
      async handleGetPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a batch handler function for the query event.

      Registers a batch handler for a specific GraphQL Query field that can process multiple requests in a batch. The handler will be invoked when requests are made for the specified field in the Query type.

      Parameters

      • fieldName: string

        The name of the Query field to register the batch handler for.

      • Optionaloptions: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">

        Optional batch configuration including aggregate and throwOnError settings.

        • aggregate

          Whether to aggregate multiple requests into a single handler call, defaults to true.

        • throwOnError

          Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false.

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';


      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery<{ id: number }>('getPosts', async (events) => {
      // Process all events in batch
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      By default, the handler will receive all batch events at once as an array and you are responsible for processing them and returning an array of results. The first parameter is an array of events, while the second parameter provides the original event array and context.

      If your function throws an error, we catch it and format the error response to be sent back to AppSync. This helps the client understand what went wrong and handle the error accordingly.

      It's important to note that if your function throws an error when processing in aggregate mode, the entire batch of events will be affected.

      Process events individually

      If you want to process each event individually instead of receiving all events at once, you can set the aggregate option to false. In this case, the handler will be called once for each event in the batch, similar to regular resolvers.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      When the handler is called, the first parameter contains the arguments from the GraphQL request, while the second parameter provides the original event and context, similar to regular resolvers.

      When aggregate is false, by default if one of the events in the batch throws an error, we catch it and append null for that specific event in the results array, allowing other events to be processed successfully. This provides graceful error handling where partial failures don't affect the entire batch.

      Strict error handling

      If you want stricter error handling when processing events individually, you can set the throwOnError option to true. In this case, if any event throws an error, the entire batch processing will stop and the error will be propagated. Note that throwOnError can only be used when aggregate is set to false.

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onBatchQuery('getPost', async (args, { event, context }) => {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }, { aggregate: false, throwOnError: true });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onBatchQuery('getPosts')
      async handleGetPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false })
      async handleGetPost(args, { event, context }) {
      // Process individual request
      return { id: args.id, data: 'processed' };
      }

      ⁣@app.onBatchQuery('getPost', { aggregate: false, throwOnError: true })
      async handleGetPostStrict(args, { event, context }) {
      // Process individual request with strict error handling
      return { id: args.id, data: 'processed' };
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a handler function for the mutation event.

      Registers a handler for a specific GraphQL Mutation field. The handler will be invoked when a request is made for the specified field in the Mutation type.

      Type Parameters

      • TParams extends Record<string, unknown>

      Parameters

      • fieldName: string

        The name of the Mutation field to register the handler for.

      • handler: ResolverHandler<TParams>

        The handler function to be called when the event is received.

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onMutation('createPost', async (payload) => {
      // your business logic here
      return payload;
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onMutation('createPost')
      async handleCreatePost(payload) {
      // your business logic here
      return payload;
      }

      async handler(event, context) {
      return app.resolve(event, context);
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a handler function for the mutation event.

      Registers a handler for a specific GraphQL Mutation field. The handler will be invoked when a request is made for the specified field in the Mutation type.

      Parameters

      • fieldName: string

        The name of the Mutation field to register the handler for.

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.onMutation('createPost', async (payload) => {
      // your business logic here
      return payload;
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.onMutation('createPost')
      async handleCreatePost(payload) {
      // your business logic here
      return payload;
      }

      async handler(event, context) {
      return app.resolve(event, context);
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a handler function for the query event.

      • Registers a handler for a specific GraphQL Query field. The handler will be invoked when a request is made
      • for the specified field in the Query type.

      Type Parameters

      • TParams extends Record<string, unknown>

      Parameters

      • fieldName: string

        The name of the Query field to register the handler for. *

      • handler: ResolverHandler<TParams>

        The handler function to be called when the event is received.

      Returns void

      • 
        
      • import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      • const app = new AppSyncGraphQLResolver();

      • app.onQuery('getPost', async (payload) => {

      • // your business logic here

      • return payload;

      • });

      • export const handler = async (event, context) =>

      • app.resolve(event, context);

      • 
        
      • As a decorator:

      • 
        
      • import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      • const app = new AppSyncGraphQLResolver();
      • class Lambda {
      • ⁣@app.onQuery('getPost')
      • async handleGetPost(payload) {
      • // your business logic here
        
      • return payload;
        
      • }
      • async handler(event, context) {
      • return app.resolve(event, context);
        
      • }
      • }
      • const lambda = new Lambda();
      • export const handler = lambda.handler.bind(lambda);
      • 
        
    • Register a handler function for the query event.

      • Registers a handler for a specific GraphQL Query field. The handler will be invoked when a request is made
      • for the specified field in the Query type.

      Parameters

      • fieldName: string

        The name of the Query field to register the handler for. *

      Returns MethodDecorator

      • 
        
      • import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      • const app = new AppSyncGraphQLResolver();

      • app.onQuery('getPost', async (payload) => {

      • // your business logic here

      • return payload;

      • });

      • export const handler = async (event, context) =>

      • app.resolve(event, context);

      • 
        
      • As a decorator:

      • 
        
      • import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      • const app = new AppSyncGraphQLResolver();
      • class Lambda {
      • ⁣@app.onQuery('getPost')
      • async handleGetPost(payload) {
      • // your business logic here
        
      • return payload;
        
      • }
      • async handler(event, context) {
      • return app.resolve(event, context);
        
      • }
      • }
      • const lambda = new Lambda();
      • export const handler = lambda.handler.bind(lambda);
      • 
        
    • Register a resolver function for any GraphQL event.

      Registers a handler for a specific GraphQL field. The handler will be invoked when a request is made for the specified field.

      Type Parameters

      • TParams extends Record<string, unknown>

      Parameters

      • handler: ResolverHandler<TParams>

        The handler function to be called when the event is received.

      • options: GraphQlRouteOptions

        Route options including the required fieldName and optional typeName.

        Options for registering a route

        • fieldName: string

          The name of the field to be registered

        • OptionaltypeName?: string

          The type name of the event to be registered, i.e. Query, Mutation, or a custom type

      Returns void

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      // Register a Query resolver
      app.resolver(async (payload) => {
      // your business logic here
      return payload;
      }, {
      fieldName: 'getPost'
      });

      // Register a Mutation resolver
      app.resolver(async (payload) => {
      // your business logic here
      return payload;
      }, {
      fieldName: 'createPost',
      typeName: 'Mutation'
      });

      // Register a batch resolver
      app.batchResolver<{ id: number }>(async (events) => {
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }, {
      fieldName: 'getPosts',
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      You can also specify the type of the arguments using a generic type parameter:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.resolver<{ postId: string }>(async ({ postId }) => {
      // postId is now typed as string
      return { id: postId };
      }, {
      fieldName: 'getPost'
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';
      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.resolver({ fieldName: 'getPost' })
      async handleGetPost(payload) {
      // your business logic here
      return payload;
      }

      @app.batchResolver({ fieldName: 'getPosts' })
      async handleGetPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);
    • Register a resolver function for any GraphQL event.

      Registers a handler for a specific GraphQL field. The handler will be invoked when a request is made for the specified field.

      Parameters

      • options: GraphQlRouteOptions

        Route options including the required fieldName and optional typeName.

        Options for registering a route

        • fieldName: string

          The name of the field to be registered

        • OptionaltypeName?: string

          The type name of the event to be registered, i.e. Query, Mutation, or a custom type

      Returns MethodDecorator

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      // Register a Query resolver
      app.resolver(async (payload) => {
      // your business logic here
      return payload;
      }, {
      fieldName: 'getPost'
      });

      // Register a Mutation resolver
      app.resolver(async (payload) => {
      // your business logic here
      return payload;
      }, {
      fieldName: 'createPost',
      typeName: 'Mutation'
      });

      // Register a batch resolver
      app.batchResolver<{ id: number }>(async (events) => {
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }, {
      fieldName: 'getPosts',
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      You can also specify the type of the arguments using a generic type parameter:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';

      const app = new AppSyncGraphQLResolver();

      app.resolver<{ postId: string }>(async ({ postId }) => {
      // postId is now typed as string
      return { id: postId };
      }, {
      fieldName: 'getPost'
      });

      export const handler = async (event, context) =>
      app.resolve(event, context);

      As a decorator:

      import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
      import type { AppSyncResolverEvent } from 'aws-lambda';
      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.resolver({ fieldName: 'getPost' })
      async handleGetPost(payload) {
      // your business logic here
      return payload;
      }

      @app.batchResolver({ fieldName: 'getPosts' })
      async handleGetPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
      // Process batch of events
      return events.map(event => ({ id: event.arguments.id, data: 'processed' }));
      }

      async handler(event, context) {
      return app.resolve(event, context, {
      scope: this, // bind decorated methods to the class instance
      });
      }
      }

      const lambda = new Lambda();
      export const handler = lambda.handler.bind(lambda);