X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/main/java/graphql/language/AstPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import graphql.PublicApi;
import graphql.collect.ImmutableKit;

import java.io.PrintWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -745,10 +746,33 @@ String wrap(String start, Node maybeNode, String end) {
*/
public static String printAst(Node node) {
StringBuilder builder = new StringBuilder();
printImpl(builder, node, false);
printAstTo(node, builder);
return builder.toString();
}

/**
* This will pretty print the AST node in graphql language format to the given Appendable
*
* @param node the AST node to print
* @param appendable the Appendable to write the output to
*
*/
public static void printAstTo(Node<?> node, Appendable appendable) {
if (appendable instanceof StringBuilder) {
printImpl((StringBuilder) appendable, node, false);
} else if (appendable instanceof Writer) {
printAst((Writer) appendable, node);
} else {
StringBuilder builder = new StringBuilder();
printImpl(builder, node, false);
try {
appendable.append(builder);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

/**
* This will pretty print the AST node in graphql language format
*
Expand All @@ -757,8 +781,11 @@ public static String printAst(Node node) {
*/
public static void printAst(Writer writer, Node node) {
String ast = printAst(node);
PrintWriter printer = new PrintWriter(writer);
printer.write(ast);
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kilink @bbakerman @dondonz why did we change that? What is the consequence of that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PrintWriter never throws an exception but just drops data.

This is better really. Change in behavior yes... but much better behavior

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a breaking change needs to be documented?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can call out that it's a change in the release notes, I can add the label

writer.write(ast);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
Expand All @@ -775,7 +802,7 @@ public static String printAstCompact(Node node) {
return builder.toString();
}

private static void printImpl(StringBuilder writer, Node node, boolean compactMode) {
private static void printImpl(StringBuilder writer, Node<?> node, boolean compactMode) {
AstPrinter astPrinter = new AstPrinter(compactMode);
NodePrinter<Node> printer = astPrinter._findPrinter(node);
printer.print(writer, node);
Expand Down
39 changes: 39 additions & 0 deletions src/test/groovy/graphql/language/AstPrinterTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package graphql.language
import graphql.parser.Parser
import spock.lang.Specification

import java.nio.CharBuffer

class AstPrinterTest extends Specification {

Document parse(String input) {
Expand Down Expand Up @@ -769,4 +771,41 @@ extend input Input @directive {
then:
output == "foo"
}

def "printAstTo writes to a StringBuilder instance"() {
def document = parse(starWarsSchema)
def output = new StringBuilder()
AstPrinter.printAstTo(document.getDefinitions().get(0), output)

expect:
output.toString() == """schema {
query: QueryType
mutation: Mutation
}"""
}

def "printAstTo writes to a Writer instance"() {
def document = parse(starWarsSchema)
def output = new StringWriter()
AstPrinter.printAstTo(document.getDefinitions().get(0), output)

expect:
output.toString() == """schema {
query: QueryType
mutation: Mutation
}"""
}

def "printAstTo writes to an Appendable instance"() {
def document = parse(starWarsSchema)
def output = CharBuffer.allocate(100)
AstPrinter.printAstTo(document.getDefinitions().get(0), output)
output.flip()

expect:
output.toString() == """schema {
query: QueryType
mutation: Mutation
}"""
}
}
Loading
X Tutup