From 11b762d08b68acaea1f5a428f0db0cd053e8e5ce Mon Sep 17 00:00:00 2001 From: XuJian <265865031+xujiantop-crypto@users.noreply.github.com> Date: Tue, 29 Sep 2026 10:25:53 +0900 Subject: [PATCH] fix(appsync): send VTL Lambda payload to function (#4579) * fix(appsync): pass JS Lambda request envelope to function Signed-off-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com> * fix(appsync): send VTL Lambda payload to function --------- Signed-off-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com> --- docs/services/appsync.md | 9 ++--- .../datasource/LambdaDataSourceInvoker.java | 4 +- .../LambdaDataSourceInvokerTest.java | 34 +++++++++++----- .../resolver/AppSyncResolverExecutorTest.java | 39 +++++++++++++++++-- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/docs/services/appsync.md b/docs/services/appsync.md index a61a3ffdf2..2a63be1bd8 100644 --- a/docs/services/appsync.md +++ b/docs/services/appsync.md @@ -349,10 +349,9 @@ Supported request documents are: - `NONE`: `version` plus an optional `payload`, which is unwrapped into `ctx.result`. - DynamoDB: `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, and `Scan` requests. -- Lambda: `Invoke` with synchronous `RequestResponse` invocation. The entire resolved - VTL request document reaches the function, including `version` and `operation`. - When `payload` is omitted, it remains absent from that document, as shown by the - [AWS Lambda VTL mapping reference](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-lambda.html). +- Lambda: `Invoke` with synchronous `RequestResponse` invocation. The function receives + only the resolved `payload`; `version` and `operation` stay in the AppSync request document. + When `payload` is omitted, Floci invokes the function with an empty object. VTL `BatchInvoke` requires batching field requests and is not implemented by the per-field resolver callback. - RDS: one or two `statements`, with optional `variableMap` and `variableTypeHintMap`. @@ -426,7 +425,7 @@ environment variables). `ctx.request.headers` is empty: the GraphQL context does | Type | Behaviour | |---|---| | `NONE` | The request is the result; a `payload` member is unwrapped, as on AWS. Supports APPSYNC_JS and `2018-05-29` VTL UNIT resolvers. | -| `AWS_LAMBDA` | `Invoke` and `BatchInvoke` for APPSYNC_JS, and `Invoke` for `2018-05-29` VTL UNIT resolvers. VTL sends the entire resolved request document to the function. The existing APPSYNC_JS path sends only `payload`, a Floci deviation from AWS that this VTL feature does not change. A function error fails the field rather than resolving to the error object. | +| `AWS_LAMBDA` | `Invoke` and `BatchInvoke` for APPSYNC_JS, and `Invoke` for `2018-05-29` VTL UNIT resolvers. Both runtimes send only the resolved `payload` to the function; `BatchInvoke` sends the payload list. A function error fails the field rather than resolving to the error object. | | `RELATIONAL_DATABASE` | Statements run over the RDS Data API against `rdsHttpEndpointConfig`. Accepts `{statements, variableMap}`, the `{statement, parameters}` the `/rds` helpers build, a list of either, or a bare SQL string. `variableTypeHintMap` is honoured, without it a bound date binds as text and PostgreSQL refuses the comparison (`operator does not exist: timestamp with time zone >= character varying`), so a resolver's date filters need it. The result is wrapped as `{sqlStatementResults: […]}`, which is what `toJsonObject()` reads. Supports APPSYNC_JS and `2018-05-29` VTL UNIT resolvers. | | `AMAZON_DYNAMODB` | `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, through the native DynamoDB path so expressions, conditions and indexes all apply. Items come back as **plain JSON**, not attribute values, as AppSync returns them. `nextToken` is an opaque encoding of `LastEvaluatedKey`. `BatchGetItem`, `TransactWriteItems` and `Sync` are not implemented and say so. Supports APPSYNC_JS and `2018-05-29` VTL UNIT resolvers. | | `HTTP`, `AMAZON_EVENTBRIDGE`, `AMAZON_OPENSEARCH_SERVICE`, `AMAZON_BEDROCK_RUNTIME` | Not implemented; a resolver using one fails its field naming the type. | diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvoker.java b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvoker.java index b4d481e5fe..ec9508226a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvoker.java +++ b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvoker.java @@ -61,8 +61,8 @@ public class LambdaDataSourceInvoker implements AppSyncDataSourceInvoker { @Override public Object invokeVtl(DataSource dataSource, Object request, String region) { - // AWS sends the entire resolved VTL request document to the Lambda function. - return invokeOnce(functionArn(dataSource), request, region); + Object payload = request instanceof Map map ? map.get("payload") : null; + return invokeOnce(functionArn(dataSource), payload, region); } private Object invokeOnce(String functionArn, Object payload, String region) { diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvokerTest.java index 1cb10128c8..7c29133384 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvokerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/LambdaDataSourceInvokerTest.java @@ -60,25 +60,31 @@ class LambdaDataSourceInvokerTest { Map.of("version", "2018-05-29", "operation", "Invoke", "payload", Map.of("field", "getMessages")), "eu-west-1"); - // Preserve the existing APPSYNC_JS behavior independently of VTL. assertEquals("{\"field\":\"getMessages\"}", capturePayload()); assertEquals(Map.of("id", "1"), result); } @Test - void vtlSendsTheEntireRequestDocumentToTheFunction() throws Exception { + void vtlOnlyThePayloadReachesTheFunction() throws Exception { answers("{\"id\": \"1\"}"); Object result = invoker.invokeVtl(dataSource(), Map.of("version", "2018-05-29", "operation", "Invoke", "payload", Map.of("field", "getMessages")), "eu-west-1"); - assertEquals(Map.of("version", "2018-05-29", "operation", "Invoke", - "payload", Map.of("field", "getMessages")), - mapper.readValue(capturePayload(), Map.class)); + assertEquals(Map.of("field", "getMessages"), mapper.readValue(capturePayload(), Map.class)); assertEquals(Map.of("id", "1"), result); } + @Test + void vtlWithoutPayloadDoesNotSendRequestMetadata() throws Exception { + answers("{}"); + + invoker.invokeVtl(dataSource(), Map.of("version", "2018-05-29", "operation", "Invoke"), "eu-west-1"); + + assertEquals(Map.of(), mapper.readValue(capturePayload(), Map.class)); + } + @Test void aRequestWithNoEnvelopeIsThePayload() { answers("{}"); @@ -89,13 +95,23 @@ class LambdaDataSourceInvokerTest { } @Test - void aBatchInvokeAnswersTheListTheFunctionReturned() { + void aLegacyRequestWithoutOperationStillUnwrapsPayload() { + answers("{}"); + + invoker.invoke(dataSource(), Map.of("payload", Map.of("field", "getMessages")), "eu-west-1"); + + assertEquals("{\"field\":\"getMessages\"}", capturePayload()); + } + + @Test + void aBatchInvokeSendsOnlyThePayloadAndAnswersTheList() throws Exception { answers("[{\"id\": \"1\"}, {\"id\": \"2\"}]"); - Object result = invoker.invoke(dataSource(), - Map.of("operation", "BatchInvoke", "payload", List.of(Map.of("id", "1"), Map.of("id", "2"))), - "eu-west-1"); + Map request = Map.of("operation", "BatchInvoke", + "payload", List.of(Map.of("id", "1"), Map.of("id", "2"))); + Object result = invoker.invoke(dataSource(), request, "eu-west-1"); + assertEquals(request.get("payload"), mapper.readValue(capturePayload(), List.class)); assertEquals(List.of(Map.of("id", "1"), Map.of("id", "2")), result); } diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutorTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutorTest.java index deee92c4a0..ac48f3de4a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutorTest.java @@ -629,7 +629,35 @@ class AppSyncResolverExecutorTest { } @Test - void vtlLambdaInvokeWithoutPayloadSendsTheDocumentToTheFunction() throws Exception { + void jsLambdaResolverSendsOnlyThePayloadToTheFunction() throws Exception { + String functionArn = "arn:aws:lambda:eu-west-1:000000000000:function:resolver-fn"; + DataSource lambdaDataSource = dataSource("accountFunction", DataSourceType.AWS_LAMBDA); + lambdaDataSource.setLambdaConfig(Map.of("lambdaFunctionArn", functionArn)); + when(appSync.getDataSource(API_ID, "accountFunction")).thenReturn(lambdaDataSource); + LambdaService lambdaService = mock(LambdaService.class); + when(lambdaService.invokeArn(eq(functionArn), any(byte[].class), eq(InvocationType.RequestResponse))) + .thenReturn(new InvokeResult(200, null, "{\"id\":\"42\"}".getBytes(StandardCharsets.UTF_8), + null, "request-id")); + AppSyncResolverExecutor lambdaExecutor = new AppSyncResolverExecutor(appSync, jsRuntime, + new AppSyncDataSourceInvokers(List.of(new LambdaDataSourceInvoker(lambdaService, objectMapper))), + vtlEngine(), objectMapper); + Resolver resolver = resolver(ResolverKind.UNIT, "unit-code"); + resolver.setDataSourceName("accountFunction"); + Map request = Map.of("operation", "Invoke", "payload", Map.of("id", "42")); + jsRuntime.script("unit-code", "request", (handler, context) -> ok(request)); + jsRuntime.script("unit-code", "response", (handler, context) -> ok(context.get("result"))); + + ResolverOutcome result = lambdaExecutor.execute(resolver, invocation(Map.of("id", "42"))); + + assertTrue(result.errors().isEmpty()); + assertEquals(Map.of("id", "42"), result.data()); + ArgumentCaptor payload = ArgumentCaptor.forClass(byte[].class); + verify(lambdaService).invokeArn(eq(functionArn), payload.capture(), eq(InvocationType.RequestResponse)); + assertEquals(request.get("payload"), objectMapper.readValue(payload.getValue(), Map.class)); + } + + @Test + void vtlLambdaResolverSendsOnlyPayloadToTheFunction() throws Exception { String functionArn = "arn:aws:lambda:eu-west-1:000000000000:function:resolver-fn"; DataSource lambdaDataSource = dataSource("accountFunction", DataSourceType.AWS_LAMBDA); lambdaDataSource.setLambdaConfig(Map.of("lambdaFunctionArn", functionArn)); @@ -645,17 +673,20 @@ class AppSyncResolverExecutorTest { vtlEngine(), objectMapper); Resolver resolver = resolver(ResolverKind.UNIT, null); resolver.setDataSourceName("accountFunction"); - resolver.setRequestMappingTemplate("{\"version\":\"2018-05-29\",\"operation\":\"Invoke\"}"); + resolver.setRequestMappingTemplate(""" + {"version":"2018-05-29","operation":"Invoke", + "payload":{"field":"getPost","arguments":{"id":$util.toJson($ctx.args.id)}}} + """); resolver.setResponseMappingTemplate("$util.toJson($ctx.result)"); - ResolverOutcome result = lambdaExecutor.execute(resolver, invocation(Map.of())); + ResolverOutcome result = lambdaExecutor.execute(resolver, invocation(Map.of("id", "42"))); assertTrue(result.errors().isEmpty()); assertEquals(Map.of(), result.data()); ArgumentCaptor payload = ArgumentCaptor.forClass(byte[].class); verify(lambdaService).invokeArn(eq(functionArn), payload.capture(), eq(InvocationType.RequestResponse)); - assertEquals(Map.of("version", "2018-05-29", "operation", "Invoke"), + assertEquals(Map.of("field", "getPost", "arguments", Map.of("id", "42")), objectMapper.readValue(payload.getValue(), Map.class)); }