Optional
options: GraphQlRouterOptionsProtected
Readonly
batchA map of registered routes for GraphQL batch events, keyed by their fieldNames.
Protected
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 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.
The batch handler function to be called when events are received.
Batch route options including the required fieldName and optional configuration.
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.
The batch handler function to be called when events are received.
Batch route options including the required fieldName and optional configuration.
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.
Batch route options including the required fieldName and optional configuration.
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 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.
The name of the Mutation field to register the batch handler for.
The batch handler function to be called when events are received.
Optional
options: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Mutation field to register the batch handler for.
The batch handler function to be called when events are received.
Optional
options: Omit<GraphQlBatchRouteOptions<false, boolean>, "fieldName" | "typeName">Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Mutation field to register the batch handler for.
Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Mutation field to register the batch handler for.
Optional
options: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Query field to register the batch handler for.
The batch handler function to be called when events are received.
Optional
options: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Query field to register the batch handler for.
The batch handler function to be called when events are received.
Optional
options: Omit<GraphQlBatchRouteOptions<false, boolean>, "fieldName" | "typeName">Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Query field to register the batch handler for.
Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
The name of the Query field to register the batch handler for.
Optional
options: Omit<GraphQlBatchRouteOptions<true, boolean>, "fieldName" | "typeName">Optional batch configuration including aggregate and throwOnError settings.
Whether to aggregate multiple requests into a single handler call, defaults to true
.
Whether to raise errors when processing individual requests (only available when aggregate is false), defaults to false
.
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.
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);
Resolves the response based on the provided batch event and route handlers configured.
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
const app = new AppSyncGraphQLResolver();
app.batchResolver<{ id: number }>(async (events) => {
// your business logic here
const ids = events.map((event) => event.arguments.id);
return ids.map((id) => ({
id,
title: 'Post Title',
content: 'Post Content',
}));
});
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);
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
import type { AppSyncResolverEvent } from 'aws-lambda';
const app = new AppSyncGraphQLResolver();
class Lambda {
@app.batchResolver({ fieldName: 'getPosts', typeName: 'Query' })
async getPosts(events: AppSyncResolverEvent<{ id: number }>[]) {
// your business logic here
const ids = events.map((event) => event.arguments.id);
return ids.map((id) => ({
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'
});
// 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.
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'
});
// 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);
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