Skip to content

Commit 9d8ff9f

Browse files
committed
feat: enhance AppSyncGraphQLResolver to pass context to resolver handlers and update tests for event and context access
1 parent 46b6f19 commit 9d8ff9f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

packages/event-handler/src/appsync-graphql/AppSyncGraphQLResolver.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class AppSyncGraphQLResolver extends Router {
101101
return;
102102
}
103103
try {
104-
return await this.#executeSingleResolver(event);
104+
return await this.#executeSingleResolver(event, context);
105105
} catch (error) {
106106
this.logger.error(
107107
`An error occurred in handler ${event.info.fieldName}`,
@@ -120,17 +120,25 @@ export class AppSyncGraphQLResolver extends Router {
120120
* with the event arguments. If no handler is found, it throws a `ResolverNotFoundException`.
121121
*
122122
* @param event - The AppSync GraphQL event containing resolver information.
123+
* @param context - The Lambda execution context.
123124
* @throws {ResolverNotFoundException} If no resolver is registered for the given field and type.
124125
*/
125-
async #executeSingleResolver(event: AppSyncGraphQLEvent): Promise<unknown> {
126+
async #executeSingleResolver(
127+
event: AppSyncGraphQLEvent,
128+
context: Context
129+
): Promise<unknown> {
126130
const { fieldName, parentTypeName: typeName } = event.info;
127131

128132
const resolverHandlerOptions = this.resolverRegistry.resolve(
129133
typeName,
130134
fieldName
131135
);
132136
if (resolverHandlerOptions) {
133-
return resolverHandlerOptions.handler.apply(this, [event.arguments]);
137+
return resolverHandlerOptions.handler.apply(this, [
138+
event.arguments,
139+
event,
140+
context,
141+
]);
134142
}
135143

136144
throw new ResolverNotFoundException(

packages/event-handler/src/types/appsync-graphql.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import type { Context } from 'aws-lambda';
12
import type { RouteHandlerRegistry } from '../appsync-graphql/RouteHandlerRegistry.js';
23
import type { GenericLogger } from './common.js';
34

45
// #region Resolver fn
56

67
type ResolverSyncHandlerFn<TParams = Record<string, unknown>> = (
7-
args: TParams
8+
args: TParams,
9+
event: AppSyncGraphQLEvent,
10+
context: Context
811
) => unknown;
912

1013
type ResolverHandlerFn<TParams = Record<string, unknown>> = (
11-
args: TParams
14+
args: TParams,
15+
event: AppSyncGraphQLEvent,
16+
context: Context
1217
) => Promise<unknown>;
1318

1419
type ResolverHandler<TParams = Record<string, unknown>> =

packages/event-handler/tests/unit/appsync-graphql/AppSyncGraphQLResolver.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import context from '@aws-lambda-powertools/testing-utils/context';
2+
import { Context } from 'aws-lambda';
3+
import { AppSyncGraphQLEvent } from 'src/types/appsync-graphql.js';
24
import { onGraphqlEventFactory } from 'tests/helpers/factories.js';
35
import { beforeEach, describe, expect, it, vi } from 'vitest';
46
import { AppSyncGraphQLResolver } from '../../../src/appsync-graphql/AppSyncGraphQLResolver.js';
@@ -174,6 +176,34 @@ describe('Class: AppSyncGraphQLResolver', () => {
174176
});
175177
});
176178

179+
it('resolver function has access to event and context', async () => {
180+
// Prepare
181+
const app = new AppSyncGraphQLResolver({ logger: console });
182+
app.resolver<{ id: string }>(
183+
async ({ id }, event, context) => {
184+
return {
185+
id,
186+
event,
187+
context,
188+
};
189+
},
190+
{
191+
fieldName: 'getPost',
192+
}
193+
);
194+
195+
// Act
196+
const event = onGraphqlEventFactory('getPost', 'Query', { id: '123' });
197+
const result = await app.resolve(event, context);
198+
199+
// Assess
200+
expect(result).toStrictEqual({
201+
id: '123',
202+
event,
203+
context,
204+
});
205+
});
206+
177207
it('emits debug message when AWS_LAMBDA_LOG_LEVEL is set to DEBUG', async () => {
178208
// Prepare
179209
vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'DEBUG');

0 commit comments

Comments
 (0)