Skip to content

Commit 6c75035

Browse files
committed
Directive definitions now have a predicate and fixed up tests
1 parent 3f7e8ae commit 6c75035

3 files changed

Lines changed: 107 additions & 16 deletions

File tree

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

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public static class Options {
9898

9999
private final boolean descriptionsAsHashComments;
100100

101+
private final Predicate<String> includeDirectiveDefinition;
102+
101103
private final Predicate<String> includeDirective;
102104

103105
private final Predicate<GraphQLSchemaElement> includeSchemaElement;
@@ -110,6 +112,7 @@ private Options(boolean includeIntrospectionTypes,
110112
boolean includeScalars,
111113
boolean includeSchemaDefinition,
112114
boolean includeDirectiveDefinitions,
115+
Predicate<String> includeDirectiveDefinition,
113116
boolean useAstDefinitions,
114117
boolean descriptionsAsHashComments,
115118
Predicate<String> includeDirective,
@@ -120,6 +123,7 @@ private Options(boolean includeIntrospectionTypes,
120123
this.includeScalars = includeScalars;
121124
this.includeSchemaDefinition = includeSchemaDefinition;
122125
this.includeDirectiveDefinitions = includeDirectiveDefinitions;
126+
this.includeDirectiveDefinition = includeDirectiveDefinition;
123127
this.includeDirective = includeDirective;
124128
this.useAstDefinitions = useAstDefinitions;
125129
this.descriptionsAsHashComments = descriptionsAsHashComments;
@@ -144,6 +148,10 @@ public boolean isIncludeDirectiveDefinitions() {
144148
return includeDirectiveDefinitions;
145149
}
146150

151+
public Predicate<String> getIncludeDirectiveDefinition() {
152+
return includeDirectiveDefinition;
153+
}
154+
147155
public Predicate<String> getIncludeDirective() {
148156
return includeDirective;
149157
}
@@ -164,14 +172,16 @@ public boolean isUseAstDefinitions() {
164172
return useAstDefinitions;
165173
}
166174

167-
public boolean isIncludeAstDefinitionComments() { return includeAstDefinitionComments; }
175+
public boolean isIncludeAstDefinitionComments() {
176+
return includeAstDefinitionComments;
177+
}
168178

169179
public static Options defaultOptions() {
170180
return new Options(false,
171181
true,
172182
false,
173183
true,
174-
false,
184+
directive -> true, false,
175185
false,
176186
directive -> true,
177187
element -> true,
@@ -191,7 +201,7 @@ public Options includeIntrospectionTypes(boolean flag) {
191201
this.includeScalars,
192202
this.includeSchemaDefinition,
193203
this.includeDirectiveDefinitions,
194-
this.useAstDefinitions,
204+
this.includeDirectiveDefinition, this.useAstDefinitions,
195205
this.descriptionsAsHashComments,
196206
this.includeDirective,
197207
this.includeSchemaElement,
@@ -211,7 +221,7 @@ public Options includeScalarTypes(boolean flag) {
211221
flag,
212222
this.includeSchemaDefinition,
213223
this.includeDirectiveDefinitions,
214-
this.useAstDefinitions,
224+
this.includeDirectiveDefinition, this.useAstDefinitions,
215225
this.descriptionsAsHashComments,
216226
this.includeDirective,
217227
this.includeSchemaElement,
@@ -234,6 +244,7 @@ public Options includeSchemaDefinition(boolean flag) {
234244
this.includeScalars,
235245
flag,
236246
this.includeDirectiveDefinitions,
247+
this.includeDirectiveDefinition,
237248
this.useAstDefinitions,
238249
this.descriptionsAsHashComments,
239250
this.includeDirective,
@@ -259,6 +270,29 @@ public Options includeDirectiveDefinitions(boolean flag) {
259270
this.includeScalars,
260271
this.includeSchemaDefinition,
261272
flag,
273+
directive -> flag,
274+
this.useAstDefinitions,
275+
this.descriptionsAsHashComments,
276+
this.includeDirective,
277+
this.includeSchemaElement,
278+
this.comparatorRegistry,
279+
this.includeAstDefinitionComments);
280+
}
281+
282+
283+
/**
284+
* This is a Predicate that decides whether a directive definition is printed.
285+
*
286+
* @param includeDirectiveDefinition the predicate to decide of a directive defintion is printed
287+
*
288+
* @return new instance of options
289+
*/
290+
public Options includeDirectiveDefinition(Predicate<String> includeDirectiveDefinition) {
291+
return new Options(this.includeIntrospectionTypes,
292+
this.includeScalars,
293+
this.includeSchemaDefinition,
294+
this.includeDirectiveDefinitions,
295+
includeDirectiveDefinition,
262296
this.useAstDefinitions,
263297
this.descriptionsAsHashComments,
264298
this.includeDirective,
@@ -280,6 +314,7 @@ public Options includeDirectives(boolean flag) {
280314
this.includeScalars,
281315
this.includeSchemaDefinition,
282316
this.includeDirectiveDefinitions,
317+
this.includeDirectiveDefinition,
283318
this.useAstDefinitions,
284319
this.descriptionsAsHashComments,
285320
directive -> flag,
@@ -300,6 +335,7 @@ public Options includeDirectives(Predicate<String> includeDirective) {
300335
this.includeScalars,
301336
this.includeSchemaDefinition,
302337
this.includeDirectiveDefinitions,
338+
this.includeDirectiveDefinition,
303339
this.useAstDefinitions,
304340
this.descriptionsAsHashComments,
305341
includeDirective,
@@ -308,6 +344,7 @@ public Options includeDirectives(Predicate<String> includeDirective) {
308344
this.includeAstDefinitionComments);
309345
}
310346

347+
311348
/**
312349
* This is a general purpose Predicate that decides whether a schema element is printed ever.
313350
*
@@ -321,6 +358,7 @@ public Options includeSchemaElement(Predicate<GraphQLSchemaElement> includeSchem
321358
this.includeScalars,
322359
this.includeSchemaDefinition,
323360
this.includeDirectiveDefinitions,
361+
this.includeDirectiveDefinition,
324362
this.useAstDefinitions,
325363
this.descriptionsAsHashComments,
326364
this.includeDirective,
@@ -342,6 +380,7 @@ public Options useAstDefinitions(boolean flag) {
342380
this.includeScalars,
343381
this.includeSchemaDefinition,
344382
this.includeDirectiveDefinitions,
383+
this.includeDirectiveDefinition,
345384
flag,
346385
this.descriptionsAsHashComments,
347386
this.includeDirective,
@@ -365,6 +404,7 @@ public Options descriptionsAsHashComments(boolean flag) {
365404
this.includeScalars,
366405
this.includeSchemaDefinition,
367406
this.includeDirectiveDefinitions,
407+
this.includeDirectiveDefinition,
368408
this.useAstDefinitions,
369409
flag,
370410
this.includeDirective,
@@ -387,6 +427,7 @@ public Options setComparators(GraphqlTypeComparatorRegistry comparatorRegistry)
387427
this.includeScalars,
388428
this.includeSchemaDefinition,
389429
this.includeDirectiveDefinitions,
430+
this.includeDirectiveDefinition,
390431
this.useAstDefinitions,
391432
this.descriptionsAsHashComments,
392433
this.includeDirective,
@@ -409,6 +450,7 @@ public Options includeAstDefinitionComments(boolean flag) {
409450
this.includeScalars,
410451
this.includeSchemaDefinition,
411452
this.includeDirectiveDefinitions,
453+
this.includeDirectiveDefinition,
412454
this.useAstDefinitions,
413455
this.descriptionsAsHashComments,
414456
this.includeDirective,
@@ -638,7 +680,9 @@ private SchemaElementPrinter<GraphQLUnionType> unionPrinter() {
638680

639681
private SchemaElementPrinter<GraphQLDirective> directivePrinter() {
640682
return (out, directive, visibility) -> {
641-
if (options.isIncludeDirectiveDefinitions()) {
683+
boolean isOnEver = options.isIncludeDirectiveDefinitions();
684+
boolean specificTest = options.getIncludeDirectiveDefinition().test(directive.getName());
685+
if (isOnEver && specificTest) {
642686
String s = directiveDefinition(directive);
643687
out.format("%s", s);
644688
out.print("\n\n");
@@ -964,14 +1008,14 @@ private boolean hasDeprecatedDirective(List<GraphQLAppliedDirective> directives)
9641008

9651009
private List<GraphQLAppliedDirective> addDeprecatedDirectiveIfNeeded(GraphQLDirectiveContainer directiveContainer) {
9661010
List<GraphQLAppliedDirective> directives = DirectivesUtil.toAppliedDirectives(directiveContainer);
967-
if (!hasDeprecatedDirective(directives) && isDeprecatedDirectiveAllowed()) {
1011+
if (!hasDeprecatedDirective(directives) && isDeprecatedDirectiveAllowed()) {
9681012
directives = new ArrayList<>(directives);
969-
String reason = getDeprecationReason(directiveContainer);
970-
GraphQLAppliedDirectiveArgument arg = GraphQLAppliedDirectiveArgument.newArgument()
971-
.name("reason")
972-
.valueProgrammatic(reason)
973-
.type(GraphQLString)
974-
.build();
1013+
String reason = getDeprecationReason(directiveContainer);
1014+
GraphQLAppliedDirectiveArgument arg = GraphQLAppliedDirectiveArgument.newArgument()
1015+
.name("reason")
1016+
.valueProgrammatic(reason)
1017+
.type(GraphQLString)
1018+
.build();
9751019
GraphQLAppliedDirective directive = GraphQLAppliedDirective.newDirective()
9761020
.name("deprecated")
9771021
.argument(arg)
@@ -1108,7 +1152,7 @@ private void printComments(PrintWriter out, Object graphQLType, String prefix) {
11081152
if (options.isIncludeAstDefinitionComments()) {
11091153
String commentsText = getAstDefinitionComments(graphQLType);
11101154
if (!isNullOrEmpty(commentsText)) {
1111-
List<String> lines = Arrays.asList(commentsText.split("\n") );
1155+
List<String> lines = Arrays.asList(commentsText.split("\n"));
11121156
if (!lines.isEmpty()) {
11131157
printMultiLineHashDescription(out, prefix, lines);
11141158
}
@@ -1183,7 +1227,7 @@ private String getAstDefinitionComments(Object commentHolder) {
11831227
}
11841228

11851229
private String comments(List<Comment> comments) {
1186-
if ( comments == null || comments.isEmpty() ) {
1230+
if (comments == null || comments.isEmpty()) {
11871231
return null;
11881232
}
11891233
String s = comments.stream().map(c -> c.getContent()).collect(joining("\n", "", "\n"));

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2710,7 +2710,7 @@ input Gun {
27102710
.build()
27112711
when:
27122712

2713-
def printOptions = defaultOptions().includeDirectives({d -> true})
2713+
def printOptions = defaultOptions().includeDirectiveDefinitions(false).includeDirectives({ d -> true })
27142714

27152715
def result = "\n" + new SchemaPrinter(printOptions).print(schema)
27162716
println(result)
@@ -2734,4 +2734,49 @@ input Input {
27342734
}
27352735
"""
27362736
}
2737+
2738+
def "can use predicate for directive definitions"() {
2739+
2740+
def schema = TestUtil.schema("""
2741+
type Query {
2742+
field: String @deprecated
2743+
}
2744+
""")
2745+
2746+
2747+
def options = defaultOptions()
2748+
.includeDirectiveDefinitions(true)
2749+
.includeDirectiveDefinition({ it != "skip" })
2750+
def result = new SchemaPrinter(options).print(schema)
2751+
2752+
expect: "has no skip definition"
2753+
2754+
result == """"Marks the field, argument, input field or enum value as deprecated"
2755+
directive @deprecated(
2756+
"The reason for the deprecation"
2757+
reason: String = "No longer supported"
2758+
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
2759+
2760+
"Directs the executor to include this field or fragment only when the `if` argument is true"
2761+
directive @include(
2762+
"Included when true."
2763+
if: Boolean!
2764+
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
2765+
2766+
"Indicates an Input Object is a OneOf Input Object."
2767+
directive @oneOf on INPUT_OBJECT
2768+
2769+
"Exposes a URL that specifies the behaviour of this scalar."
2770+
directive @specifiedBy(
2771+
"The URL that specifies the behaviour of this scalar."
2772+
url: String!
2773+
) on SCALAR
2774+
2775+
type Query {
2776+
field: String @deprecated(reason : "No longer supported")
27372777
}
2778+
"""
2779+
}
2780+
}
2781+
2782+

src/test/groovy/graphql/util/AnonymizerTest.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ type Object1 {
718718
when:
719719
def result = Anonymizer.anonymizeSchema(schema)
720720
def newSchema = new SchemaPrinter(SchemaPrinter.Options.defaultOptions()
721-
.includeDirectives({!DirectiveInfo.isGraphqlSpecifiedDirective(it)}))
721+
.includeDirectives({!DirectiveInfo.isGraphqlSpecifiedDirective(it) || it == "deprecated"}))
722722
.print(result)
723723

724724
then:
@@ -729,6 +729,8 @@ type Object1 {
729729
730730
directive @Directive1(argument1: String! = "stringValue4") repeatable on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
731731
732+
directive @deprecated(reason: String) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
733+
732734
interface Interface1 @Directive1(argument1 : "stringValue12") {
733735
field2: String
734736
field3: Enum1

0 commit comments

Comments
 (0)