Skip to content

Commit

Permalink
Fix AstPrinter to print field descriptions (#2808)
Browse files Browse the repository at this point in the history
* Fix AstPrinter to print field descriptions

* consider compact mode

* fix autoformatted lines

* fix autoformatted imports

* fix `IntrospectionResultToSchemaTest.groovy` test

* fix `IntrospectionResultToSchemaTest.groovy` test
  • Loading branch information
david-castaneda committed Apr 27, 2022
1 parent a9a8a29 commit 5e7d856
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/graphql/language/AstPrinter.java
Expand Up @@ -163,7 +163,7 @@ private NodePrinter<FieldDefinition> fieldDefinition() {
final String argSep = compactMode ? "," : ", ";
return (out, 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())
Expand Down
Expand Up @@ -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")
}"""

}
Expand Down Expand Up @@ -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]
}"""

Expand Down Expand Up @@ -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]
}
Expand All @@ -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
}
"""
Expand Down Expand Up @@ -975,4 +988,4 @@ scalar EmployeeRef
scalar EmployeeRef
'''
}
}
}
20 changes: 20 additions & 0 deletions src/test/groovy/graphql/language/AstPrinterTest.groovy
Expand Up @@ -472,6 +472,26 @@ type Query {

}

def "print field descriptions"() {
def query = '''type Query {
"description"
field(
"description"
a: String): String
}
'''
def document = parse(query)
String output = printAst(document)
expect:
output == '''type Query {
"description"
field(
"description"
a: String): String
}
'''
}

def "print empty description"() {
def query = '''
""
Expand Down

0 comments on commit 5e7d856

Please sign in to comment.