From 15d8d5414530efc66cb414084d49cdff09fa59b8 Mon Sep 17 00:00:00 2001 From: david-castaneda Date: Thu, 21 Apr 2022 14:49:47 -0400 Subject: [PATCH 1/6] Fix AstPrinter to print field descriptions --- src/main/java/graphql/language/AstPrinter.java | 2 +- .../graphql/language/AstPrinterTest.groovy | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/graphql/language/AstPrinter.java b/src/main/java/graphql/language/AstPrinter.java index 1567162588..0addbb4f8a 100644 --- a/src/main/java/graphql/language/AstPrinter.java +++ b/src/main/java/graphql/language/AstPrinter.java @@ -162,9 +162,9 @@ private NodePrinter field() { private NodePrinter fieldDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { + out.append(description(node)); String args; if (hasDescription(node.getInputValueDefinitions()) && !compactMode) { - out.append(description(node)); args = join(node.getInputValueDefinitions(), "\n"); out.append(node.getName()) .append(wrap("(\n", args, ")")) diff --git a/src/test/groovy/graphql/language/AstPrinterTest.groovy b/src/test/groovy/graphql/language/AstPrinterTest.groovy index bb7024e8b6..a1c9f43482 100644 --- a/src/test/groovy/graphql/language/AstPrinterTest.groovy +++ b/src/test/groovy/graphql/language/AstPrinterTest.groovy @@ -472,6 +472,22 @@ type Query { } + def "print field descriptions"() { + def query = '''type Query { + "comment" + field: String +} +''' + def document = parse(query) + String output = printAst(document) + expect: + output == '''type Query { + "comment" + field: String +} +''' + } + def "print empty description"() { def query = ''' "" From b2506d010ca5cbdf15aabd287b0ec1d48bdc880f Mon Sep 17 00:00:00 2001 From: david-castaneda Date: Thu, 21 Apr 2022 16:02:03 -0400 Subject: [PATCH 2/6] consider compact mode --- src/main/java/graphql/language/AstPrinter.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/graphql/language/AstPrinter.java b/src/main/java/graphql/language/AstPrinter.java index 0addbb4f8a..dcb7d73a44 100644 --- a/src/main/java/graphql/language/AstPrinter.java +++ b/src/main/java/graphql/language/AstPrinter.java @@ -162,9 +162,9 @@ private NodePrinter field() { private NodePrinter fieldDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { - out.append(description(node)); String args; - if (hasDescription(node.getInputValueDefinitions()) && !compactMode) { + if (hasDescription(Collections.singletonList(node)) && !compactMode) { + out.append(description(node)); args = join(node.getInputValueDefinitions(), "\n"); out.append(node.getName()) .append(wrap("(\n", args, ")")) @@ -593,8 +593,8 @@ private StringBuilder indent(StringBuilder maybeString) { for (int i = 0; i < maybeString.length(); i++) { char c = maybeString.charAt(i); if (c == '\n') { - maybeString.replace(i,i+1,"\n "); - i+=3; + maybeString.replace(i, i + 1, "\n "); + i += 3; } } return maybeString; @@ -612,7 +612,6 @@ String wrap(String start, Node maybeNode, String end) { * This will pretty print the AST node in graphql language format * * @param node the AST node to print - * * @return the printed node in graphql language format */ public static String printAst(Node node) { @@ -638,7 +637,6 @@ public static void printAst(Writer writer, Node node) { * and comments stripped out of the text. * * @param node the AST node to print - * * @return the printed node in a compact graphql language format */ public static String printAstCompact(Node node) { From 8ecc8200ab52049ea85042cd6eadc2fee6783ae2 Mon Sep 17 00:00:00 2001 From: david-castaneda Date: Thu, 21 Apr 2022 16:04:56 -0400 Subject: [PATCH 3/6] fix autoformatted lines --- src/main/java/graphql/language/AstPrinter.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/graphql/language/AstPrinter.java b/src/main/java/graphql/language/AstPrinter.java index dcb7d73a44..19e102eb8b 100644 --- a/src/main/java/graphql/language/AstPrinter.java +++ b/src/main/java/graphql/language/AstPrinter.java @@ -5,10 +5,7 @@ import java.io.PrintWriter; import java.io.Writer; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static graphql.Assert.assertTrue; import static graphql.util.EscapeUtil.escapeJsonString; @@ -593,8 +590,8 @@ private StringBuilder indent(StringBuilder maybeString) { for (int i = 0; i < maybeString.length(); i++) { char c = maybeString.charAt(i); if (c == '\n') { - maybeString.replace(i, i + 1, "\n "); - i += 3; + maybeString.replace(i,i+1,"\n "); + i+=3; } } return maybeString; From a2e6702d77e9be579a68b2e5100ad0faa1653f20 Mon Sep 17 00:00:00 2001 From: david-castaneda Date: Thu, 21 Apr 2022 16:06:45 -0400 Subject: [PATCH 4/6] fix autoformatted imports --- src/main/java/graphql/language/AstPrinter.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/graphql/language/AstPrinter.java b/src/main/java/graphql/language/AstPrinter.java index 19e102eb8b..8d15854637 100644 --- a/src/main/java/graphql/language/AstPrinter.java +++ b/src/main/java/graphql/language/AstPrinter.java @@ -5,7 +5,10 @@ import java.io.PrintWriter; import java.io.Writer; -import java.util.*; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import static graphql.Assert.assertTrue; import static graphql.util.EscapeUtil.escapeJsonString; @@ -609,6 +612,7 @@ String wrap(String start, Node maybeNode, String end) { * This will pretty print the AST node in graphql language format * * @param node the AST node to print + * * @return the printed node in graphql language format */ public static String printAst(Node node) { @@ -634,6 +638,7 @@ public static void printAst(Writer writer, Node node) { * and comments stripped out of the text. * * @param node the AST node to print + * * @return the printed node in a compact graphql language format */ public static String printAstCompact(Node node) { From 749195ce82a00830eff56b4128e7e097d6c917ca Mon Sep 17 00:00:00 2001 From: david-castaneda Date: Tue, 26 Apr 2022 11:48:39 -0400 Subject: [PATCH 5/6] fix `IntrospectionResultToSchemaTest.groovy` test --- .../IntrospectionResultToSchemaTest.groovy | 37 +++++++++++++------ .../graphql/language/AstPrinterTest.groovy | 12 ++++-- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy b/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy index 2b687e77e6..d9b5ccff18 100644 --- a/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy +++ b/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy @@ -104,13 +104,11 @@ class IntrospectionResultToSchemaTest extends Specification { then: result == """type QueryType implements Query { - hero( - \"\"\" + hero(\"\"\" comment about episode on two lines \"\"\" - episode: Episode - foo: String = \"bar\"): Character @deprecated(reason: "killed off character") + episode: Episode, foo: String = \"bar\"): Character @deprecated(reason: "killed off character") }""" } @@ -212,9 +210,13 @@ class IntrospectionResultToSchemaTest extends Specification { then: result == """"A character in the Star Wars Trilogy" interface Character { + "The id of the character." id: String! + "The name of the character." name: String + "The friends of the character, or an empty list if they have none." friends: [Character] + "Which movies they appear in." appearsIn: [Episode] }""" @@ -407,22 +409,23 @@ input CharacterInput { } type QueryType { - hero( - "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode." + hero("If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode." episode: Episode): Character - human( - "id of the human" + human("id of the human" id: String!): Human - droid( - "id of the droid" + droid("id of the droid" id: String!): Droid -} +} "A character in the Star Wars Trilogy" interface Character { + "The id of the character." id: String! + "The name of the character." name: String + "The friends of the character, or an empty list if they have none." friends: [Character] + "Which movies they appear in." appearsIn: [Episode] } @@ -438,19 +441,29 @@ enum Episode { "A humanoid creature in the Star Wars universe." type Human implements Character { + "The id of the human." id: String! + "The name of the human." name: String + "The friends of the human, or an empty list if they have none." friends: [Character] + "Which movies they appear in." appearsIn: [Episode] + "The home planet of the human, or null if unknown." homePlanet: String } "A mechanical creature in the Star Wars universe." type Droid implements Character { + "The id of the droid." id: String! + "The name of the droid." name: String + "The friends of the droid, or an empty list if they have none." friends: [Character] + "Which movies they appear in." appearsIn: [Episode] + "The primary function of the droid." primaryFunction: String } """ @@ -975,4 +988,4 @@ scalar EmployeeRef scalar EmployeeRef ''' } -} \ No newline at end of file +} diff --git a/src/test/groovy/graphql/language/AstPrinterTest.groovy b/src/test/groovy/graphql/language/AstPrinterTest.groovy index a1c9f43482..54e5eb2d90 100644 --- a/src/test/groovy/graphql/language/AstPrinterTest.groovy +++ b/src/test/groovy/graphql/language/AstPrinterTest.groovy @@ -474,16 +474,20 @@ type Query { def "print field descriptions"() { def query = '''type Query { - "comment" - field: String + "description" + field( + "description" + a: String): String } ''' def document = parse(query) String output = printAst(document) expect: output == '''type Query { - "comment" - field: String + "description" + field( + "description" + a: String): String } ''' } From 7373fd2b8a6337e78961088361fcc1b24361f20e Mon Sep 17 00:00:00 2001 From: david-castaneda Date: Tue, 26 Apr 2022 12:59:32 -0400 Subject: [PATCH 6/6] fix `IntrospectionResultToSchemaTest.groovy` test --- .../introspection/IntrospectionResultToSchemaTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy b/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy index d9b5ccff18..82c38ce42d 100644 --- a/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy +++ b/src/test/groovy/graphql/introspection/IntrospectionResultToSchemaTest.groovy @@ -415,7 +415,7 @@ type QueryType { id: String!): Human droid("id of the droid" id: String!): Droid -} +} "A character in the Star Wars Trilogy" interface Character {