Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/main/java/graphql/schema/idl/SchemaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
Expand Down Expand Up @@ -122,7 +121,6 @@ public String print(GraphQLSchema schema) {
List<GraphQLType> typesAsList = new ArrayList<>(schema.getAllTypesAsList());
typesAsList.sort(Comparator.comparing(GraphQLType::getName));

printType(out, typesAsList, GraphQLInputType.class, visibility);
printType(out, typesAsList, GraphQLInterfaceType.class, visibility);
printType(out, typesAsList, GraphQLUnionType.class, visibility);
printType(out, typesAsList, GraphQLObjectType.class, visibility);
Expand Down Expand Up @@ -306,7 +304,7 @@ String typeString(GraphQLType rawType) {
}

String argsString(List<GraphQLArgument> arguments) {
boolean hasDescriptions = arguments.stream().filter(arg -> arg.getDescription() != null).count() > 0;
boolean hasDescriptions = arguments.stream().filter(arg -> !isNullOrEmpty(arg.getDescription())).count() > 0;
String prefix = hasDescriptions ? " " : "";
int count = 0;
StringBuilder sb = new StringBuilder();
Expand All @@ -320,7 +318,7 @@ String argsString(List<GraphQLArgument> arguments) {
sb.append("\n");
}
String description = argument.getDescription();
if (description != null) {
if (!isNullOrEmpty(description)) {
Stream<String> stream = Arrays.stream(description.split("\n"));
stream.map(s -> " #" + s + "\n").forEach(sb::append);
}
Expand All @@ -347,9 +345,15 @@ String argsString(List<GraphQLArgument> arguments) {

@SuppressWarnings("unchecked")
private <T> TypePrinter<T> printer(Class<?> clazz) {
TypePrinter typePrinter = printers.computeIfAbsent(clazz,
k -> (out, type, visibility) -> out.println("Type not implemented : " + type)
);
TypePrinter typePrinter = printers.computeIfAbsent(clazz, k -> {
Class<?> superClazz = clazz.getSuperclass();
TypePrinter result;
if (superClazz != Object.class)
result = printer(superClazz);
else
result = (out, type, visibility) -> out.println("Type not implemented : " + type);
return result;
});
return (TypePrinter<T>) typePrinter;
}

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

private void printType(PrintWriter out, List<GraphQLType> typesAsList, Class typeClazz, GraphqlFieldVisibility visibility) {
typesAsList.stream()
.filter(type -> type.getClass().equals(typeClazz))
.filter(type -> typeClazz.isAssignableFrom(type.getClass()))
.forEach(type -> printType(out, type, visibility));
}

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

private void printComments(PrintWriter out, Object graphQLType, String prefix) {
String description = getDescription(graphQLType);
if (description == null) {
if (isNullOrEmpty(description)) {
return;
}
Stream<String> stream = Arrays.stream(description.split("\n"));
Expand Down Expand Up @@ -408,4 +412,8 @@ private String getDescription(Object descriptionHolder) {
return Assert.assertShouldNeverHappen();
}
}

private static boolean isNullOrEmpty(String s) {
return s == null || s.isEmpty();
}
}
40 changes: 40 additions & 0 deletions src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLList
import graphql.schema.GraphQLNonNull
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLOutputType
import graphql.schema.GraphQLScalarType
import graphql.schema.GraphQLSchema
import graphql.schema.GraphQLType
Expand Down Expand Up @@ -86,6 +87,13 @@ class SchemaPrinterTest extends Specification {
schema
}

static class MyGraphQLObjectType extends GraphQLObjectType {

MyGraphQLObjectType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions) {
super(name, description, fieldDefinitions, new ArrayList<GraphQLOutputType>())
}
}

def "typeString"() {

GraphQLType type1 = nonNull(list(nonNull(list(nonNull(Scalars.GraphQLInt)))))
Expand Down Expand Up @@ -267,7 +275,22 @@ type Query {
field: String
}
"""
}

def "does not print empty field description as comment"() {
given:
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
.name("field").description("").type(GraphQLString).build()
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition).build()
def schema = GraphQLSchema.newSchema().query(queryType).build()
when:
def result = new SchemaPrinter().print(schema)

then:
result == """type Query {
field: String
}
"""
}

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

}

def "prints derived object type"() {
given:
GraphQLFieldDefinition fieldDefinition = newFieldDefinition().name("field").type(GraphQLString).build()
def queryType = new MyGraphQLObjectType("Query", "About Query\nSecond Line", Arrays.asList(fieldDefinition))
def schema = GraphQLSchema.newSchema().query(queryType).build()

when:
def result = new SchemaPrinter().print(schema)

then:
result == """#About Query
#Second Line
type Query {
field: String
}
"""
}

}