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<Resource> list = new ArrayList<Resource>(); 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); } } }
/** * Write the output using the system default line separator (as defined in {@link System#lineSeparator} * @param b The StringBuilder to populate * @param indent The indent level, or <code>-1</code> for no indent */ public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine { write(b, indent, System.lineSeparator()); }
private Value parseValue() throws EGraphQLException, IOException { Value result = null; switch (lexType) { case gqlltNull: throw new EGraphQLException("Attempt to read a value after reading off the } of the GraphQL statement"); case gqlltName: result = new NameValue(token.toString()); break; case gqlltPunctuation: if (hasPunctuation("$")) { consumePunctuation("$"); result = new VariableValue(token.toString()); } else if (hasPunctuation("{")) { consumePunctuation("{"); ObjectValue obj = new ObjectValue(); while (!hasPunctuation("}")) obj.getFields().add(parseArgument()); result = obj; } else throw new EGraphQLException("Attempt to read a value at \""+token.toString()+"\""); break; case gqlltString: result = new StringValue(token.toString()); break; case gqlltNumber: result = new NumberValue(token.toString()); break; } next(); return result; }
public void valuesFromNode(JsonElement json) throws EGraphQLException { if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isString()) values.add(new StringValue(((JsonPrimitive)json).getAsString())); else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isNumber()) values.add(new NumberValue(((JsonPrimitive)json).getAsString())); else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isBoolean()) values.add(new NameValue(((JsonPrimitive)json).getAsBoolean())); else if (json instanceof JsonObject) values.add(new ObjectValue((JsonObject) json)); else if (json instanceof JsonArray) { for (JsonElement v : (JsonArray) json) valuesFromNode(v); } else throw new EGraphQLException("Unexpected JSON type for \""+name+"\": "+json.getClass().getName()); }
if (prop == null) { if ((sel.getField().getName().equals("resourceType") && source instanceof Resource)) target.addField("resourceType", listStatus(sel.getField(), false)).addValue(new StringValue(source.fhirType())); else if ((sel.getField().getName().equals("resource") && source.fhirType().equals("Reference"))) processReference(context, source, sel.getField(), target, inheritedList, suffix);
public void execute() throws EGraphEngine, EGraphQLException, FHIRException { if (graphQL == null) throw new EGraphEngine("Unable to process graphql - graphql document missing"); fpe = new FHIRPathEngine(this.context); magicExpression = new ExpressionNode(0); output = new ObjectValue(); Operation op = null; // todo: initial conditions if (!Utilities.noString(graphQL.getOperationName())) { op = graphQL.getDocument().operation(graphQL.getOperationName()); if (op == null) throw new EGraphEngine("Unable to find operation \""+graphQL.getOperationName()+"\""); } else if ((graphQL.getDocument().getOperations().size() == 1)) op = graphQL.getDocument().getOperations().get(0); else throw new EGraphQLException("No operation name provided, so expected to find a single operation"); if (op.getOperationType() == OperationType.qglotMutation) throw new EGraphQLException("Mutation operations are not supported (yet)"); checkNoDirectives(op.getDirectives()); processVariables(op); if (focus == null) processSearch(output, op.getSelectionSet(), false, ""); else processObject(focus, focus, output, op.getSelectionSet(), false, ""); }
if (prop == null) { if ((sel.getField().getName().equals("resourceType") && source instanceof Resource)) target.addField("resourceType", listStatus(sel.getField(), false)).addValue(new StringValue(source.fhirType())); else if ((sel.getField().getName().equals("resource") && source.fhirType().equals("Reference"))) processReference(context, source, sel.getField(), target, inheritedList, suffix);
private void processSearchFull(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); List<Argument> params = new ArrayList<Argument>(); Argument carg = null; for ( Argument arg : field.getArguments()) if (arg.getName().equals("cursor")) carg = arg; else params.add(arg); if ((carg != null)) { params.clear();; String[] parts = getSingleValue(carg).split(":"); params.add(new Argument("search-id", new StringValue(parts[0]))); params.add(new Argument("search-offset", new StringValue(parts[1]))); } Bundle bnd = services.search(appInfo, field.getName().substring(0, field.getName().length()-10), params); SearchWrapper bndWrapper = new SearchWrapper(field.getName(), bnd); Argument arg = target.addField(field.getAlias()+suffix, listStatus(field, false)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(null, bndWrapper, obj, field.getSelectionSet(), inheritedList, suffix); }
private Value parseValue() throws EGraphQLException, IOException { Value result = null; switch (lexType) { case gqlltNull: throw new EGraphQLException("Attempt to read a value after reading off the } of the GraphQL statement"); case gqlltName: result = new NameValue(token.toString()); break; case gqlltPunctuation: if (hasPunctuation("$")) { consumePunctuation("$"); result = new VariableValue(token.toString()); } else if (hasPunctuation("{")) { consumePunctuation("{"); ObjectValue obj = new ObjectValue(); while (!hasPunctuation("}")) obj.getFields().add(parseArgument()); result = obj; } else throw new EGraphQLException("Attempt to read a value at \""+token.toString()+"\""); break; case gqlltString: result = new StringValue(token.toString()); break; case gqlltNumber: result = new NumberValue(token.toString()); break; } next(); return result; }
@GraphQL public String graphql(ServletRequestDetails theRequestDetails, @IdParam IIdType theId, @GraphQLQuery String theQuery) { GraphQLEngine engine = new GraphQLEngine(myWorkerContext); engine.setServices(myStorageServices); try { engine.setGraphQL(Parser.parse(theQuery)); } catch (Exception theE) { throw new InvalidRequestException("Unable to parse GraphQL Expression: " + theE.toString()); } try { if (theId != null) { Resource focus = myStorageServices.lookup(theRequestDetails, theId.getResourceType(), theId.getIdPart()); engine.setFocus(focus); } engine.execute(); StringBuilder outputBuilder = new StringBuilder(); ObjectValue output = engine.getOutput(); output.write(outputBuilder, 0, "\n"); return outputBuilder.toString(); } catch (Exception theE) { throw new InvalidRequestException("Unable to execute GraphQL Expression: " + theE.toString()); } }
public void valuesFromNode(JsonElement json) throws EGraphQLException { if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isString()) values.add(new StringValue(((JsonPrimitive)json).getAsString())); else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isNumber()) values.add(new NumberValue(((JsonPrimitive)json).getAsString())); else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isBoolean()) values.add(new NameValue(((JsonPrimitive)json).getAsBoolean())); else if (json instanceof JsonObject) values.add(new ObjectValue((JsonObject) json)); else if (json instanceof JsonArray) { for (JsonElement v : (JsonArray) json) valuesFromNode(v); } else throw new EGraphQLException("Unexpected JSON type for \""+name+"\": "+json.getClass().getName()); }
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);
/** * Write the output using the system default line separator (as defined in {@link System#lineSeparator} * @param b The StringBuilder to populate * @param indent The indent level, or <code>-1</code> for no indent */ public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine { write(b, indent, System.lineSeparator()); }
public void execute() throws EGraphEngine, EGraphQLException, FHIRException { if (graphQL == null) throw new EGraphEngine("Unable to process graphql - graphql document missing"); fpe = new FHIRPathEngine(this.context); magicExpression = new ExpressionNode(0); output = new ObjectValue(); Operation op = null; // todo: initial conditions if (!Utilities.noString(graphQL.getOperationName())) { op = graphQL.getDocument().operation(graphQL.getOperationName()); if (op == null) throw new EGraphEngine("Unable to find operation \""+graphQL.getOperationName()+"\""); } else if ((graphQL.getDocument().getOperations().size() == 1)) op = graphQL.getDocument().getOperations().get(0); else throw new EGraphQLException("No operation name provided, so expected to find a single operation"); if (op.getOperationType() == OperationType.qglotMutation) throw new EGraphQLException("Mutation operations are not supported (yet)"); checkNoDirectives(op.getDirectives()); processVariables(op); if (focus == null) processSearch(output, op.getSelectionSet(), false, ""); else processObject(focus, focus, output, op.getSelectionSet(), false, ""); }
private void processReverseReferenceSearch(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<Argument> params = new ArrayList<Argument>(); 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.getId())); Bundle bnd = services.search(appInfo, field.getName().substring(0, field.getName().length()-10), params); Base bndWrapper = new SearchWrapper(field.getName(), bnd); arg = target.addField(field.getAlias()+suffix, listStatus(field, false)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(null, bndWrapper, obj, field.getSelectionSet(), inheritedList, suffix); }
@GraphQL public String graphql(ServletRequestDetails theRequestDetails, @IdParam IIdType theId, @GraphQLQuery String theQuery) { GraphQLEngine engine = new GraphQLEngine(myWorkerContext); engine.setServices(myStorageServices); try { engine.setGraphQL(Parser.parse(theQuery)); } catch (Exception theE) { throw new InvalidRequestException("Unable to parse GraphQL Expression: " + theE.toString()); } try { if (theId != null) { Resource focus = myStorageServices.lookup(theRequestDetails, theId.getResourceType(), theId.getIdPart()); engine.setFocus(focus); } engine.execute(); StringBuilder outputBuilder = new StringBuilder(); ObjectValue output = engine.getOutput(); output.write(outputBuilder, 0, "\n"); return outputBuilder.toString(); } catch (Exception theE) { throw new InvalidRequestException("Unable to execute GraphQL Expression: " + theE.toString()); } }
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 = 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); }
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.target)) { Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(res.targetContext, res.target, obj, field.getSelectionSet(), inheritedList, suffix); } } else if (!hasArgument(field.getArguments(), "optional", "true")) throw new EGraphQLException("Unable to resolve reference to "+ref.getReference()); }
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.target)) { Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(res.targetContext, res.target, obj, field.getSelectionSet(), inheritedList, suffix); } } else if (!hasArgument(field.getArguments(), "optional", "true")) throw new EGraphQLException("Unable to resolve reference to "+ref.getReference()); }
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)); ss = suffix+'.'+fpe.evaluateToString(null, null, value, expression); if (!sel.getField().hasDirective("flatten")) arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), prop.isList() || inheritedList)); 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);