Skip to content

Commit 465271b

Browse files
authored
Pr 600 michaelplavnik schema printer improvments (#648)
* Allow subclasses of the types to be printed. * Replace '-' character with one from cp1252 Documentaiton build on windows failed as '-' character was not from cp1252 code page (first 127 codes). * Support instances of classes derived from schema classes 1. graphql-spqr generates schema out of GraphQL types subclasess and changes in this commit handle it correctly. 2. Empty discriptions are not printed out. * Fixed * imports
1 parent bb8c54c commit 465271b

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import graphql.schema.GraphQLFieldDefinition;
99
import graphql.schema.GraphQLInputObjectField;
1010
import graphql.schema.GraphQLInputObjectType;
11-
import graphql.schema.GraphQLInputType;
1211
import graphql.schema.GraphQLInterfaceType;
1312
import graphql.schema.GraphQLList;
1413
import graphql.schema.GraphQLNonNull;
@@ -122,7 +121,6 @@ public String print(GraphQLSchema schema) {
122121
List<GraphQLType> typesAsList = new ArrayList<>(schema.getAllTypesAsList());
123122
typesAsList.sort(Comparator.comparing(GraphQLType::getName));
124123

125-
printType(out, typesAsList, GraphQLInputType.class, visibility);
126124
printType(out, typesAsList, GraphQLInterfaceType.class, visibility);
127125
printType(out, typesAsList, GraphQLUnionType.class, visibility);
128126
printType(out, typesAsList, GraphQLObjectType.class, visibility);
@@ -306,7 +304,7 @@ String typeString(GraphQLType rawType) {
306304
}
307305

308306
String argsString(List<GraphQLArgument> arguments) {
309-
boolean hasDescriptions = arguments.stream().filter(arg -> arg.getDescription() != null).count() > 0;
307+
boolean hasDescriptions = arguments.stream().filter(arg -> !isNullOrEmpty(arg.getDescription())).count() > 0;
310308
String prefix = hasDescriptions ? " " : "";
311309
int count = 0;
312310
StringBuilder sb = new StringBuilder();
@@ -320,7 +318,7 @@ String argsString(List<GraphQLArgument> arguments) {
320318
sb.append("\n");
321319
}
322320
String description = argument.getDescription();
323-
if (description != null) {
321+
if (!isNullOrEmpty(description)) {
324322
Stream<String> stream = Arrays.stream(description.split("\n"));
325323
stream.map(s -> " #" + s + "\n").forEach(sb::append);
326324
}
@@ -347,9 +345,15 @@ String argsString(List<GraphQLArgument> arguments) {
347345

348346
@SuppressWarnings("unchecked")
349347
private <T> TypePrinter<T> printer(Class<?> clazz) {
350-
TypePrinter typePrinter = printers.computeIfAbsent(clazz,
351-
k -> (out, type, visibility) -> out.println("Type not implemented : " + type)
352-
);
348+
TypePrinter typePrinter = printers.computeIfAbsent(clazz, k -> {
349+
Class<?> superClazz = clazz.getSuperclass();
350+
TypePrinter result;
351+
if (superClazz != Object.class)
352+
result = printer(superClazz);
353+
else
354+
result = (out, type, visibility) -> out.println("Type not implemented : " + type);
355+
return result;
356+
});
353357
return (TypePrinter<T>) typePrinter;
354358
}
355359

@@ -364,7 +368,7 @@ public String print(GraphQLType type) {
364368

365369
private void printType(PrintWriter out, List<GraphQLType> typesAsList, Class typeClazz, GraphqlFieldVisibility visibility) {
366370
typesAsList.stream()
367-
.filter(type -> type.getClass().equals(typeClazz))
371+
.filter(type -> typeClazz.isAssignableFrom(type.getClass()))
368372
.forEach(type -> printType(out, type, visibility));
369373
}
370374

@@ -376,7 +380,7 @@ private void printType(PrintWriter out, GraphQLType type, GraphqlFieldVisibility
376380

377381
private void printComments(PrintWriter out, Object graphQLType, String prefix) {
378382
String description = getDescription(graphQLType);
379-
if (description == null) {
383+
if (isNullOrEmpty(description)) {
380384
return;
381385
}
382386
Stream<String> stream = Arrays.stream(description.split("\n"));
@@ -408,4 +412,8 @@ private String getDescription(Object descriptionHolder) {
408412
return Assert.assertShouldNeverHappen();
409413
}
410414
}
415+
416+
private static boolean isNullOrEmpty(String s) {
417+
return s == null || s.isEmpty();
418+
}
411419
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import graphql.schema.GraphQLInterfaceType
1212
import graphql.schema.GraphQLList
1313
import graphql.schema.GraphQLNonNull
1414
import graphql.schema.GraphQLObjectType
15+
import graphql.schema.GraphQLOutputType
1516
import graphql.schema.GraphQLScalarType
1617
import graphql.schema.GraphQLSchema
1718
import graphql.schema.GraphQLType
@@ -86,6 +87,13 @@ class SchemaPrinterTest extends Specification {
8687
schema
8788
}
8889

90+
static class MyGraphQLObjectType extends GraphQLObjectType {
91+
92+
MyGraphQLObjectType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions) {
93+
super(name, description, fieldDefinitions, new ArrayList<GraphQLOutputType>())
94+
}
95+
}
96+
8997
def "typeString"() {
9098

9199
GraphQLType type1 = nonNull(list(nonNull(list(nonNull(Scalars.GraphQLInt)))))
@@ -267,7 +275,22 @@ type Query {
267275
field: String
268276
}
269277
"""
278+
}
279+
280+
def "does not print empty field description as comment"() {
281+
given:
282+
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
283+
.name("field").description("").type(GraphQLString).build()
284+
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition).build()
285+
def schema = GraphQLSchema.newSchema().query(queryType).build()
286+
when:
287+
def result = new SchemaPrinter().print(schema)
270288

289+
then:
290+
result == """type Query {
291+
field: String
292+
}
293+
"""
271294
}
272295

273296
def "prints enum description as comment"() {
@@ -489,5 +512,22 @@ scalar Scalar
489512

490513
}
491514

515+
def "prints derived object type"() {
516+
given:
517+
GraphQLFieldDefinition fieldDefinition = newFieldDefinition().name("field").type(GraphQLString).build()
518+
def queryType = new MyGraphQLObjectType("Query", "About Query\nSecond Line", Arrays.asList(fieldDefinition))
519+
def schema = GraphQLSchema.newSchema().query(queryType).build()
520+
521+
when:
522+
def result = new SchemaPrinter().print(schema)
523+
524+
then:
525+
result == """#About Query
526+
#Second Line
527+
type Query {
528+
field: String
529+
}
530+
"""
531+
}
492532

493533
}

0 commit comments

Comments
 (0)