Search in sources :

Example 1 with ObjectValue

use of org.hl7.fhir.utilities.graphql.ObjectValue 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());
}
Also used : ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)

Example 2 with ObjectValue

use of org.hl7.fhir.utilities.graphql.ObjectValue 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);
}
Also used : IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 3 with ObjectValue

use of org.hl7.fhir.utilities.graphql.ObjectValue 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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 4 with ObjectValue

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

the class GraphQLEngine method processReverseReferenceList.

private void processReverseReferenceList(Resource source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    List<IBaseResource> list = new ArrayList<>();
    List<Argument> params = new ArrayList<>();
    Argument parg = null;
    for (Argument a : field.getArguments()) if (!(a.getName().equals("_reference")))
        params.add(a);
    else if ((parg == null))
        parg = a;
    else
        throw new EGraphQLException("Duplicate parameter _reference");
    if (parg == null)
        throw new EGraphQLException("Missing parameter _reference");
    Argument arg = new Argument();
    params.add(arg);
    arg.setName(getSingleValue(parg));
    arg.addValue(new StringValue(source.fhirType() + "/" + source.getIdPart()));
    services.listResources(appInfo, field.getName().substring(0, field.getName().length() - 4), params, list);
    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);
        }
    }
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) StringValue(org.hl7.fhir.utilities.graphql.StringValue) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 5 with ObjectValue

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

the class GraphQLEngine method processValues.

private void processValues(Resource context, Selection sel, Property prop, ObjectValue target, List<Base> values, boolean extensionMode, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    boolean il = false;
    Argument arg = null;
    ExpressionNode expression = null;
    if (sel.getField().hasDirective("slice")) {
        Directive dir = sel.getField().directive("slice");
        String s = ((StringValue) dir.getArguments().get(0).getValues().get(0)).getValue();
        if (s.equals("$index"))
            expression = magicExpression;
        else
            expression = fpe.parse(s);
    }
    if (// special: instruction to drop this node...
    sel.getField().hasDirective("flatten"))
        il = prop.isList() && !sel.getField().hasDirective("first");
    else if (sel.getField().hasDirective("first")) {
        if (expression != null)
            throw new FHIRException("You cannot mix @slice and @first");
        arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), inheritedList));
    } else if (expression == null)
        arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), prop.isList() || inheritedList));
    int index = 0;
    for (Base value : values) {
        String ss = "";
        if (expression != null) {
            if (expression == magicExpression)
                ss = suffix + '.' + Integer.toString(index);
            else
                ss = suffix + '.' + fpe.evaluateToString(null, null, null, value, expression);
            if (!sel.getField().hasDirective("flatten"))
                arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), prop.isList() || inheritedList));
        }
        if (value.isPrimitive() && !extensionMode) {
            if (!sel.getField().getSelectionSet().isEmpty())
                throw new EGraphQLException("Encountered a selection set on a scalar field type");
            processPrimitive(arg, value);
        } else {
            if (sel.getField().getSelectionSet().isEmpty())
                throw new EGraphQLException("No Fields selected on a complex object");
            if (arg == null)
                processObject(context, value, target, sel.getField().getSelectionSet(), il, ss);
            else {
                ObjectValue n = new ObjectValue();
                arg.addValue(n);
                processObject(context, value, n, sel.getField().getSelectionSet(), il, ss);
            }
        }
        if (sel.getField().hasDirective("first"))
            return;
        index++;
    }
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) StringValue(org.hl7.fhir.utilities.graphql.StringValue) Directive(org.hl7.fhir.utilities.graphql.Directive) FHIRException(org.hl7.fhir.exceptions.FHIRException) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Aggregations

Argument (org.hl7.fhir.utilities.graphql.Argument)16 EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)16 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)16 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 ArrayList (java.util.ArrayList)8 ReferenceResolution (org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)8 StringValue (org.hl7.fhir.utilities.graphql.StringValue)8 DomainResource (org.hl7.fhir.r4b.model.DomainResource)3 Resource (org.hl7.fhir.r4b.model.Resource)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 Base (org.hl7.fhir.r4b.model.Base)2 Bundle (org.hl7.fhir.r4b.model.Bundle)2 Reference (org.hl7.fhir.r4b.model.Reference)2 Directive (org.hl7.fhir.utilities.graphql.Directive)2 CanonicalType (org.hl7.fhir.r4b.model.CanonicalType)1 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)1