private NodePrinter<Directive> directive() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String arguments = wrap("(", join(node.getArguments(), argSep), ")"); out.printf("@%s%s", node.getName(), arguments); }; }
private String type(Type type) { if (type instanceof NonNullType) { NonNullType inner = (NonNullType) type; return wrap("", type(inner.getType()), "!"); } else if (type instanceof ListType) { ListType inner = (ListType) type; return wrap("[", type(inner.getType()), "]"); } else { TypeName inner = (TypeName) type; return inner.getName(); } }
private NodePrinter<VariableDefinition> variableDefinition() { String nameTypeSep = compactMode ? ":" : ": "; String defaultValueEquals = compactMode ? "=" : " = "; return (out, node) -> out.printf("$%s%s%s%s", node.getName(), nameTypeSep, type(node.getType()), wrap(defaultValueEquals, node.getDefaultValue(), "") ); }
private NodePrinter<DirectiveDefinition> directiveDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String arguments = wrap("(", join(node.getInputValueDefinitions(), argSep), ")"); String locations = join(node.getDirectiveLocations(), " | "); out.printf("directive @%s%s on %s", node.getName(), arguments, locations); }; }
private NodePrinter<InputValueDefinition> inputValueDefinition() { String nameTypeSep = compactMode ? ":" : ": "; String defaultValueEquals = compactMode ? "=" : "= "; return (out, node) -> { Value defaultValue = node.getDefaultValue(); out.printf("%s", comments(node)); out.printf("%s", spaced( node.getName() + nameTypeSep + type(node.getType()), wrap(defaultValueEquals, defaultValue, ""), directives(node.getDirectives()) ) ); }; }
private NodePrinter<Field> field() { final String argSep = compactMode ? "," : ", "; final String aliasSuffix = compactMode ? ":" : ": "; return (out, node) -> { String alias = wrap("", node.getAlias(), aliasSuffix); String name = node.getName(); String arguments = wrap("(", join(node.getArguments(), argSep), ")"); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("%s", spaced( alias + name + arguments, directives, selectionSet )); }; }
private NodePrinter<ObjectTypeDefinition> objectTypeDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "type", node.getName(), wrap("implements ", join(node.getImplements(), " & "), ""), directives(node.getDirectives()), block(node.getFieldDefinitions()) )); }; }
private String value(Value value) { String argSep = compactMode ? "," : ", "; if (value instanceof IntValue) { return valueOf(((IntValue) value).getValue()); } else if (value instanceof FloatValue) { return valueOf(((FloatValue) value).getValue()); } else if (value instanceof StringValue) { return wrap("\"", valueOf(((StringValue) value).getValue()), "\""); } else if (value instanceof EnumValue) { return valueOf(((EnumValue) value).getName()); } else if (value instanceof BooleanValue) { return valueOf(((BooleanValue) value).isValue()); } else if (value instanceof NullValue) { return "null"; } else if (value instanceof ArrayValue) { return "[" + join(((ArrayValue) value).getValues(), argSep) + "]"; } else if (value instanceof ObjectValue) { return "{" + join(((ObjectValue) value).getObjectFields(), argSep) + "}"; } else if (value instanceof VariableReference) { return "$" + ((VariableReference) value).getName(); } return ""; }
private NodePrinter<InlineFragment> inlineFragment() { return (out, node) -> { TypeName typeName = node.getTypeCondition(); //Inline fragments may not have a type condition String typeCondition = typeName == null ? "" : wrap("on ", type(typeName), ""); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("%s", comments(node)); out.printf("%s", spaced( "...", typeCondition, directives, selectionSet )); }; }
private NodePrinter<OperationDefinition> operationDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String op = node.getOperation().toString().toLowerCase(); String name = node.getName(); String varDefinitions = wrap("(", join(nvl(node.getVariableDefinitions()), argSep), ")"); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); // Anonymous queries with no directives or variable definitions can use // the query short form. if (isEmpty(name) && isEmpty(directives) && isEmpty(varDefinitions) && op.equals("QUERY")) { out.printf("%s", selectionSet); } else { out.printf("%s", spaced(op, smooshed(name, varDefinitions), directives, selectionSet)); } }; }
private NodePrinter<FieldDefinition> fieldDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { out.printf("%s", comments(node)); String args; if (hasComments(node.getInputValueDefinitions()) && !compactMode) { args = join(node.getInputValueDefinitions(), "\n"); out.printf("%s", node.getName() + wrap("(\n", args, "\n)") + ": " + spaced( type(node.getType()), directives(node.getDirectives()) ) ); } else { args = join(node.getInputValueDefinitions(), argSep); out.printf("%s", node.getName() + wrap("(", args, ")") + ": " + spaced( type(node.getType()), directives(node.getDirectives()) ) ); } }; }
private NodePrinter<Directive> directive() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String arguments = wrap("(", join(node.getArguments(), argSep), ")"); out.printf("@%s%s", node.getName(), arguments); }; }
private String type(Type type) { if (type instanceof NonNullType) { NonNullType inner = (NonNullType) type; return wrap("", type(inner.getType()), "!"); } else if (type instanceof ListType) { ListType inner = (ListType) type; return wrap("[", type(inner.getType()), "]"); } else { TypeName inner = (TypeName) type; return inner.getName(); } }
private NodePrinter<VariableDefinition> variableDefinition() { String nameTypeSep = compactMode ? ":" : ": "; String defaultValueEquals = compactMode ? "=" : " = "; return (out, node) -> out.printf("$%s%s%s%s", node.getName(), nameTypeSep, type(node.getType()), wrap(defaultValueEquals, node.getDefaultValue(), "") ); }
private NodePrinter<DirectiveDefinition> directiveDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String arguments = wrap("(", join(node.getInputValueDefinitions(), argSep), ")"); String locations = join(node.getDirectiveLocations(), " | "); out.printf("directive @%s%s on %s", node.getName(), arguments, locations); }; }
private NodePrinter<Field> field() { final String argSep = compactMode ? "," : ", "; final String aliasSuffix = compactMode ? ":" : ": "; return (out, node) -> { String alias = wrap("", node.getAlias(), aliasSuffix); String name = node.getName(); String arguments = wrap("(", join(node.getArguments(), argSep), ")"); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("%s", spaced( alias + name + arguments, directives, selectionSet )); }; }
private NodePrinter<InlineFragment> inlineFragment() { return (out, node) -> { TypeName typeName = node.getTypeCondition(); //Inline fragments may not have a type condition String typeCondition = typeName == null ? "" : wrap("on ", type(typeName), ""); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("%s", comments(node)); out.printf("%s", spaced( "...", typeCondition, directives, selectionSet )); }; }
private NodePrinter<InputValueDefinition> inputValueDefinition() { String nameTypeSep = compactMode ? ":" : ": "; String defaultValueEquals = compactMode ? "=" : "= "; return (out, node) -> { Value defaultValue = node.getDefaultValue(); out.printf("%s", comments(node)); out.printf("%s", spaced( node.getName() + nameTypeSep + type(node.getType()), wrap(defaultValueEquals, defaultValue, ""), directives(node.getDirectives()) ) ); }; }
private NodePrinter<ObjectTypeDefinition> objectTypeDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "type", node.getName(), wrap("implements ", join(node.getImplements(), " & "), ""), directives(node.getDirectives()), block(node.getFieldDefinitions()) )); }; }
private NodePrinter<OperationDefinition> operationDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String op = node.getOperation().toString().toLowerCase(); String name = node.getName(); String varDefinitions = wrap("(", join(nvl(node.getVariableDefinitions()), argSep), ")"); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); // Anonymous queries with no directives or variable definitions can use // the query short form. if (isEmpty(name) && isEmpty(directives) && isEmpty(varDefinitions) && op.equals("QUERY")) { out.printf("%s", selectionSet); } else { out.printf("%s", spaced(op, smooshed(name, varDefinitions), directives, selectionSet)); } }; }