Skip to content

Commit 0f006c4

Browse files
andimarekbbakerman
authored andcommitted
print enum descriptions
1 parent dcaa999 commit 0f006c4

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ private TypePrinter<GraphQLEnumType> enumPrinter() {
148148
if (isIntrospectionType(type)) {
149149
return;
150150
}
151+
printComments(out, type, "");
151152
out.format("enum %s {\n", type.getName());
152153
for (GraphQLEnumValueDefinition enumValueDefinition : type.getValues()) {
154+
printComments(out, enumValueDefinition, " ");
153155
out.format(" %s\n", enumValueDefinition.getName());
154156
}
155157
out.format("}\n\n");
@@ -193,7 +195,7 @@ private TypePrinter<GraphQLObjectType> objectPrinter() {
193195
if (isIntrospectionType(type)) {
194196
return;
195197
}
196-
printComments(out, type);
198+
printComments(out, type, "");
197199
out.format("type %s {\n", type.getName());
198200
type.getFieldDefinitions().forEach(fd -> {
199201
printComments(out, fd, " ");
@@ -335,27 +337,25 @@ private void printType(PrintWriter out, GraphQLType type) {
335337
printer.print(out, type);
336338
}
337339

338-
void printComments(PrintWriter out, GraphQLFieldDefinition fieldDefinition, String prefix) {
339-
String description = fieldDefinition.getDescription();
340-
if (description == null) {
341-
return;
342-
}
343-
Stream<String> stream = Arrays.stream(description.split("\n"));
344-
stream.map(s -> prefix + "#" + s + "\n").forEach(out::write);
345-
}
346340

347-
void printComments(PrintWriter out, GraphQLType graphQLType) {
341+
private void printComments(PrintWriter out, Object graphQLType, String prefix) {
348342
String description = getDescription(graphQLType);
349343
if (description == null) {
350344
return;
351345
}
352346
Stream<String> stream = Arrays.stream(description.split("\n"));
353-
stream.map(s -> "#" + s + "\n").forEach(out::write);
347+
stream.map(s -> prefix + "#" + s + "\n").forEach(out::write);
354348
}
355349

356-
String getDescription(GraphQLType graphQLType) {
357-
if (graphQLType instanceof GraphQLObjectType) {
358-
return ((GraphQLObjectType) graphQLType).getDescription();
350+
private String getDescription(Object descriptionHolder) {
351+
if (descriptionHolder instanceof GraphQLObjectType) {
352+
return ((GraphQLObjectType) descriptionHolder).getDescription();
353+
} else if (descriptionHolder instanceof GraphQLEnumType) {
354+
return ((GraphQLEnumType) descriptionHolder).getDescription();
355+
} else if (descriptionHolder instanceof GraphQLFieldDefinition) {
356+
return ((GraphQLFieldDefinition) descriptionHolder).getDescription();
357+
} else if (descriptionHolder instanceof GraphQLEnumValueDefinition) {
358+
return ((GraphQLEnumValueDefinition) descriptionHolder).getDescription();
359359
} else {
360360
return Assert.assertShouldNeverHappen();
361361
}

src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import graphql.Scalars
44
import graphql.TypeResolutionEnvironment
55
import graphql.schema.Coercing
66
import graphql.schema.GraphQLArgument
7+
import graphql.schema.GraphQLEnumType
78
import graphql.schema.GraphQLFieldDefinition
89
import graphql.schema.GraphQLList
910
import graphql.schema.GraphQLNonNull
@@ -260,6 +261,35 @@ type Query {
260261
field : String
261262
}
262263
264+
"""
265+
266+
}
267+
268+
def "prints enum description as comment"() {
269+
given:
270+
GraphQLEnumType graphQLEnumType = GraphQLEnumType.newEnum()
271+
.name("Enum")
272+
.description("About enum")
273+
.value("value", "value", "value desc")
274+
.build()
275+
GraphQLFieldDefinition fieldDefinition = GraphQLFieldDefinition.newFieldDefinition()
276+
.name("field").type(graphQLEnumType).build()
277+
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition).build()
278+
def schema = GraphQLSchema.newSchema().query(queryType).build()
279+
when:
280+
def result = new SchemaPrinter().print(schema)
281+
282+
then:
283+
result == """type Query {
284+
field : Enum
285+
}
286+
287+
#About enum
288+
enum Enum {
289+
#value desc
290+
value
291+
}
292+
263293
"""
264294

265295
}

0 commit comments

Comments
 (0)