public static String getAstDesc(Type type) { return AstPrinter.printAst(type); }
private static void printImpl(Writer writer, Node node, boolean compactMode) { AstPrinter astPrinter = new AstPrinter(compactMode); NodePrinter<Node> printer = astPrinter._findPrinter(node); printer.print(new PrintWriter(writer), node); }
private AstPrinter(boolean compactMode) { this.compactMode = compactMode; printers.put(Argument.class, argument()); printers.put(ArrayValue.class, value()); printers.put(BooleanValue.class, value()); printers.put(NullValue.class, value()); printers.put(Directive.class, directive()); printers.put(DirectiveDefinition.class, directiveDefinition()); printers.put(DirectiveLocation.class, directiveLocation()); printers.put(Document.class, document()); printers.put(EnumTypeDefinition.class, enumTypeDefinition()); printers.put(EnumTypeExtensionDefinition.class, enumTypeExtensionDefinition()); printers.put(EnumValue.class, enumValue()); printers.put(EnumValueDefinition.class, enumValueDefinition()); printers.put(Field.class, field()); printers.put(FieldDefinition.class, fieldDefinition()); printers.put(FloatValue.class, value()); printers.put(FragmentDefinition.class, fragmentDefinition()); printers.put(FragmentSpread.class, fragmentSpread()); printers.put(InlineFragment.class, inlineFragment()); printers.put(InputObjectTypeDefinition.class, inputObjectTypeDefinition()); printers.put(InputObjectTypeExtensionDefinition.class, inputObjectTypeExtensionDefinition()); printers.put(InputValueDefinition.class, inputValueDefinition()); printers.put(InterfaceTypeDefinition.class, interfaceTypeDefinition()); printers.put(InterfaceTypeExtensionDefinition.class, interfaceTypeExtensionDefinition()); printers.put(IntValue.class, value()); printers.put(ListType.class, type()); printers.put(NonNullType.class, type()); printers.put(ObjectField.class, objectField()); printers.put(ObjectTypeDefinition.class, objectTypeDefinition());
private NodePrinter<SchemaDefinition> schemaDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "schema", directives(node.getDirectives()), block(node.getOperationTypeDefinitions()) )); }; }
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<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<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<EnumValueDefinition> enumValueDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( node.getName(), directives(node.getDirectives()) )); }; }
private NodePrinter<UnionTypeDefinition> unionTypeDefinition() { String barSep = compactMode ? "|" : " | "; String equals = compactMode ? "=" : "= "; return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "union", node.getName(), directives(node.getDirectives()), equals + join(node.getMemberTypes(), barSep) )); }; }
private NodePrinter<FragmentDefinition> fragmentDefinition() { return (out, node) -> { String name = node.getName(); String typeCondition = type(node.getTypeCondition()); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("fragment %s on %s ", name, typeCondition); out.printf("%s", directives + selectionSet); }; }
@SuppressWarnings("unchecked") private <T extends Node> NodePrinter<T> _findPrinter(Node node) { return _findPrinter(node, null); }
private NodePrinter<FragmentSpread> fragmentSpread() { return (out, node) -> { String name = node.getName(); String directives = directives(node.getDirectives()); out.printf("...%s%s", name, directives); }; }
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<InterfaceTypeDefinition> interfaceTypeDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "interface", node.getName(), 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)); } }; }
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<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()) )); }; }