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
17 changes: 13 additions & 4 deletions src/main/java/graphql/schema/idl/SchemaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,19 @@ private List<GraphQLDirective> addDeprecatedDirectiveIfNeeded(List<GraphQLDirect

private String directiveDefinitions(List<GraphQLDirective> directives) {
StringBuilder sb = new StringBuilder();
directives.stream().filter(options.getIncludeSchemaElement()).forEach(directive -> {
sb.append(directiveDefinition(directive));
sb.append("\n\n");
});
GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
.parentType(GraphQLSchemaElement.class)
.elementType(GraphQLDirective.class)
.build();
Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);
directives
.stream()
.sorted(comparator)
.filter(options.getIncludeSchemaElement())
.forEach(directive -> {
sb.append(directiveDefinition(directive));
sb.append("\n\n");
});
return sb.toString();
}

Expand Down
20 changes: 10 additions & 10 deletions src/test/groovy/graphql/Issue2141.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ class Issue2141 extends Specification {
def schemaStr = new SchemaPrinter().print(schema)

then:
schemaStr == """"Directs the executor to include this field or fragment only when the `if` argument is true"
schemaStr == """directive @auth(roles: [String!]) on FIELD_DEFINITION

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
Expand All @@ -34,14 +42,6 @@ directive @skip(
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @auth(roles: [String!]) on FIELD_DEFINITION

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
Expand All @@ -53,4 +53,4 @@ type Query {
}
"""
}
}
}
156 changes: 78 additions & 78 deletions src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -892,66 +892,66 @@ type Query {

then:
// args and directives are sorted like the rest of the schema printer
result == '''"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"Directs the executor to skip this field or fragment when the `if`'argument is true."
directive @skip(
"Skipped when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
result == '''directive @argDirective on ARGUMENT_DEFINITION

directive @interfaceFieldDirective on FIELD_DEFINITION

directive @unionTypeDirective on UNION
"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

directive @query1 repeatable on OBJECT
directive @enumTypeDirective on ENUM

directive @query2(arg1: String) on OBJECT
directive @enumValueDirective on ENUM_VALUE

directive @fieldDirective1 on FIELD_DEFINITION

directive @fieldDirective2(argBool: Boolean, argFloat: Float, argInt: Int, argStr: String) on FIELD_DEFINITION

directive @argDirective on ARGUMENT_DEFINITION

directive @interfaceImplementingTypeDirective on OBJECT
"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @enumTypeDirective on ENUM
directive @inputFieldDirective on INPUT_FIELD_DEFINITION

directive @single on OBJECT
directive @inputTypeDirective on INPUT_OBJECT

directive @singleField on FIELD_DEFINITION
directive @interfaceFieldDirective on FIELD_DEFINITION

directive @interfaceImplementingFieldDirective on FIELD_DEFINITION

directive @enumValueDirective on ENUM_VALUE
directive @interfaceImplementingTypeDirective on OBJECT

directive @inputTypeDirective on INPUT_OBJECT
directive @interfaceTypeDirective on INTERFACE

directive @inputFieldDirective on INPUT_FIELD_DEFINITION
directive @query1 repeatable on OBJECT

directive @interfaceTypeDirective on INTERFACE
directive @query2(arg1: String) on OBJECT

directive @repeatableDirective repeatable on SCALAR

directive @scalarDirective on SCALAR

directive @repeatableDirective repeatable on SCALAR
directive @single on OBJECT

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
directive @singleField on FIELD_DEFINITION

"Directs the executor to skip this field or fragment when the `if`'argument is true."
directive @skip(
"Skipped when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
url: String!
) on SCALAR

directive @unionTypeDirective on UNION

interface SomeInterface @interfaceTypeDirective {
fieldA: String @interfaceFieldDirective
}
Expand Down Expand Up @@ -1067,7 +1067,13 @@ input SomeInput {

then:
// args and directives are sorted like the rest of the schema printer
result == '''"Directs the executor to include this field or fragment only when the `if` argument is true"
result == '''"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
Expand All @@ -1079,12 +1085,6 @@ directive @skip(
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
Expand Down Expand Up @@ -1160,28 +1160,28 @@ type Query {
def resultWithDirectives = new SchemaPrinter(defaultOptions().includeDirectives(true)).print(schema)

then:
resultWithDirectives == '''"Directs the executor to include this field or fragment only when the `if` argument is true"
resultWithDirectives == '''"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

directive @example on FIELD_DEFINITION

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @moreComplex(arg1: String = "default", arg2: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION

"Directs the executor to skip this field or fragment when the `if`'argument is true."
directive @skip(
"Skipped when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @example on FIELD_DEFINITION

directive @moreComplex(arg1: String = "default", arg2: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
Expand Down Expand Up @@ -1225,28 +1225,28 @@ type Query {
def resultWithDirectiveDefinitions = new SchemaPrinter(defaultOptions().includeDirectiveDefinitions(true)).print(schema)

then:
resultWithDirectiveDefinitions == '''"Directs the executor to include this field or fragment only when the `if` argument is true"
resultWithDirectiveDefinitions == '''"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

directive @example on FIELD_DEFINITION

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @moreComplex(arg1: String = "default", arg2: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION

"Directs the executor to skip this field or fragment when the `if`'argument is true."
directive @skip(
"Skipped when true."
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @example on FIELD_DEFINITION

directive @moreComplex(arg1: String = "default", arg2: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
Expand Down Expand Up @@ -1359,7 +1359,15 @@ type Query {
def result = new SchemaPrinter(printOptions).print(schema)

then:
result == '''"Directs the executor to include this field or fragment only when the `if` argument is true"
result == '''"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

directive @directive1 on SCALAR

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
if: Boolean!
Expand All @@ -1371,14 +1379,6 @@ directive @skip(
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @directive1 on SCALAR

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
Expand Down Expand Up @@ -1862,6 +1862,14 @@ type PrintMeType {
query: MyQuery
}

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

directive @foo on SCHEMA

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
"Included when true."
Expand All @@ -1874,14 +1882,6 @@ directive @skip(
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @foo on SCHEMA

"Marks the field, argument, input field or enum value as deprecated"
directive @deprecated(
"The reason for the deprecation"
reason: String = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
"The URL that specifies the behaviour of this scalar."
Expand Down