feat(appsync): execute VTL pipeline resolvers on sidecar (#4586)

* feat(appsync): execute VTL pipeline functions

* fix(appsync): validate VTL functions before early return
This commit is contained in:
XuJian authored and GitHub committed 2026-09-29 11:36:27 -04:00
1 parent 1465251e0c
commit 95cbe7de34
4 files changed
+344 -62

No files matched your search

@@ -39,8 +39,10 @@ class AppSyncJsResolverDockerIntegrationTest {
private static final String SCHEMA = """
type Message { id: ID! name: String }
type VtlPipelineResult { value: String before: String }
type Query {
getMessages(orgNo: String!): [Message]
vtlPipeline(value: String!): VtlPipelineResult
ping(x: String): String
echoArg(x: String): String
failing: String
@@ -207,6 +209,51 @@ class AppSyncJsResolverDockerIntegrationTest {
.body("data.getMessages.id", contains("1", "2"));
}
@Test
void aVtlPipelineRunsThroughTheGraphqlSidecar() {
String functionId = given()
.header("Authorization", AUTH)
.contentType("application/json")
.body("""
{"name":"Query_vtlPipeline_0","dataSourceName":"local",
"functionVersion":"2018-05-29",
"requestMappingTemplate":"%s",
"responseMappingTemplate":"%s"}
""".formatted(escape("""
{"version":"2018-05-29","payload":{
"value":$util.toJson($ctx.stash.value),
"before":$util.toJson($ctx.prev.result.before)}}
"""), escape("$util.toJson($ctx.result)")))
.when()
.post("/v1/apis/" + apiId + "/functions")
.then()
.statusCode(200)
.extract().path("functionConfiguration.functionId");
given()
.header("Authorization", AUTH)
.contentType("application/json")
.body("""
{"typeName":"Query","fieldName":"vtlPipeline","kind":"PIPELINE",
"pipelineConfig":{"functions":["%s"]},
"requestMappingTemplate":"%s",
"responseMappingTemplate":"%s"}
""".formatted(functionId, escape("""
#set($discard = $ctx.stash.put("value", $ctx.args.value))
{"before":$util.toJson($ctx.args.value)}
"""), escape("$util.toJson($ctx.prev.result)")))
.when()
.post("/v1/apis/" + apiId + "/types/Query/resolvers")
.then()
.statusCode(200);
query("{ vtlPipeline(value: \\\"live\\\") { value before } }")
.statusCode(200)
.body("errors", nullValue())
.body("data.vtlPipeline.value", equalTo("live"))
.body("data.vtlPipeline.before", equalTo("live"));
}
@Test
void aUnitResolverRunsRequestAndResponseAroundItsDataSource() {
query("{ ping }")
@@ -40,6 +40,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -897,25 +898,222 @@ class AppSyncResolverExecutorTest {
}
@Test
void aLaterVtlFunctionIsRejectedBeforeAnyPipelineStageRuns() {
when(appSync.getFunction(API_ID, "fn1"))
.thenReturn(function("fn1", "one", "accountDB", "fn1-code"));
when(appSync.getFunction(API_ID, "fn2")).thenReturn(vtlFunction("fn2"));
void vtlPipelineThreadsStashAndPreviousResultThroughItsFunctionAndAfterTemplate() {
when(appSync.getDataSource(API_ID, "accountTable"))
.thenReturn(dataSource("accountTable", DataSourceType.AMAZON_DYNAMODB));
when(appSync.getFunction(API_ID, "fn1")).thenReturn(vtlFunction("fn1", "accountTable", """
{"version":"2018-05-29","operation":"GetItem",
"key":{"id":{"S":$util.toJson($ctx.stash.id)}}}
""", """
#set($discard = $ctx.stash.put("function", "seen"))
{"id":$util.toJson($ctx.result.id),
"before":$util.toJson($ctx.prev.result.before)}
"""));
dynamoDbInvoker.answer = Map.of("id", "42");
Resolver resolver = resolver(ResolverKind.PIPELINE, null);
resolver.setRequestMappingTemplate("""
#set($discard = $ctx.stash.put("id", $ctx.args.id))
{"before":$util.toJson($ctx.args.id)}
""");
resolver.setResponseMappingTemplate("""
{"last":$util.toJson($ctx.prev.result),
"stash":$util.toJson($ctx.stash.function)}
""");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1")));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of("id", "42")));
assertTrue(result.errors().isEmpty());
assertEquals(Map.of("last", Map.of("id", "42", "before", "42"),
"stash", "seen"), result.data());
assertEquals(1, dynamoDbInvoker.requests.size());
Map<?, ?> request = (Map<?, ?>) dynamoDbInvoker.requests.get(0);
assertEquals("2018-05-29", request.get("version"));
assertEquals("GetItem", request.get("operation"));
}
@Test
void vtlPipelineLambdaFunctionSendsOnlyPayloadToTheFunction() 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);
when(appSync.getFunction(API_ID, "fn1")).thenReturn(vtlFunction("fn1", "accountFunction", """
{"version":"2018-05-29","operation":"Invoke",
"payload":{"id":$util.toJson($ctx.args.id)}}
""", "$util.toJson($ctx.result)"));
LambdaService lambdaService = mock(LambdaService.class);
when(lambdaService.invokeArn(eq(functionArn), any(byte[].class),
eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult(200, null, "{}".getBytes(StandardCharsets.UTF_8),
null, "request-id"));
AppSyncResolverExecutor lambdaExecutor = new AppSyncResolverExecutor(appSync, jsRuntime,
new AppSyncDataSourceInvokers(List.of(
new LambdaDataSourceInvoker(lambdaService, objectMapper)),
mock(AppSyncDataSourceAuthorizer.class)),
vtlEngine(), objectMapper);
Resolver resolver = resolver(ResolverKind.PIPELINE, null);
resolver.setRequestMappingTemplate("{}");
resolver.setResponseMappingTemplate("$util.toJson($ctx.result)");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1")));
ResolverOutcome result = lambdaExecutor.execute(resolver, invocation(Map.of("id", "42")));
assertTrue(result.errors().isEmpty());
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(lambdaService).invokeArn(eq(functionArn), payload.capture(),
eq(InvocationType.RequestResponse));
assertEquals(Map.of("id", "42"), objectMapper.readValue(payload.getValue(), Map.class));
}
@Test
void vtlFunctionRequestReturnSkipsItsDataSourceAndContinuesThePipeline() {
when(appSync.getDataSource(API_ID, "local"))
.thenThrow(new AwsException("NotFoundException", "Data source not found", 404));
when(appSync.getDataSource(API_ID, "accountDB")).thenReturn(dataSource("accountDB"));
when(appSync.getFunction(API_ID, "fn1")).thenReturn(vtlFunction("fn1", "local",
"#return({\"cached\":true})", "$util.toJson($ctx.result)"));
when(appSync.getFunction(API_ID, "fn2"))
.thenReturn(function("fn2", "two", "accountDB", "fn2-code"));
Resolver resolver = resolver(ResolverKind.PIPELINE, "pipeline-code");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1", "fn2")));
jsRuntime.script("pipeline-code", "request", (h, ctx) -> ok(null));
jsRuntime.script("fn1-code", "request", (h, ctx) -> ok(Map.of()));
jsRuntime.script("pipeline-code", "request", (h, ctx) -> ok(Map.of()));
jsRuntime.script("fn2-code", "request", (h, ctx) -> {
assertEquals(Map.of("cached", true), ((Map<?, ?>) ctx.get("prev")).get("result"));
return ok(Map.of("statement", "select 1"));
});
jsRuntime.script("fn2-code", "response", (h, ctx) -> ok("continued"));
jsRuntime.script("pipeline-code", "response", (h, ctx) -> ok(ctx.get("result")));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals("continued", result.data());
assertTrue(result.errors().isEmpty());
assertTrue(noneInvoker.requests.isEmpty());
verify(appSync, never()).getDataSource(API_ID, "local");
assertEquals(List.of("pipeline-code#request", "fn2-code#request",
"fn2-code#response", "pipeline-code#response"), jsRuntime.calls);
}
@Test
void vtlFunctionRequestReturnCannotBypassUnsupportedFunctionVersion() {
FunctionConfiguration function = vtlFunction("fn1", "local",
"#return({\"cached\":true})", "$util.toJson($ctx.result)");
function.setFunctionVersion("2017-02-28");
when(appSync.getFunction(API_ID, "fn1")).thenReturn(function);
Resolver resolver = resolver(ResolverKind.PIPELINE, "pipeline-code");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1")));
jsRuntime.script("pipeline-code", "request", (h, ctx) -> ok(Map.of()));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals(1, result.errors().size());
assertTrue(result.errors().get(0).message().contains("VTL"),
result.errors().get(0).message());
assertEquals("UnsupportedOperation", result.errors().get(0).errorType());
assertEquals(List.of("pipeline-code#request"), jsRuntime.calls);
assertTrue(result.errors().get(0).message().contains("2017-02-28"));
verify(appSync, never()).getDataSource(API_ID, "local");
assertTrue(noneInvoker.requests.isEmpty());
}
@Test
void vtlFunctionResponseReturnBecomesTheNextFunctionsPreviousResult() {
when(appSync.getDataSource(API_ID, "local"))
.thenReturn(dataSource("local", DataSourceType.NONE));
when(appSync.getFunction(API_ID, "fn1")).thenReturn(vtlFunction("fn1", "local",
"{\"version\":\"2018-05-29\",\"payload\":{\"id\":\"1\"}}",
"#return({\"from\":\"first\"})"));
when(appSync.getFunction(API_ID, "fn2")).thenReturn(vtlFunction("fn2", "local", """
{"version":"2018-05-29",
"payload":{"previous":$util.toJson($ctx.prev.result.from)}}
""", "$util.toJson($ctx.result)"));
Resolver resolver = resolver(ResolverKind.PIPELINE, null);
resolver.setRequestMappingTemplate("{}");
resolver.setResponseMappingTemplate("$util.toJson($ctx.result)");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1", "fn2")));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals(Map.of("previous", "first"), result.data());
assertTrue(result.errors().isEmpty());
assertEquals(2, noneInvoker.requests.size());
}
@Test
void vtlFunctionCanHandleADataSourceErrorAndContinueToTheAfterTemplate() {
when(appSync.getDataSource(API_ID, "accountFunction"))
.thenReturn(dataSource("accountFunction", DataSourceType.AWS_LAMBDA));
when(appSync.getFunction(API_ID, "fn1")).thenReturn(vtlFunction("fn1", "accountFunction",
"{\"version\":\"2018-05-29\",\"operation\":\"Invoke\",\"payload\":{}}", """
#if($ctx.error)
{"handled":$util.toJson($ctx.error.type)}
#else
$util.toJson($ctx.result)
#end
"""));
lambdaInvoker.failure = new AwsException("LambdaExecutionException", "failed", 400);
Resolver resolver = resolver(ResolverKind.PIPELINE, null);
resolver.setRequestMappingTemplate("{}");
resolver.setResponseMappingTemplate("$util.toJson($ctx.result)");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1")));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals(Map.of("handled", "LambdaExecutionException"), result.data());
assertTrue(result.errors().isEmpty());
lambdaInvoker.failure = null;
}
@Test
void vtlResolverBeforeReturnStopsTheWholePipelineBeforeFunctionLookup() {
Resolver resolver = resolver(ResolverKind.PIPELINE, null);
resolver.setRequestMappingTemplate("#return({\"cached\":true})");
resolver.setResponseMappingTemplate("$util.toJson($ctx.result)");
resolver.setPipelineConfig(Map.of("functions", List.of("missing")));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals(Map.of("cached", true), result.data());
assertTrue(result.errors().isEmpty());
assertTrue(invoker.requests.isEmpty());
}
@Test
void unsupportedVtlFunctionVersionFailsBeforeDataSourceInvocation() {
when(appSync.getDataSource(API_ID, "local"))
.thenReturn(dataSource("local", DataSourceType.NONE));
FunctionConfiguration function = vtlFunction("fn1", "local",
"{\"payload\":{}}", "$util.toJson($ctx.result)");
function.setFunctionVersion("2017-02-28");
when(appSync.getFunction(API_ID, "fn1")).thenReturn(function);
Resolver resolver = resolver(ResolverKind.PIPELINE, "pipeline-code");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1")));
jsRuntime.script("pipeline-code", "request", (h, ctx) -> ok(Map.of()));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals(1, result.errors().size());
assertEquals("UnsupportedOperation", result.errors().get(0).errorType());
assertTrue(result.errors().get(0).message().contains("2017-02-28"));
assertTrue(noneInvoker.requests.isEmpty());
}
@Test
void vtlFunctionRequestWithoutVersionFailsBeforeDataSourceInvocation() {
when(appSync.getDataSource(API_ID, "local"))
.thenReturn(dataSource("local", DataSourceType.NONE));
when(appSync.getFunction(API_ID, "fn1")).thenReturn(vtlFunction("fn1", "local",
"{\"payload\":{}}", "$util.toJson($ctx.result)"));
Resolver resolver = resolver(ResolverKind.PIPELINE, null);
resolver.setRequestMappingTemplate("{}");
resolver.setResponseMappingTemplate("$util.toJson($ctx.result)");
resolver.setPipelineConfig(Map.of("functions", List.of("fn1")));
ResolverOutcome result = executor.execute(resolver, invocation(Map.of()));
assertEquals(1, result.errors().size());
assertEquals("MappingTemplate", result.errors().get(0).errorType());
assertTrue(noneInvoker.requests.isEmpty());
}
@Test
void anExplicitVtlRuntimeIsRefusedEvenWithCode() {
when(appSync.getDataSource(API_ID, "accountDB")).thenReturn(dataSource("accountDB"));
@@ -966,13 +1164,15 @@ class AppSyncResolverExecutorTest {
result.errors().get(0).errorType());
}
private FunctionConfiguration vtlFunction(String id) {
private FunctionConfiguration vtlFunction(String id, String dataSourceName,
String requestTemplate, String responseTemplate) {
FunctionConfiguration fn = new FunctionConfiguration();
fn.setFunctionId(id);
fn.setName("vtl-" + id);
fn.setDataSourceName("accountDB");
fn.setRequestMappingTemplate("{\"version\":\"2018-05-29\"}");
fn.setResponseMappingTemplate("$util.toJson($ctx.result)");
fn.setDataSourceName(dataSourceName);
fn.setFunctionVersion("2018-05-29");
fn.setRequestMappingTemplate(requestTemplate);
fn.setResponseMappingTemplate(responseTemplate);
return fn;
}