Skip to content

Commit aef50b9

Browse files
committed
refactor: remove sharedContext from AppSyncGraphQLResolver and related types
1 parent 2b40997 commit aef50b9

File tree

3 files changed

+20
-449
lines changed

3 files changed

+20
-449
lines changed

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

Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AppSyncResolverEvent, Context } from 'aws-lambda';
22
import type {
33
BatchResolverAggregateHandlerFn,
44
BatchResolverHandlerFn,
5-
GraphQlRouterOptions,
65
ResolverHandler,
76
RouteHandlerOptions,
87
} from '../types/appsync-graphql.js';
@@ -43,16 +42,6 @@ import { isAppSyncGraphQLEvent } from './utils.js';
4342
* ```
4443
*/
4544
class AppSyncGraphQLResolver extends Router {
46-
/**
47-
* A map to hold shared contextual data accessible to all resolver handlers.
48-
*/
49-
public readonly sharedContext: Map<string, unknown>;
50-
51-
public constructor(options?: GraphQlRouterOptions) {
52-
super(options);
53-
this.sharedContext = new Map<string, unknown>();
54-
}
55-
5645
/**
5746
* Resolve the response based on the provided event and route handlers configured.
5847
*
@@ -172,18 +161,11 @@ class AppSyncGraphQLResolver extends Router {
172161
return;
173162
}
174163

175-
try {
176-
return this.#withErrorHandling(
177-
() => this.#executeBatchResolvers(event, context, options),
178-
event[0],
179-
options
180-
);
181-
} finally {
182-
/**
183-
* Clear shared context after batch processing for safety
184-
*/
185-
this.sharedContext.clear();
186-
}
164+
return this.#withErrorHandling(
165+
() => this.#executeBatchResolvers(event, context, options),
166+
event[0],
167+
options
168+
);
187169
}
188170
if (!isAppSyncGraphQLEvent(event)) {
189171
this.logger.warn(
@@ -192,18 +174,11 @@ class AppSyncGraphQLResolver extends Router {
192174
return;
193175
}
194176

195-
try {
196-
return this.#withErrorHandling(
197-
() => this.#executeSingleResolver(event, context, options),
198-
event,
199-
options
200-
);
201-
} finally {
202-
/**
203-
* Clear shared context after batch processing for safety
204-
*/
205-
this.sharedContext.clear();
206-
}
177+
return this.#withErrorHandling(
178+
() => this.#executeSingleResolver(event, context, options),
179+
event,
180+
options
181+
);
207182
}
208183

209184
/**
@@ -244,46 +219,6 @@ class AppSyncGraphQLResolver extends Router {
244219
this.logger.debug('Router included successfully');
245220
}
246221

247-
/**
248-
* Appends contextual data to be shared with all resolver handlers.
249-
*
250-
* This method allows you to add key-value pairs to the shared context that will be
251-
* accessible to all resolver handlers through the `sharedContext` parameter. The context
252-
* is automatically cleared after each invocation for safety.
253-
*
254-
* @example
255-
* ```ts
256-
* import { AppSyncGraphQLResolver, Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';
257-
*
258-
* const postRouter = new Router();
259-
* postRouter.onQuery('getPosts', async ({ sharedContext }) => {
260-
* const requestId = sharedContext?.get('requestId');
261-
* return [{ id: 1, title: 'Post 1', requestId }];
262-
* });
263-
*
264-
* const userRouter = new Router();
265-
* userRouter.onQuery('getUsers', async ({ sharedContext }) => {
266-
* const requestId = sharedContext?.get('requestId');
267-
* return [{ id: 1, name: 'John Doe', requestId }];
268-
* });
269-
*
270-
* const app = new AppSyncGraphQLResolver();
271-
*
272-
* app.includeRouter([userRouter, postRouter]);
273-
* app.appendContext({ requestId: '12345' });
274-
*
275-
* export const handler = async (event, context) =>
276-
* app.resolve(event, context);
277-
* ```
278-
*
279-
* @param data - A record of key-value pairs to add to the shared context
280-
*/
281-
public appendContext(data: Record<string, unknown>): void {
282-
for (const [key, value] of Object.entries(data)) {
283-
this.sharedContext.set(key, value);
284-
}
285-
}
286-
287222
/**
288223
* Executes the provided asynchronous function with error handling.
289224
* If the function throws an error, it delegates error processing to `#handleError`
@@ -422,7 +357,6 @@ class AppSyncGraphQLResolver extends Router {
422357
{
423358
event: events,
424359
context,
425-
...this.#getSharedContextOnlyIfNotEmpty(),
426360
},
427361
]);
428362

