private void consumePunctuation(String punc) throws EGraphQLException, IOException { if (lexType != LexType.gqlltPunctuation) throw new EGraphQLException("Found \""+token.toString()+"\" expecting \""+punc+"\""); if (!token.toString().equals(punc)) throw new EGraphQLException("Found \""+token.toString()+"\" expecting \""+punc+"\""); next(); }
private void consumeName(String name) throws EGraphQLException, IOException{ if (lexType != LexType.gqlltName) throw new EGraphQLException("Found \""+token.toString()+"\" expecting a name"); if (!token.toString().equals(name)) throw new EGraphQLException("Found \""+token.toString()+"\" expecting \""+name+"\""); next(); }
private String consumeName() throws EGraphQLException, IOException { if (lexType != LexType.gqlltName) throw new EGraphQLException("Found \""+token.toString()+"\" expecting a name"); String result = token.toString(); next(); return result; }
private List<Value> resolveValues(Argument arg, int max, String vars) throws EGraphQLException { List<Value> result = new ArrayList<Value>(); for (Value v : arg.getValues()) { if (! (v instanceof VariableValue)) result.add(v); else { if (vars.contains(":"+v.toString()+":")) throw new EGraphQLException("Recursive reference to variable "+v.toString()); Argument a = workingVariables.get(v.toString()); if (a == null) throw new EGraphQLException("No value found for variable \""+v.toString()+"\" in \""+arg.getName()+"\""); List<Value> vl = resolveValues(a, -1, vars+":"+v.toString()+":"); result.addAll(vl); } } if ((max != -1 && result.size() > max)) throw new EGraphQLException("Only "+Integer.toString(max)+" values are allowed for \""+arg.getName()+"\", but "+Integer.toString(result.size())+" enoucntered"); return result; }
private boolean checkBooleanDirective(Directive dir) throws EGraphQLException { if (dir.getArguments().size() != 1) throw new EGraphQLException("Unable to process @"+dir.getName()+": expected a single argument \"if\""); if (!dir.getArguments().get(0).getName().equals("if")) throw new EGraphQLException("Unable to process @"+dir.getName()+": expected a single argument \"if\""); List<Value> vl = resolveValues(dir.getArguments().get(0), 1); return vl.get(0).toString().equals("true"); }
} else { if (values.size() > 1) throw new EGraphQLException("Internal error: non list \""+name+"\" has "+Integer.toString(values.size())+" values"); if (values.size() == 0) b.append("null");
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; }
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()); }
pushChar(ch); if ((token.length() != 3)) throw new EGraphQLException("Found \""+token.toString()+"\" expecting \"...\""); } else if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch == '_')) { lexType = LexType.gqlltName; if (ch == '\\') { if (!reader.ready()) throw new EGraphQLException("premature termination of GraphQL during a string constant"); ch = getNextChar(); if (ch == '"') token.append('"'); token.append((char) Integer.parseInt(hex, 16)); } else throw new EGraphQLException("Unexpected character: \""+ch+"\""); ch = '\0'; } else if (ch != '"') } while (!(readerDone || ch == '"')); if (ch != '"') throw new EGraphQLException("premature termination of GraphQL during a string constant"); } else throw new EGraphQLException("Unexpected character \""+ch+"\"");
List<Value> vl = resolveValues(arg); if ((vl.size() != 1)) throw new EGraphQLException("Incorrect number of arguments"); if (values.get(0).isPrimitive()) throw new EGraphQLException("Attempt to use a filter ("+arg.getName()+") on a primtive type ("+prop.getTypeCode()+")"); if ((arg.getName().equals("fhirpath"))) fp.append(" and "+vl.get(0).toString()); Property p = values.get(0).getNamedProperty(arg.getName()); if (p == null) throw new EGraphQLException("Attempt to use an unknown filter ("+arg.getName()+") on a type ("+prop.getTypeCode()+")"); fp.append(" and "+arg.getName()+" = '"+vl.get(0).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 processVariables(Operation op) throws EGraphQLException { for (Variable varRef : op.getVariables()) { Argument varDef = null; for (Argument v : graphQL.getVariables()) if (v.getName().equals(varRef.getName())) varDef = v; if (varDef != null) workingVariables.put(varRef.getName(), varDef); // todo: check type? else if (varRef.getDefaultValue() != null) workingVariables.put(varRef.getName(), new Argument(varRef.getName(), varRef.getDefaultValue())); else throw new EGraphQLException("No value found for variable "); } }
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; }
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 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<Resource> list = new ArrayList<Resource>(); List<Argument> params = new ArrayList<Argument>(); 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);
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); }
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); } } }
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()); }
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); }
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, ""); }