mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
feat(appsync): Phase 4 — VTL Engine (#1288)
* feat(appsync): add VTL engine core infrastructure Add ReturnSignal exception for #return directive, AppSyncVtlResult record for evaluation results, and ReturnDirective custom Velocity directive. * feat(appsync): wire AppSyncUtil to VTL context Add errorList and authTypeValue fields with setters. Change authType() to return configured value. Change appendError() overloads to accumulate errors in internal list. Add toJson() exclusion for selectionSetList and selectionSetGraphQL. Add base64Encode(String) overload. * feat(appsync): add VTL engine context and evaluator AppSyncVtlContext builds the map with all aliases (, , , , , , ). AppSyncVtlEngine evaluates VTL templates using Apache Velocity with AppSync-specific context variables, custom #return directive, and VtlErrorSignal/ReturnSignal handling. * chore(appsync): add GraalVM reflection config for VTL engine classes Register AppSyncVtlEngine, AppSyncVtlContext, ReturnDirective, and all Phase 3 util classes for native image reflection. * test(appsync): add VTL engine unit tests 89 tests across categories A-J covering context variables, util methods, error handling, appendError accumulation, #return directive, authType, velocity directives, pipeline simulation, and edge cases. Validates AWS AppSync behavior: null references render as literal text, #return clears buffer, .error() clears output, var silent references. * fix(appsync): set $context.prev to null for unit resolvers AWS AppSync does not initialize $context.prev in unit (non-pipeline) resolvers. It is null, not an empty map. Accessing $ctx.prev in a unit resolver renders the literal text (e.g. "$ctx.prev") in non-strict Velocity mode, matching AWS behavior. * fix(appsync): accept Object in $util.qr() and $util.quiet() AWS AppSync defines qr/quiet as accepting any Object, not just String. The previous String signature caused method resolution failures in Velocity when non-String values (Integer, Boolean, List, Map, or method return values like Map.put) were passed. Changed to Object to match AWS. * fix(appsync): remove dead exception handling in VTL engine Velocity wraps exceptions thrown by method calls inside a generic Exception, but exceptions thrown by custom directives (ReturnSignal) propagate directly. The catch(VtlErrorSignal) and ReturnSignal unwrap branches were dead code. Simplified to only unwrap VtlErrorSignal from wrapped method exceptions. * test(appsync): update and add tests for AWS parity and full coverage Updates: - j11: $context.prev default is now null (not empty map) - j11b/j11c: undefined prev renders as literal text - qr/quiet: expanded with Integer, Boolean, List, Map arguments New tests for 100% coverage: - c21: $util.transform accessible from VTL - e6: appendError with errorInfo (4-arg overload) - getTransform_returnsInstance: getter coverage - escapeJavaScript_controlCharDefault: control char <0x20 branch - toJson_mapWithFieldNameOnlyDoesNotFilter: fieldName-only branch - toJson_serializationFailureReturnsNull: JsonProcessingException catch - appendError_withErrorInfo: errorInfo non-null branch
This commit is contained in:
1 parent
aeb11d77c3
commit
113c7d7e7d
9 files changed
+1209
-12
No files matched your search
+146
@@ -0,0 +1,146 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.util.AppSyncUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AppSyncVtlContext {
|
||||
|
||||
private final Map<String, Object> contextMap;
|
||||
private final AppSyncUtil util;
|
||||
private final List<Map<String, Object>> appendedErrors;
|
||||
|
||||
public AppSyncVtlContext(
|
||||
Map<String, Object> arguments,
|
||||
Map<String, Object> source,
|
||||
Map<String, Object> identity,
|
||||
Map<String, Object> request,
|
||||
Map<String, Object> info,
|
||||
Map<String, Object> stash,
|
||||
Map<String, Object> prev,
|
||||
Object result,
|
||||
String authType,
|
||||
ObjectMapper objectMapper
|
||||
) {
|
||||
this.contextMap = buildContextMap(arguments, source, identity, request, info, stash, prev, result);
|
||||
this.appendedErrors = new ArrayList<>();
|
||||
this.util = new AppSyncUtil(objectMapper);
|
||||
this.util.setErrorList(this.appendedErrors);
|
||||
this.util.setAuthTypeValue(authType);
|
||||
}
|
||||
|
||||
private AppSyncVtlContext(Builder builder) {
|
||||
this.contextMap = buildContextMap(
|
||||
builder.arguments, builder.source, builder.identity, builder.request,
|
||||
builder.info, builder.stash, builder.prev, builder.result);
|
||||
this.appendedErrors = new ArrayList<>();
|
||||
this.util = new AppSyncUtil(builder.objectMapper);
|
||||
this.util.setErrorList(this.appendedErrors);
|
||||
this.util.setAuthTypeValue(builder.authType);
|
||||
}
|
||||
|
||||
private static Map<String, Object> buildContextMap(
|
||||
Map<String, Object> arguments,
|
||||
Map<String, Object> source,
|
||||
Map<String, Object> identity,
|
||||
Map<String, Object> request,
|
||||
Map<String, Object> info,
|
||||
Map<String, Object> stash,
|
||||
Map<String, Object> prev,
|
||||
Object result) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("arguments", arguments != null ? arguments : Map.of());
|
||||
map.put("source", source != null ? source : Map.of());
|
||||
map.put("result", result);
|
||||
map.put("identity", identity != null ? identity : Map.of());
|
||||
map.put("request", request != null ? request : Map.of("headers", Map.of()));
|
||||
map.put("stash", stash != null ? stash : new HashMap<>());
|
||||
map.put("prev", prev);
|
||||
map.put("info", info != null ? info : Map.of());
|
||||
map.put("error", null);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Builder builder(ObjectMapper objectMapper) {
|
||||
return new Builder(objectMapper);
|
||||
}
|
||||
|
||||
public Map<String, Object> getContextMap() {
|
||||
return contextMap;
|
||||
}
|
||||
|
||||
public AppSyncUtil getUtil() {
|
||||
return util;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getAppendedErrors() {
|
||||
return appendedErrors;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private final ObjectMapper objectMapper;
|
||||
private Map<String, Object> arguments;
|
||||
private Map<String, Object> source;
|
||||
private Map<String, Object> identity;
|
||||
private Map<String, Object> request;
|
||||
private Map<String, Object> info;
|
||||
private Map<String, Object> stash;
|
||||
private Map<String, Object> prev;
|
||||
private Object result;
|
||||
private String authType;
|
||||
|
||||
Builder(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Builder arguments(Map<String, Object> arguments) {
|
||||
this.arguments = arguments;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder source(Map<String, Object> source) {
|
||||
this.source = source;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder identity(Map<String, Object> identity) {
|
||||
this.identity = identity;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder request(Map<String, Object> request) {
|
||||
this.request = request;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder info(Map<String, Object> info) {
|
||||
this.info = info;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder stash(Map<String, Object> stash) {
|
||||
this.stash = stash;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder prev(Map<String, Object> prev) {
|
||||
this.prev = prev;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder result(Object result) {
|
||||
this.result = result;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder authType(String authType) {
|
||||
this.authType = authType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AppSyncVtlContext build() {
|
||||
return new AppSyncVtlContext(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql;
|
||||
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.util.AppSyncUtil;
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.util.VtlErrorSignal;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.runtime.RuntimeConstants;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AppSyncVtlEngine {
|
||||
|
||||
private final VelocityEngine engine;
|
||||
|
||||
@Inject
|
||||
public AppSyncVtlEngine() {
|
||||
this.engine = new VelocityEngine();
|
||||
engine.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
|
||||
engine.setProperty(RuntimeConstants.RUNTIME_LOG_NAME,
|
||||
"io.github.hectorvent.floci.appsync.vtl");
|
||||
engine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "string");
|
||||
engine.setProperty("resource.loader.string.class",
|
||||
"org.apache.velocity.runtime.resource.loader.StringResourceLoader");
|
||||
engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
|
||||
engine.setProperty("userdirective",
|
||||
"io.github.hectorvent.floci.services.appsync.graphql.ReturnDirective");
|
||||
engine.init();
|
||||
}
|
||||
|
||||
public AppSyncVtlResult evaluate(String template, AppSyncVtlContext ctx) {
|
||||
if (template == null || template.isEmpty()) {
|
||||
return new AppSyncVtlResult("", null, List.of());
|
||||
}
|
||||
|
||||
VelocityContext vc = new VelocityContext();
|
||||
Map<String, Object> contextMap = ctx.getContextMap();
|
||||
|
||||
vc.put("context", contextMap);
|
||||
vc.put("ctx", contextMap);
|
||||
vc.put("args", contextMap.get("arguments"));
|
||||
vc.put("source", contextMap.get("source"));
|
||||
vc.put("stash", contextMap.get("stash"));
|
||||
vc.put("prev", contextMap.get("prev"));
|
||||
|
||||
AppSyncUtil util = ctx.getUtil();
|
||||
vc.put("util", util);
|
||||
vc.put("utils", util);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
engine.evaluate(vc, writer, "appsync-template", template);
|
||||
} catch (ReturnSignal signal) {
|
||||
return new AppSyncVtlResult(signal.getValue(), null, ctx.getAppendedErrors());
|
||||
} catch (Exception e) {
|
||||
Throwable cause = e;
|
||||
while (cause != null) {
|
||||
if (cause instanceof VtlErrorSignal signal) {
|
||||
return new AppSyncVtlResult("", signal, ctx.getAppendedErrors());
|
||||
}
|
||||
cause = cause.getCause();
|
||||
}
|
||||
throw new RuntimeException("VTL evaluation failed", e);
|
||||
}
|
||||
|
||||
return new AppSyncVtlResult(writer.toString(), null, ctx.getAppendedErrors());
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql;
|
||||
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.util.VtlErrorSignal;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record AppSyncVtlResult(
|
||||
Object output,
|
||||
VtlErrorSignal error,
|
||||
List<Map<String, Object>> appendedErrors
|
||||
) {
|
||||
public boolean hasError() {
|
||||
return error != null;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql;
|
||||
|
||||
import org.apache.velocity.context.InternalContextAdapter;
|
||||
import org.apache.velocity.runtime.directive.Directive;
|
||||
import org.apache.velocity.runtime.parser.node.Node;
|
||||
|
||||
import java.io.Writer;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReturnDirective extends Directive {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "return";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return LINE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean render(InternalContextAdapter context, Writer writer, Node node)
|
||||
throws IOException {
|
||||
Object value = null;
|
||||
if (node.jjtGetNumChildren() > 0) {
|
||||
value = node.jjtGetChild(0).value(context);
|
||||
}
|
||||
throw new ReturnSignal(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql;
|
||||
|
||||
public class ReturnSignal extends RuntimeException {
|
||||
private final Object value;
|
||||
|
||||
public ReturnSignal(Object value) {
|
||||
super("return");
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
+41
-10
@@ -19,6 +19,8 @@ public class AppSyncUtil {
|
||||
private final TransformUtil transformUtil;
|
||||
private final ListUtil listUtil;
|
||||
private final MapUtil mapUtil;
|
||||
private List<Map<String, Object>> errorList;
|
||||
private String authTypeValue;
|
||||
|
||||
public AppSyncUtil(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
@@ -31,6 +33,14 @@ public class AppSyncUtil {
|
||||
this.mapUtil = new MapUtil();
|
||||
}
|
||||
|
||||
public void setErrorList(List<Map<String, Object>> errorList) {
|
||||
this.errorList = errorList;
|
||||
}
|
||||
|
||||
public void setAuthTypeValue(String authTypeValue) {
|
||||
this.authTypeValue = authTypeValue;
|
||||
}
|
||||
|
||||
public StrUtil getStr() { return strUtil; }
|
||||
public TimeUtil getTime() { return timeUtil; }
|
||||
public MathUtil getMath() { return mathUtil; }
|
||||
@@ -97,6 +107,17 @@ public class AppSyncUtil {
|
||||
|
||||
public String toJson(Object o) {
|
||||
try {
|
||||
if (o instanceof Map<?, ?> map && map.containsKey("fieldName")
|
||||
&& map.containsKey("parentTypeName")) {
|
||||
Map<String, Object> filtered = new LinkedHashMap<>();
|
||||
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||
String key = entry.getKey().toString();
|
||||
if (!key.equals("selectionSetList") && !key.equals("selectionSetGraphQL")) {
|
||||
filtered.put(key, entry.getValue());
|
||||
}
|
||||
}
|
||||
return objectMapper.writeValueAsString(filtered);
|
||||
}
|
||||
return objectMapper.writeValueAsString(o);
|
||||
} catch (JsonProcessingException e) {
|
||||
return "null";
|
||||
@@ -107,12 +128,12 @@ public class AppSyncUtil {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public String quiet(String s) {
|
||||
public String quiet(Object o) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String qr(String s) {
|
||||
return quiet(s);
|
||||
public String qr(Object o) {
|
||||
return quiet(o);
|
||||
}
|
||||
|
||||
public boolean matches(String pattern, String value) {
|
||||
@@ -177,7 +198,7 @@ public class AppSyncUtil {
|
||||
}
|
||||
|
||||
public String authType() {
|
||||
return null;
|
||||
return authTypeValue;
|
||||
}
|
||||
|
||||
public void error(String message) {
|
||||
@@ -197,20 +218,30 @@ public class AppSyncUtil {
|
||||
}
|
||||
|
||||
public void appendError(String message) {
|
||||
// In VTL context, this adds to $context.error without halting
|
||||
// For standalone usage, this is a no-op
|
||||
appendErrorInternal(message, "Unknown", null, null);
|
||||
}
|
||||
|
||||
public void appendError(String message, String errorType) {
|
||||
// No-op for standalone usage
|
||||
appendErrorInternal(message, errorType, null, null);
|
||||
}
|
||||
|
||||
public void appendError(String message, String errorType, Object data) {
|
||||
// No-op for standalone usage
|
||||
appendErrorInternal(message, errorType, data, null);
|
||||
}
|
||||
|
||||
public void appendError(String message, String errorType, Object data, Object errorInfo) {
|
||||
// No-op for standalone usage
|
||||
appendErrorInternal(message, errorType, data, errorInfo);
|
||||
}
|
||||
|
||||
private void appendErrorInternal(String message, String errorType, Object data, Object errorInfo) {
|
||||
if (errorList != null) {
|
||||
Map<String, Object> error = new LinkedHashMap<>();
|
||||
error.put("message", message);
|
||||
error.put("type", errorType);
|
||||
if (data != null) error.put("data", data);
|
||||
if (errorInfo != null) error.put("errorInfo", errorInfo);
|
||||
errorList.add(error);
|
||||
}
|
||||
}
|
||||
|
||||
public void unauthorized() {
|
||||
@@ -218,7 +249,7 @@ public class AppSyncUtil {
|
||||
}
|
||||
|
||||
public void validate(boolean condition, String message) {
|
||||
validate(condition, message, "Unknown");
|
||||
validate(condition, message, "CustomTemplateException");
|
||||
}
|
||||
|
||||
public void validate(boolean condition, String message, String errorType) {
|
||||
|
||||
Reference in new issue
Block a user