use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processReference.
private void processReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
if (!(source instanceof Reference))
throw new EGraphQLException("Not done yet");
if (services == null)
throw new EGraphQLException("Resource Referencing services not provided");
Reference ref = (Reference) source;
ReferenceResolution res = services.lookup(appInfo, context, ref);
if (res != null) {
if (targetTypeOk(field.getArguments(), res.getTarget())) {
Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList));
ObjectValue obj = new ObjectValue();
arg.addValue(obj);
processObject((Resource) res.getTargetContext(), (Base) res.getTarget(), obj, field.getSelectionSet(), inheritedList, suffix);
}
} else if (!hasArgument(field.getArguments(), "optional", "true"))
throw new EGraphQLException("Unable to resolve reference to " + ref.getReference());
}
use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processSearchSingle.
private void processSearchSingle(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
if (services == null)
throw new EGraphQLException("Resource Referencing services not provided");
String id = "";
for (Argument arg : field.getArguments()) if ((arg.getName().equals("id")))
id = getSingleValue(arg);
else
throw new EGraphQLException("Unknown/invalid parameter " + arg.getName());
if (Utilities.noString(id))
throw new EGraphQLException("No id found");
Resource res = (Resource) services.lookup(appInfo, field.getName(), id);
if (res == null)
throw new EGraphQLException("Resource " + field.getName() + "/" + id + " not found");
Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
ObjectValue obj = new ObjectValue();
arg.addValue(obj);
processObject(res, res, obj, field.getSelectionSet(), inheritedList, suffix);
}
use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method filterResources.
private List<Resource> filterResources(Argument fhirpath, List<IBaseResource> list) throws EGraphQLException, FHIRException {
List<Resource> result = new ArrayList<>();
if (list.size() > 0) {
if ((fhirpath == null))
for (IBaseResource v : list) result.add((Resource) v);
else {
FHIRPathEngine fpe = new FHIRPathEngine(context);
ExpressionNode node = fpe.parse(getSingleValue(fhirpath));
for (IBaseResource v : list) if (fpe.evaluateToBoolean(null, (Resource) v, (Resource) v, node))
result.add((Resource) v);
}
}
return result;
}
use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processSearchSimple.
private void processSearchSimple(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
if (services == null)
throw new EGraphQLException("Resource Referencing services not provided");
List<IBaseResource> list = new ArrayList<>();
services.listResources(appInfo, field.getName().substring(0, field.getName().length() - 4), field.getArguments(), list);
Argument arg = null;
ObjectValue obj = null;
List<Resource> vl = filterResources(field.argument("fhirpath"), list);
if (!vl.isEmpty()) {
arg = target.addField(field.getAlias() + suffix, listStatus(field, true));
for (Resource v : vl) {
obj = new ObjectValue();
arg.addValue(obj);
processObject(v, v, obj, field.getSelectionSet(), inheritedList, suffix);
}
}
}
use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngineTests method testResource.
private void testResource(Resource resource, String output, String source) throws IOException, EGraphEngine, EGraphQLException {
GraphQLEngine gql = new GraphQLEngine(TestingUtilities.context());
gql.setServices(this);
if (resource != null) {
gql.setFocus(resource);
}
gql.setGraphQL(Parser.parseFile(TestingUtilities.resourceNameToFile("graphql", source)));
gql.getGraphQL().setOperationName(null);
gql.getGraphQL().getVariables().add(new Argument("var", new NameValue("true")));
boolean ok = false;
String msg = null;
try {
gql.execute();
ok = true;
} catch (Exception e) {
if (!output.equals("$error"))
e.printStackTrace();
ok = false;
msg = e.getMessage();
}
if (ok) {
Assertions.assertTrue(!output.equals("$error"), "Expected to fail, but didn't");
StringBuilder str = new StringBuilder();
gql.getOutput().setWriteWrapper(false);
gql.getOutput().write(str, 0);
TextFile.stringToFile(str.toString(), TestingUtilities.resourceNameToFile("graphql", output + ".out"));
msg = TestingUtilities.checkJsonIsSame(TestingUtilities.resourceNameToFile("graphql", output + ".out"), TestingUtilities.resourceNameToFile("graphql", output));
Assertions.assertTrue(Utilities.noString(msg), msg);
} else
Assertions.assertTrue(output.equals("$error"), "Error, but proper output was expected (" + msg + ")");
}
Aggregations