Search in sources :

Example 1 with Directive

use of org.hl7.fhir.utilities.graphql.Directive 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)

Example 2 with Directive

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

the class GraphQLEngine method checkDirectives.

private boolean checkDirectives(List<Directive> directives) throws EGraphQLException {
    Directive skip = null;
    Directive include = null;
    for (Directive dir : directives) {
        if (dir.getName().equals("skip")) {
            if ((skip == null))
                skip = dir;
            else
                throw new EGraphQLException("Duplicate @skip directives");
        } else if (dir.getName().equals("include")) {
            if ((include == null))
                include = dir;
            else
                throw new EGraphQLException("Duplicate @include directives");
        } else if (!Utilities.existsInList(dir.getName(), "flatten", "first", "singleton", "slice"))
            throw new EGraphQLException("Directive \"" + dir.getName() + "\" instanceof not recognised");
    }
    if ((skip != null && include != null))
        throw new EGraphQLException("Cannot mix @skip and @include directives");
    if (skip != null)
        return !checkBooleanDirective(skip);
    else if (include != null)
        return checkBooleanDirective(include);
    else
        return true;
}
Also used : Directive(org.hl7.fhir.utilities.graphql.Directive) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 3 with Directive

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

the class GraphQLEngine method checkDirectives.

private boolean checkDirectives(List<Directive> directives) throws EGraphQLException {
    Directive skip = null;
    Directive include = null;
    for (Directive dir : directives) {
        if (dir.getName().equals("skip")) {
            if ((skip == null))
                skip = dir;
            else
                throw new EGraphQLException("Duplicate @skip directives");
        } else if (dir.getName().equals("include")) {
            if ((include == null))
                include = dir;
            else
                throw new EGraphQLException("Duplicate @include directives");
        } else if (!Utilities.existsInList(dir.getName(), "flatten", "first", "singleton", "slice"))
            throw new EGraphQLException("Directive \"" + dir.getName() + "\" instanceof not recognised");
    }
    if ((skip != null && include != null))
        throw new EGraphQLException("Cannot mix @skip and @include directives");
    if (skip != null)
        return !checkBooleanDirective(skip);
    else if (include != null)
        return checkBooleanDirective(include);
    else
        return true;
}
Also used : Directive(org.hl7.fhir.utilities.graphql.Directive) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 4 with Directive

use of org.hl7.fhir.utilities.graphql.Directive 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) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) 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) Base(org.hl7.fhir.r4b.model.Base)

Aggregations

Directive (org.hl7.fhir.utilities.graphql.Directive)4 EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)4 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 Argument (org.hl7.fhir.utilities.graphql.Argument)2 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)2 StringValue (org.hl7.fhir.utilities.graphql.StringValue)2 Base (org.hl7.fhir.r4b.model.Base)1 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)1