Skip to content

Commit 7f094f9

Browse files
committed
refactor: extract sharedContext empty check in a method
1 parent e0a7d61 commit 7f094f9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,7 @@ class AppSyncGraphQLResolver extends Router {
426426
{
427427
event: events,
428428
context,
429-
...(this.sharedContext.size > 0 && {
430-
sharedContext: this.sharedContext,
431-
}),
429+
...this.#getSharedContextOnlyIfNotEmpty(),
432430
},
433431
]);
434432

@@ -451,9 +449,7 @@ class AppSyncGraphQLResolver extends Router {
451449
{
452450
event,
453451
context,
454-
...(this.sharedContext.size > 0 && {
455-
sharedContext: this.sharedContext,
456-
}),
452+
...this.#getSharedContextOnlyIfNotEmpty(),
457453
},
458454
]);
459455
results.push(result);
@@ -468,9 +464,7 @@ class AppSyncGraphQLResolver extends Router {
468464
{
469465
event: events[i],
470466
context,
471-
...(this.sharedContext.size > 0 && {
472-
sharedContext: this.sharedContext,
473-
}),
467+
...this.#getSharedContextOnlyIfNotEmpty(),
474468
},
475469
]);
476470
results.push(result);
@@ -518,9 +512,7 @@ class AppSyncGraphQLResolver extends Router {
518512
{
519513
event,
520514
context,
521-
...(this.sharedContext.size > 0 && {
522-
sharedContext: this.sharedContext,
523-
}),
515+
...this.#getSharedContextOnlyIfNotEmpty(),
524516
},
525517
]
526518
);
@@ -546,6 +538,19 @@ class AppSyncGraphQLResolver extends Router {
546538
error: 'An unknown error occurred',
547539
};
548540
}
541+
542+
/**
543+
* Returns an object containing the shared context only if it has entries.
544+
* This helps avoid passing an empty map to handlers.
545+
*/
546+
#getSharedContextOnlyIfNotEmpty(): {
547+
sharedContext: Map<string, unknown> | undefined;
548+
} {
549+
return {
550+
sharedContext:
551+
this.sharedContext.size > 0 ? this.sharedContext : undefined,
552+
};
553+
}
549554
}
550555

551556
export { AppSyncGraphQLResolver };

0 commit comments

Comments
 (0)