@@ -445,7 +379,6 @@ class AppSyncGraphQLResolver extends Router {
445379
{
446380
event,
447381
context,
448-
...this.#getSharedContextOnlyIfNotEmpty(),
449382
},
450383
]);
451384
results.push(result);
@@ -460,7 +393,6 @@ class AppSyncGraphQLResolver extends Router {
460393
{
461394
event: events[i],
462395
context,
463-
...this.#getSharedContextOnlyIfNotEmpty(),
464396
},
465397
]);
466398
results.push(result);
@@ -508,7 +440,6 @@ class AppSyncGraphQLResolver extends Router {
508440
{
509441
event,
510442
context,
511-
...this.#getSharedContextOnlyIfNotEmpty(),
512443
},
513444
]
514445
);
@@ -534,19 +465,6 @@ class AppSyncGraphQLResolver extends Router {
534465
error: 'An unknown error occurred',
535466
};
536467
}
537-
538-
/**
539-
* Returns an object containing the shared context only if it has entries.
540-
* This helps avoid passing an empty map to handlers.
541-
*/
542-
#getSharedContextOnlyIfNotEmpty(): {
543-
sharedContext: Map<string, unknown> | undefined;
544-
} {
545-
return {
546-
sharedContext:
547-
this.sharedContext.size > 0 ? this.sharedContext : undefined,
548-
};
549-
}
550468
}
551469

552470
export { AppSyncGraphQLResolver };

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type BatchResolverSyncHandlerFn<
1414
options: {
1515
event: AppSyncResolverEvent<TParams, TSource>;
1616
context: Context;
17-
sharedContext?: Map<string, unknown>;
1817
}
1918
) => unknown;
2019

@@ -26,7 +25,6 @@ type BatchResolverHandlerFn<
2625
options: {
2726
event: AppSyncResolverEvent<TParams, TSource>;
2827
context: Context;
29-
sharedContext?: Map<string, unknown>;
3028
}
3129
) => Promise<unknown>;
3230

@@ -38,7 +36,6 @@ type BatchResolverAggregateHandlerFn<
3836
options: {
3937
event: AppSyncResolverEvent<TParams, TSource>[];
4038
context: Context;
41-
sharedContext?: Map<string, unknown>;
4239
}
4340
) => Promise<unknown>;
4441

@@ -50,7 +47,6 @@ type BatchResolverSyncAggregateHandlerFn<
5047
options: {
5148
event: AppSyncResolverEvent<TParams, TSource>[];
5249
context: Context;
53-
sharedContext?: Map<string, unknown>;
5450
}
5551
) => unknown;
5652

@@ -74,7 +70,6 @@ type ResolverSyncHandlerFn<TParams = Record<string, unknown>> = (
7470
options: {
7571
event: AppSyncResolverEvent<TParams>;
7672
context: Context;
77-
sharedContext?: Map<string, unknown>;
7873
}
7974
) => unknown;
8075

@@ -83,7 +78,6 @@ type ResolverHandlerFn<TParams = Record<string, unknown>> = (
8378
options: {
8479
event: AppSyncResolverEvent<TParams>;
8580
context: Context;
86-
sharedContext?: Map<string, unknown>;
8781
}
8882
) => Promise<unknown>;
8983

0 commit comments

Comments
 (0)