@@ -2,7 +2,6 @@ import type { AppSyncResolverEvent, Context } from 'aws-lambda';
22import 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 */
4544class 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
552470export { AppSyncGraphQLResolver } ;
0 commit comments