Repository navigation
feat: AppSync Lambda BatchInvoke support (createAppSyncBatchHandler + maxBatchSize) - #388
Merged
Merged
Conversation
Add the batched counterpart to createAppSyncHandler. It takes an array of resolver events, authenticates once per batch off the first event, and returns R[] in source order. The context carries both ctx.event (events[0]) and the full ctx.events array. Factor the typed-error normalisation and envelope construction out of createAppSyncHandler into shared toTypedError/buildErrorEnvelope helpers so both handlers use the identical error model. createAppSyncHandler behavior is unchanged. With APPSYNC_TYPED_ERRORS=true the batch handler returns one error envelope per event; otherwise it rethrows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #387.
Adds first-class AppSync Lambda BatchInvoke support. Two additive, backward-compatible pieces.
createAppSyncBatchHandler
New factory
createAppSyncBatchHandler<T, R, S>insrc/lambda/handler.ts, returning anAWSLambda.AppSyncBatchResolverHandler<T, R, S>. Array of events in,R[]out in source order. It mirrorscreateAppSyncHandler's context and auth plumbing but operates over a batch:events[0]with a freshAppSyncCognitoAuthorizer, same logger metadata as the single handler.AppSyncBatchHandlerContext<T, S>extendsAppSyncHandlerContext<T>and exposes bothctx.event(=events[0]) and the fullctx.events, so existing handler code that readsctx.event.argumentskeeps compiling.createAppSyncHandlerinto sharedtoTypedError/buildErrorEnvelopehelpers so both handlers run one error path and cannot drift.maxBatchSize on addLambdaResolver
New optional argument
LambdaResolverOptions { maxBatchSize?: number }onGraphQlApi.addLambdaResolverinsrc/constructs/graphql.ts. When set, the default UNIT resolver getsMaxBatchSize, the BatchInvoke request templateMappingTemplate.lambdaRequest('$utils.toJson($context)', 'BatchInvoke'), and thelambdaResult()response template. Unset keeps today's exact single-invoke shape. CombiningmaxBatchSizewithtypedErrorsthrows before any resource is created, since the BatchInvoke templates have no equivalent on the APPSYNC_JS pipeline resolver.Error semantics
Batch errors are fail-whole-batch by design. A thrown typed error returns one identical envelope per event (AppSync's BatchInvoke contract expects one result per event); a plain Error always rethrows. This matches the single handler. A caller wanting per-event partial success catches inside its own
ctx.events.map.Compatibility
Fully additive. New export
createAppSyncBatchHandlerand a new optional arg onaddLambdaResolver. Existing single-item resolvers are untouched, verified byte-for-byte viaMatch.absent()assertions on the no-options resolver.Testing
npm run build(compile + jest + package): green.npm run test: 14 suites, 147 tests pass (+10 new). New tests cover order preservation, once-per-batch auth offevents[0], per-event envelope fan-out, rethrow paths, and CDK template assertions forMaxBatchSize+ BatchInvoke/lambdaResulttemplates plus thetypedErrorsguard.npm run eslint: clean.Notes
The
Stype parameter is boundedS extends Record<string, any> | null(the upstream default in@types/aws-lambda), becauseS = anydoes not compile againstAppSyncBatchResolverHandler.maxBatchSizeis forwarded straight to the L2 and is not range-validated at synth time; AppSync caps it at 2000, so an out-of-range value fails at deploy.