Skip to content

Commit 59679e0

Browse files
authored
Improve SchemaPrinter (#2881)
* Remove redundant isNullOrEmpty check * Consistently print schema elements with one trailing new line char * Remove trailing spaces when printing arguments with descriptions * Cleanup SchemaPrinterTest, SchemaPrinterComparatorsTest and SchemaTransformerTest
1 parent 325667f commit 59679e0

4 files changed

Lines changed: 88 additions & 92 deletions

File tree

src/main/java/graphql/schema/idl/SchemaPrinter.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,7 @@ public String print(GraphQLSchema schema) {
447447
printSchemaElement(out, element, visibility);
448448
}
449449

450-
String result = sw.toString();
451-
if (result.endsWith("\n\n")) {
452-
result = result.substring(0, result.length() - 1);
453-
}
454-
return result;
450+
return trimNewLineChars(sw.toString());
455451
}
456452

457453
private interface SchemaElementPrinter<T> {
@@ -796,7 +792,10 @@ String argsString(Class<? extends GraphQLSchemaElement> parent, List<GraphQLArgu
796792
if (count == 0) {
797793
sb.append("(");
798794
} else {
799-
sb.append(", ");
795+
sb.append(",");
796+
if (!hasDescriptions) {
797+
sb.append(" ");
798+
}
800799
}
801800
if (hasDescriptions) {
802801
sb.append("\n");
@@ -989,7 +988,7 @@ public String print(GraphQLType type) {
989988

990989
printSchemaElement(out, type, DEFAULT_FIELD_VISIBILITY);
991990

992-
return sw.toString();
991+
return trimNewLineChars(sw.toString());
993992
}
994993

995994
public String print(List<GraphQLSchemaElement> elements) {
@@ -1005,7 +1004,7 @@ public String print(List<GraphQLSchemaElement> elements) {
10051004
Assert.assertShouldNeverHappen("How did we miss a %s", element.getClass());
10061005
}
10071006
}
1008-
return sw.toString();
1007+
return trimNewLineChars(sw.toString());
10091008
}
10101009

10111010
public String print(GraphQLDirective graphQLDirective) {
@@ -1025,22 +1024,19 @@ private String printComments(Object graphQLType, String prefix) {
10251024
}
10261025

10271026
private void printComments(PrintWriter out, Object graphQLType, String prefix) {
1028-
10291027
String descriptionText = getDescription(graphQLType);
10301028
if (isNullOrEmpty(descriptionText)) {
10311029
return;
10321030
}
10331031

1034-
if (!isNullOrEmpty(descriptionText)) {
1035-
List<String> lines = Arrays.asList(descriptionText.split("\n"));
1036-
if (options.isDescriptionsAsHashComments()) {
1037-
printMultiLineHashDescription(out, prefix, lines);
1038-
} else if (!lines.isEmpty()) {
1039-
if (lines.size() > 1) {
1040-
printMultiLineDescription(out, prefix, lines);
1041-
} else {
1042-
printSingleLineDescription(out, prefix, lines.get(0));
1043-
}
1032+
List<String> lines = Arrays.asList(descriptionText.split("\n"));
1033+
if (options.isDescriptionsAsHashComments()) {
1034+
printMultiLineHashDescription(out, prefix, lines);
1035+
} else if (!lines.isEmpty()) {
1036+
if (lines.size() > 1) {
1037+
printMultiLineDescription(out, prefix, lines);
1038+
} else {
1039+
printSingleLineDescription(out, prefix, lines.get(0));
10441040
}
10451041
}
10461042
}
@@ -1133,6 +1129,12 @@ private Comparator<? super GraphQLSchemaElement> getComparator(Class<? extends G
11331129
return options.comparatorRegistry.getComparator(environment);
11341130
}
11351131

1132+
private static String trimNewLineChars(String s) {
1133+
if (s.endsWith("\n\n")) {
1134+
s = s.substring(0, s.length() - 1);
1135+
}
1136+
return s;
1137+
}
11361138

11371139
private static boolean isNullOrEmpty(String s) {
11381140
return s == null || s.isEmpty();

0 commit comments

Comments
 (0)