mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+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