mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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>
This commit is contained in:
1 parent
dad5648b7b
commit
11b762d08b
4 files changed
+66
-20
No files matched your search
@@ -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. |
|
||||
|
||||
+2
-2
@@ -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) {
|
||||
|
||||
+25
-9
@@ -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<String, Object> 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);
|
||||
}
|
||||
|
||||
|
||||
+35
-4
@@ -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<String, Object> 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<byte[]> 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<byte[]> 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));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user