API Reference
    Preparing search index...

    Resolver for AWS AppSync GraphQL APIs.

    This resolver is designed to handle GraphQL events from AWS AppSync GraphQL APIs. It allows you to register handlers for these events and route them to the appropriate functions based on the event's field & type.

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

    const app = new AppSyncGraphQLResolver();

    app.resolver(async ({ id }) => {
    // your business logic here
    return {
    id,
    title: 'Post Title',
    content: 'Post Content',
    };
    }, {
    fieldName: 'getPost',
    typeName: 'Query'
    });

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

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    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 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);
      • 
        
    • Resolve the response based on the provided event and route handlers configured.

      Parameters

      • event: unknown

        The incoming event, which may be an AppSync GraphQL event or an array of events.

      • context: Context

        The AWS Lambda context object.

      • Optionaloptions: ResolveOptions

        Optional parameters for the resolver, such as the scope of the handler.

      Returns Promise<unknown>

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

      const app = new AppSyncGraphQLResolver();

      app.resolver(async ({ id }) => {
      // your business logic here
      return {
      id,
      title: 'Post Title',
      content: 'Post Content',
      };
      }, {
      fieldName: 'getPost',
      typeName: 'Query'
      });

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

      The method works also as class method decorator, so you can use it like this:

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

      const app = new AppSyncGraphQLResolver();

      class Lambda {
      ⁣@app.resolver({ fieldName: 'getPost', typeName: 'Query' })
      async handleGetPost({ id }) {
      // your business logic here
      return {
      id,
      title: 'Post Title',
      content: 'Post Content',
      };
      }

      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.

      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'
      });

      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';

      const app = new AppSyncGraphQLResolver();

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

      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'
      });

      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';

      const app = new AppSyncGraphQLResolver();

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

      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);