Optional
options: GraphQlRouterOptionsProtected
Readonly
isWhether the router is running in development mode.
Protected
Readonly
loggerA 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.
Protected
Readonly
resolverA map of registered routes for all GraphQL events, keyed by their fieldNames.
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.
The name of the Mutation field to register the handler for.
The handler function to be called when the event is received.
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.
The name of the Mutation field to register the handler for.
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.
The name of the Query field to register the handler for. *
The handler function to be called when the event is received.
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:
// your business logic here
return payload;
return app.resolve(event, context);
Register a handler function for the query
event.
The name of the Query field to register the handler for. *
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:
// your business logic here
return payload;
return app.resolve(event, context);
Resolve the response based on the provided event and route handlers configured.
The incoming event, which may be an AppSync GraphQL event or an array of events.
The AWS Lambda context object.
Optional
options: ResolveOptionsOptional parameters for the resolver, such as the scope of the handler.
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.
The handler function to be called when the event is received.
Route options including the required fieldName and optional typeName.
Options for registering a route
The name of the field to be registered
Optional
typeName?: stringThe type name of the event to be registered, i.e. Query
, Mutation
, or a custom type
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.
Route options including the required fieldName and optional typeName.
Options for registering a route
The name of the field to be registered
Optional
typeName?: stringThe type name of the event to be registered, i.e. Query
, Mutation
, or a custom type
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);
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.
Example