Skip to content

Commit 08cf170

Browse files
authored
Merge pull request graphql-java#3759 from graphql-java/3736-deprecated-reason
Make deprecated reason non-null
2 parents 3e9c5da + a95cc02 commit 08cf170

File tree

8 files changed

+41
-37
lines changed

8 files changed

+41
-37
lines changed

src/main/java/graphql/Directives.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class Directives {
6363
newInputValueDefinition()
6464
.name("reason")
6565
.description(createDescription("The reason for the deprecation"))
66-
.type(newTypeName().name(STRING).build())
66+
.type(newNonNullType(newTypeName().name(STRING).build()).build())
6767
.defaultValue(StringValue.newStringValue().value(NO_LONGER_SUPPORTED).build())
6868
.build())
6969
.build();
@@ -197,7 +197,7 @@ public class Directives {
197197
.description("Marks the field, argument, input field or enum value as deprecated")
198198
.argument(newArgument()
199199
.name("reason")
200-
.type(GraphQLString)
200+
.type(nonNull(GraphQLString))
201201
.defaultValueProgrammatic(NO_LONGER_SUPPORTED)
202202
.description("The reason for the deprecation"))
203203
.validLocations(FIELD_DEFINITION, ENUM_VALUE, ARGUMENT_DEFINITION, INPUT_FIELD_DEFINITION)

src/main/java/graphql/util/Anonymizer.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import graphql.schema.GraphQLNamedOutputType;
6161
import graphql.schema.GraphQLNamedSchemaElement;
6262
import graphql.schema.GraphQLNamedType;
63-
import graphql.schema.GraphQLNonNull;
6463
import graphql.schema.GraphQLObjectType;
6564
import graphql.schema.GraphQLScalarType;
6665
import graphql.schema.GraphQLSchema;
@@ -93,7 +92,7 @@
9392

9493
import static graphql.Assert.assertNotNull;
9594
import static graphql.parser.ParserEnvironment.newParserEnvironment;
96-
import static graphql.schema.GraphQLArgument.newArgument;
95+
import static graphql.schema.GraphQLNonNull.nonNull;
9796
import static graphql.schema.GraphQLTypeUtil.unwrapNonNull;
9897
import static graphql.schema.GraphQLTypeUtil.unwrapNonNullAs;
9998
import static graphql.schema.GraphQLTypeUtil.unwrapOneAs;
@@ -260,16 +259,6 @@ public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition graph
260259

261260
@Override
262261
public TraversalControl visitGraphQLDirective(GraphQLDirective graphQLDirective, TraverserContext<GraphQLSchemaElement> context) {
263-
if (Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName().equals(graphQLDirective.getName())) {
264-
GraphQLArgument reason = newArgument().name("reason")
265-
.type(Scalars.GraphQLString)
266-
.clearValue().build();
267-
GraphQLDirective newElement = graphQLDirective.transform(builder -> {
268-
builder.description(null).argument(reason);
269-
});
270-
changeNode(context, newElement);
271-
return TraversalControl.ABORT;
272-
}
273262
if (DirectiveInfo.isGraphqlSpecifiedDirective(graphQLDirective.getName())) {
274263
return TraversalControl.ABORT;
275264
}
@@ -285,7 +274,8 @@ public TraversalControl visitGraphQLAppliedDirective(GraphQLAppliedDirective gra
285274
if (Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName().equals(graphQLDirective.getName())) {
286275
GraphQLAppliedDirectiveArgument reason = GraphQLAppliedDirectiveArgument.newArgument().name("reason")
287276
.type(Scalars.GraphQLString)
288-
.clearValue().build();
277+
.clearValue()
278+
.build();
289279
GraphQLAppliedDirective newElement = graphQLDirective.transform(builder -> {
290280
builder.description(null).argument(reason);
291281
});
@@ -918,7 +908,7 @@ private static GraphQLType fromTypeToGraphQLType(Type type, GraphQLSchema schema
918908
graphql.Assert.assertNotNull(graphQLType, "Schema must contain type %s", typeName);
919909
return graphQLType;
920910
} else if (type instanceof NonNullType) {
921-
return GraphQLNonNull.nonNull(fromTypeToGraphQLType(TypeUtil.unwrapOne(type), schema));
911+
return nonNull(fromTypeToGraphQLType(TypeUtil.unwrapOne(type), schema));
922912
} else if (type instanceof ListType) {
923913
return GraphQLList.list(fromTypeToGraphQLType(TypeUtil.unwrapOne(type), schema));
924914
} else {

src/test/groovy/graphql/Issue2141.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Issue2141 extends Specification {
2727
"Marks the field, argument, input field or enum value as deprecated"
2828
directive @deprecated(
2929
"The reason for the deprecation"
30-
reason: String = "No longer supported"
30+
reason: String! = "No longer supported"
3131
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
3232
3333
"Directs the executor to include this field or fragment only when the `if` argument is true"

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,8 @@ class SchemaGeneratorTest extends Specification {
17701770
17711771
def appliedDirective = f1.getAppliedDirective("deprecated")
17721772
appliedDirective.name == "deprecated"
1773-
appliedDirective.getArgument("reason").type == GraphQLString
1773+
appliedDirective.getArgument("reason").type instanceof GraphQLNonNull
1774+
(appliedDirective.getArgument("reason").type as GraphQLNonNull).wrappedType == GraphQLString
17741775
printAst(appliedDirective.getArgument("reason").argumentValue.value as Node) == '"No longer supported"'
17751776
17761777
when:
@@ -1781,7 +1782,8 @@ class SchemaGeneratorTest extends Specification {
17811782
17821783
def appliedDirective2 = f2.getAppliedDirective("deprecated")
17831784
appliedDirective2.name == "deprecated"
1784-
appliedDirective2.getArgument("reason").type == GraphQLString
1785+
appliedDirective2.getArgument("reason").type instanceof GraphQLNonNull
1786+
(appliedDirective2.getArgument("reason").type as GraphQLNonNull).wrappedType == GraphQLString
17851787
printAst(appliedDirective2.getArgument("reason").argumentValue.value as Node) == '"Just because"'
17861788
def directive2 = f2.getDirective("deprecated")
17871789
printAst(directive2.getArgument("reason").argumentDefaultValue.value as Node) == '"No longer supported"'

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ type Query {
968968
"Marks the field, argument, input field or enum value as deprecated"
969969
directive @deprecated(
970970
"The reason for the deprecation"
971-
reason: String = "No longer supported"
971+
reason: String! = "No longer supported"
972972
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
973973
974974
directive @enumTypeDirective on ENUM
@@ -1144,7 +1144,7 @@ input SomeInput {
11441144
result == '''"Marks the field, argument, input field or enum value as deprecated"
11451145
directive @deprecated(
11461146
"The reason for the deprecation"
1147-
reason: String = "No longer supported"
1147+
reason: String! = "No longer supported"
11481148
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
11491149
11501150
"Directs the executor to include this field or fragment only when the `if` argument is true"
@@ -1240,7 +1240,7 @@ type Query {
12401240
resultWithDirectives == '''"Marks the field, argument, input field or enum value as deprecated"
12411241
directive @deprecated(
12421242
"The reason for the deprecation"
1243-
reason: String = "No longer supported"
1243+
reason: String! = "No longer supported"
12441244
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
12451245
12461246
directive @example on FIELD_DEFINITION
@@ -1308,7 +1308,7 @@ type Query {
13081308
resultWithDirectiveDefinitions == '''"Marks the field, argument, input field or enum value as deprecated"
13091309
directive @deprecated(
13101310
"The reason for the deprecation"
1311-
reason: String = "No longer supported"
1311+
reason: String! = "No longer supported"
13121312
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
13131313
13141314
directive @example on FIELD_DEFINITION
@@ -1405,7 +1405,7 @@ extend schema {
14051405
"Marks the field, argument, input field or enum value as deprecated"
14061406
directive @deprecated(
14071407
"The reason for the deprecation"
1408-
reason: String = "No longer supported"
1408+
reason: String! = "No longer supported"
14091409
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
14101410
14111411
"Directs the executor to include this field or fragment only when the `if` argument is true"
@@ -1491,7 +1491,7 @@ schema @schemaDirective{
14911491
"Marks the field, argument, input field or enum value as deprecated"
14921492
directive @deprecated(
14931493
"The reason for the deprecation"
1494-
reason: String = "No longer supported"
1494+
reason: String! = "No longer supported"
14951495
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
14961496
14971497
"Directs the executor to include this field or fragment only when the `if` argument is true"
@@ -1631,7 +1631,7 @@ type MyQuery {
16311631
result == '''"Marks the field, argument, input field or enum value as deprecated"
16321632
directive @deprecated(
16331633
"The reason for the deprecation"
1634-
reason: String = "No longer supported"
1634+
reason: String! = "No longer supported"
16351635
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
16361636
16371637
directive @directive1 on SCALAR
@@ -1873,7 +1873,7 @@ type Query {
18731873
result == '''"Marks the field, argument, input field or enum value as deprecated"
18741874
directive @deprecated(
18751875
"The reason for the deprecation"
1876-
reason: String = "No longer supported"
1876+
reason: String! = "No longer supported"
18771877
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
18781878
18791879
type Query {
@@ -2167,7 +2167,7 @@ type PrintMeType {
21672167
"Marks the field, argument, input field or enum value as deprecated"
21682168
directive @deprecated(
21692169
"The reason for the deprecation"
2170-
reason: String = "No longer supported"
2170+
reason: String! = "No longer supported"
21712171
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
21722172
21732173
directive @foo on SCHEMA
@@ -2399,7 +2399,7 @@ directive @include(
23992399
"Marks the field, argument, input field or enum value as deprecated"
24002400
directive @deprecated(
24012401
"The reason for the deprecation"
2402-
reason: String = "No longer supported"
2402+
reason: String! = "No longer supported"
24032403
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
24042404
24052405
union ZUnion = XQuery | Query
@@ -2515,7 +2515,7 @@ schema {
25152515
"Marks the field, argument, input field or enum value as deprecated"
25162516
directive @deprecated(
25172517
"The reason for the deprecation"
2518-
reason: String = "No longer supported"
2518+
reason: String! = "No longer supported"
25192519
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
25202520
25212521
" custom directive 'example' description 1"
@@ -2752,7 +2752,7 @@ schema {
27522752
"Marks the field, argument, input field or enum value as deprecated"
27532753
directive @deprecated(
27542754
"The reason for the deprecation"
2755-
reason: String = "No longer supported"
2755+
reason: String! = "No longer supported"
27562756
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
27572757
27582758
" custom directive 'example' description 1"
@@ -2940,7 +2940,7 @@ input Input {
29402940
result == """"Marks the field, argument, input field or enum value as deprecated"
29412941
directive @deprecated(
29422942
"The reason for the deprecation"
2943-
reason: String = "No longer supported"
2943+
reason: String! = "No longer supported"
29442944
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
29452945
29462946
"Directs the executor to include this field or fragment only when the `if` argument is true"

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,12 @@ 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-
732+
"Marks the field, argument, input field or enum value as deprecated"
733+
directive @deprecated(
734+
"The reason for the deprecation"
735+
reason: String! = "No longer supported"
736+
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
737+
734738
interface Interface1 @Directive1(argument1 : "stringValue12") {
735739
field2: String
736740
field3: Enum1

src/test/resources/large-schema-1.graphqls

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ directive @skip(
1515
if: Boolean!
1616
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
1717

18-
directive @deprecated(reason: String) on FIELD_DEFINITION | ENUM_VALUE
18+
"Marks the field, argument, input field or enum value as deprecated"
19+
directive @deprecated(
20+
"The reason for the deprecation"
21+
reason: String! = "No longer supported"
22+
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
1923

2024
"Exposes a URL that specifies the behaviour of this scalar."
2125
directive @specifiedBy(
@@ -544,4 +548,4 @@ input InputObject9 {
544548
inputField32: [ID!]
545549
inputField33: [Scalar2]
546550
inputField34: InputObject8
547-
}
551+
}

src/test/resources/many-fragments.graphqls

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ directive @Directive8(argument7: Enum3!) on FIELD_DEFINITION
2525

2626
directive @Directive9(argument8: String!) on OBJECT
2727

28-
directive @deprecated(reason: String) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
28+
"Marks the field, argument, input field or enum value as deprecated"
29+
directive @deprecated(
30+
"The reason for the deprecation"
31+
reason: String! = "No longer supported"
32+
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
2933

3034
"Directs the executor to include this field or fragment only when the `if` argument is true"
3135
directive @include(

0 commit comments

Comments
 (0)