Search in sources :

Example 26 with EGraphQLException

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, Bundle bnd) throws EGraphQLException, FHIRException {
    List<Resource> result = new ArrayList<Resource>();
    if (bnd.getEntry().size() > 0) {
        if ((fhirpath == null))
            for (BundleEntryComponent be : bnd.getEntry()) result.add(be.getResource());
        else {
            FHIRPathEngine fpe = new FHIRPathEngine(context);
            ExpressionNode node = fpe.parse(getSingleValue(fhirpath));
            for (BundleEntryComponent be : bnd.getEntry()) if (fpe.evaluateToBoolean(null, be.getResource(), be.getResource(), node))
                result.add(be.getResource());
        }
    }
    return result;
}
Also used : BundleEntryComponent(org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) Resource(org.hl7.fhir.r4b.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) DomainResource(org.hl7.fhir.r4b.model.DomainResource) ArrayList(java.util.ArrayList)

Example 27 with EGraphQLException

use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processCanonicalReference.

private void processCanonicalReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (!(source instanceof CanonicalType))
        throw new EGraphQLException("Not done yet");
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    Reference ref = new Reference(source.primitiveValue());
    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());
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) Reference(org.hl7.fhir.r4b.model.Reference) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) CanonicalType(org.hl7.fhir.r4b.model.CanonicalType) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 28 with EGraphQLException

use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.

the class GraphQLParserTests method test.

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("data")
public void test(String name, String test) throws IOException, EGraphQLException, EGraphEngine {
    Package doc = Parser.parse(test);
    Assertions.assertNotNull(doc);
}
Also used : Package(org.hl7.fhir.utilities.graphql.Package) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 29 with EGraphQLException

use of org.hl7.fhir.utilities.graphql.EGraphQLException in project org.hl7.fhir.core by hapifhir.

the class GraphQLParserTests method test.

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("data")
public void test(String name, String test) throws IOException, EGraphQLException, EGraphEngine {
    Package doc = Parser.parse(test);
    Assertions.assertNotNull(doc);
}
Also used : Package(org.hl7.fhir.utilities.graphql.Package) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 30 with EGraphQLException

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, String operation) throws IOException, EGraphEngine, EGraphQLException {
    GraphQLEngine gql = new GraphQLEngine(TestingUtilities.getSharedWorkerContext());
    gql.setServices(this);
    if (resource != null)
        gql.setFocus(resource);
    gql.setGraphQL(Parser.parse(TestingUtilities.loadTestResource("r5", "graphql", source)));
    gql.getGraphQL().setOperationName(operation);
    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);
        IOUtils.copy(CompareUtilities.loadTestResourceStream("r5", "graphql", source), new FileOutputStream(CompareUtilities.tempFile("graphql", source)));
        IOUtils.copy(CompareUtilities.loadTestResourceStream("r5", "graphql", output), new FileOutputStream(CompareUtilities.tempFile("graphql", output)));
        TextFile.stringToFile(str.toString(), CompareUtilities.tempFile("graphql", output + ".out"));
        msg = CompareUtilities.checkJsonIsSame(CompareUtilities.tempFile("graphql", output), CompareUtilities.tempFile("graphql", output + ".out"));
        Assertions.assertTrue(Utilities.noString(msg), msg);
    } else
        Assertions.assertTrue(output.equals("$error"), "Error, but proper output was expected (" + msg + ")");
}
Also used : GraphQLEngine(org.hl7.fhir.r5.utils.GraphQLEngine) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)24 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)21 Argument (org.hl7.fhir.utilities.graphql.Argument)20 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)20 ArrayList (java.util.ArrayList)16 StringValue (org.hl7.fhir.utilities.graphql.StringValue)12 ReferenceResolution (org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 DomainResource (org.hl7.fhir.r4b.model.DomainResource)5 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)5 Resource (org.hl7.fhir.r4b.model.Resource)5 Base (org.hl7.fhir.r4b.model.Base)4 Directive (org.hl7.fhir.utilities.graphql.Directive)4 NameValue (org.hl7.fhir.utilities.graphql.NameValue)4 NumberValue (org.hl7.fhir.utilities.graphql.NumberValue)4 Value (org.hl7.fhir.utilities.graphql.Value)4 VariableValue (org.hl7.fhir.utilities.graphql.VariableValue)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3