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
12 changes: 11 additions & 1 deletion src/main/java/graphql/schema/idl/SchemaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.base.Predicates.not;
import static graphql.Directives.DeprecatedDirective;
import static graphql.introspection.Introspection.DirectiveLocation.ARGUMENT_DEFINITION;
import static graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE;
Expand All @@ -78,6 +79,12 @@ public class SchemaPrinter {
.validLocations(FIELD_DEFINITION, ENUM_VALUE, ARGUMENT_DEFINITION, INPUT_FIELD_DEFINITION)
.build();

/**
* This predicate excludes all directives which are specified bt the GraphQL Specification.
* Printing these directives is optional.
*/
public static final Predicate<GraphQLDirective> ExcludeGraphQLSpecifiedDirectivesPredicate = not(DirectiveInfo::isGraphqlSpecifiedDirective);

/**
* Options to use when printing a schema
*/
Expand Down Expand Up @@ -989,7 +996,10 @@ private void printMultiLineHashDescription(PrintWriter out, String prefix, List<

private void printMultiLineDescription(PrintWriter out, String prefix, List<String> lines) {
out.printf("%s\"\"\"\n", prefix);
lines.forEach(l -> out.printf("%s%s\n", prefix, l));
lines.forEach(l -> {
String escapedTripleQuotes = l.replaceAll("\"\"\"", "\\\\\"\"\"");
out.printf("%s%s\n", prefix, escapedTripleQuotes);
});
out.printf("%s\"\"\"\n", prefix);
}

Expand Down
29 changes: 24 additions & 5 deletions src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import static graphql.schema.GraphQLNonNull.nonNull
import static graphql.schema.GraphQLObjectType.newObject
import static graphql.schema.GraphQLScalarType.newScalar
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring
import static graphql.schema.idl.SchemaPrinter.ExcludeGraphQLSpecifiedDirectivesPredicate
import static graphql.schema.idl.SchemaPrinter.Options.defaultOptions

class SchemaPrinterTest extends Specification {
Expand Down Expand Up @@ -75,7 +76,7 @@ class SchemaPrinterTest extends Specification {
throw new UnsupportedOperationException("Not implemented")
}
})
.build()
.build()

def resolver = new TypeResolver() {

Expand Down Expand Up @@ -514,7 +515,7 @@ type Query {

def "prints scalar description as comment"() {
given:
GraphQLScalarType myScalar = newScalar().name("Scalar").description( "about scalar").coercing(new Coercing() {
GraphQLScalarType myScalar = newScalar().name("Scalar").description("about scalar").coercing(new Coercing() {
@Override
Object serialize(Object input) {
return null
Expand All @@ -530,7 +531,7 @@ type Query {
return null
}
})
.build()
.build()
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
.name("field").type(myScalar).build()
def queryType = newObject().name("Query").field(fieldDefinition).build()
Expand Down Expand Up @@ -1648,7 +1649,7 @@ scalar CustomScalar
return null
}
})
.build()
.build()

GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
.name("scalarType").type(myScalar).build()
Expand Down Expand Up @@ -1690,7 +1691,7 @@ scalar RandomScalar
return null
}
})
.build()
.build()

GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
.name("someType").type(GraphQLInt).build()
Expand Down Expand Up @@ -1907,4 +1908,22 @@ type MyQuery {
result == """directive @foo on FIELD_DEFINITION"""
}

def "description printing escapes triple quotes"() {
def descriptionWithTripleQuote = 'Hello """ \n World """ """';
def field = newFieldDefinition().name("hello").type(GraphQLString).build()
def queryType = newObject().name("Query").field(field).description(descriptionWithTripleQuote).build()
def schema = GraphQLSchema.newSchema().query(queryType).build()
when:
def result = new SchemaPrinter(defaultOptions().includeDirectives(ExcludeGraphQLSpecifiedDirectivesPredicate)).print(schema)

then:
result == '''"""
Hello \\"""
World \\""" \\"""
"""
type Query {
hello: String
}
'''
}
}