Skip to content

Commit cf10927

Browse files
andimarekbbakerman
authored andcommitted
print input descriptions
1 parent 2fd253d commit cf10927

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.schema.GraphQLEnumType;
66
import graphql.schema.GraphQLEnumValueDefinition;
77
import graphql.schema.GraphQLFieldDefinition;
8+
import graphql.schema.GraphQLInputObjectField;
89
import graphql.schema.GraphQLInputObjectType;
910
import graphql.schema.GraphQLInputType;
1011
import graphql.schema.GraphQLInterfaceType;
@@ -118,6 +119,7 @@ public String print(GraphQLSchema schema) {
118119
printType(out, typesAsList, GraphQLObjectType.class);
119120
printType(out, typesAsList, GraphQLEnumType.class);
120121
printType(out, typesAsList, GraphQLScalarType.class);
122+
printType(out, typesAsList, GraphQLInputObjectType.class);
121123

122124
return sw.toString();
123125
}
@@ -213,10 +215,13 @@ private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() {
213215
if (isIntrospectionType(type)) {
214216
return;
215217
}
218+
printComments(out, type, "");
216219
out.format("input %s {\n", type.getName());
217-
type.getFieldDefinitions().forEach(fd ->
218-
out.format(" %s : %s\n",
219-
fd.getName(), typeString(fd.getType())));
220+
type.getFieldDefinitions().forEach(fd -> {
221+
printComments(out, fd, " ");
222+
out.format(" %s : %s\n",
223+
fd.getName(), typeString(fd.getType()));
224+
});
220225
out.format("}\n\n");
221226
};
222227
}
@@ -359,6 +364,10 @@ private String getDescription(Object descriptionHolder) {
359364
return ((GraphQLEnumValueDefinition) descriptionHolder).getDescription();
360365
} else if (descriptionHolder instanceof GraphQLUnionType) {
361366
return ((GraphQLUnionType) descriptionHolder).getDescription();
367+
} else if (descriptionHolder instanceof GraphQLInputObjectType) {
368+
return ((GraphQLInputObjectType) descriptionHolder).getDescription();
369+
} else if (descriptionHolder instanceof GraphQLInputObjectField) {
370+
return ((GraphQLInputObjectField) descriptionHolder).getDescription();
362371
} else {
363372
return Assert.assertShouldNeverHappen();
364373
}

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import graphql.schema.Coercing
66
import graphql.schema.GraphQLArgument
77
import graphql.schema.GraphQLEnumType
88
import graphql.schema.GraphQLFieldDefinition
9+
import graphql.schema.GraphQLInputObjectType
10+
import graphql.schema.GraphQLInputType
911
import graphql.schema.GraphQLList
1012
import graphql.schema.GraphQLNonNull
1113
import graphql.schema.GraphQLObjectType
@@ -18,7 +20,10 @@ import spock.lang.Specification
1820

1921
import java.util.function.UnaryOperator
2022

23+
import static graphql.Scalars.GraphQLString
24+
import static graphql.schema.GraphQLArgument.newArgument
2125
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
26+
import static graphql.schema.GraphQLInputObjectField.newInputObjectField
2227

2328
class SchemaPrinterTest extends Specification {
2429

@@ -95,8 +100,8 @@ class SchemaPrinterTest extends Specification {
95100

96101
def "argsString"() {
97102
def argument1 = new GraphQLArgument("arg1", "desc-arg1", list(nonNull(Scalars.GraphQLInt)), 10)
98-
def argument2 = new GraphQLArgument("arg2", "desc-arg2", Scalars.GraphQLString, null)
99-
def argument3 = new GraphQLArgument("arg3", "desc-arg3", Scalars.GraphQLString, "default")
103+
def argument2 = new GraphQLArgument("arg2", "desc-arg2", GraphQLString, null)
104+
def argument3 = new GraphQLArgument("arg3", "desc-arg3", GraphQLString, "default")
100105
def argStr = new SchemaPrinter().argsString([argument1, argument2, argument3])
101106

102107
expect:
@@ -232,7 +237,7 @@ type Subscription {
232237
def "prints object description as comment"() {
233238
given:
234239
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
235-
.name("field").type(Scalars.GraphQLString).build()
240+
.name("field").type(GraphQLString).build()
236241
def queryType = GraphQLObjectType.newObject().name("Query").description("About Query\nSecond Line").field(fieldDefinition).build()
237242
def schema = GraphQLSchema.newSchema().query(queryType).build()
238243
when:
@@ -251,7 +256,7 @@ type Query {
251256
def "prints field description as comment"() {
252257
given:
253258
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
254-
.name("field").description("About field\nsecond").type(Scalars.GraphQLString).build()
259+
.name("field").description("About field\nsecond").type(GraphQLString).build()
255260
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition).build()
256261
def schema = GraphQLSchema.newSchema().query(queryType).build()
257262
when:
@@ -300,7 +305,7 @@ enum Enum {
300305
def "prints union description as comment"() {
301306
given:
302307
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
303-
.name("field").type(Scalars.GraphQLString).build()
308+
.name("field").type(GraphQLString).build()
304309
def possibleType = GraphQLObjectType.newObject().name("PossibleType").field(fieldDefinition).build()
305310
GraphQLUnionType unionType = GraphQLUnionType.newUnionType()
306311
.name("Union")
@@ -333,10 +338,10 @@ type Query {
333338

334339
def "prints union"() {
335340
def possibleType1 = GraphQLObjectType.newObject().name("PossibleType1").field(
336-
newFieldDefinition().name("field").type(Scalars.GraphQLString).build()
341+
newFieldDefinition().name("field").type(GraphQLString).build()
337342
).build()
338343
def possibleType2 = GraphQLObjectType.newObject().name("PossibleType2").field(
339-
newFieldDefinition().name("field").type(Scalars.GraphQLString).build()
344+
newFieldDefinition().name("field").type(GraphQLString).build()
340345
).build()
341346
GraphQLUnionType unionType = GraphQLUnionType.newUnionType()
342347
.name("Union")
@@ -370,4 +375,36 @@ type Query {
370375

371376
}
372377

378+
def "prints input description as comment"() {
379+
given:
380+
GraphQLInputType inputType = GraphQLInputObjectType.newInputObject()
381+
.name("Input")
382+
.field(newInputObjectField().name("field").description("about field").type(GraphQLString).build())
383+
.description("About input")
384+
.build()
385+
GraphQLFieldDefinition fieldDefinition2 = newFieldDefinition()
386+
.name("field")
387+
.argument(newArgument().name("arg").type(inputType).build())
388+
.type(GraphQLString).build()
389+
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition2).build()
390+
def schema = GraphQLSchema.newSchema().query(queryType).build()
391+
when:
392+
def result = new SchemaPrinter().print(schema)
393+
394+
then:
395+
result == """type Query {
396+
field(arg : Input) : String
397+
}
398+
399+
#About input
400+
input Input {
401+
#about field
402+
field : String
403+
}
404+
405+
"""
406+
407+
}
408+
409+
373410
}

0 commit comments

Comments
 (0)