Skip to content

Commit dfb204e

Browse files
committed
Add deprecated directive to arguments and input values
1 parent 7fff8b7 commit dfb204e

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

src/type/__tests__/introspection-test.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,17 @@ describe('Introspection', () => {
307307
},
308308
{
309309
name: 'inputFields',
310-
args: [],
310+
args: [
311+
{
312+
name: 'includeDeprecated',
313+
type: {
314+
kind: 'SCALAR',
315+
name: 'Boolean',
316+
ofType: null,
317+
},
318+
defaultValue: 'false',
319+
},
320+
],
311321
type: {
312322
kind: 'LIST',
313323
name: null,
@@ -432,7 +442,17 @@ describe('Introspection', () => {
432442
},
433443
{
434444
name: 'args',
435-
args: [],
445+
args: [
446+
{
447+
name: 'includeDeprecated',
448+
type: {
449+
kind: 'SCALAR',
450+
name: 'Boolean',
451+
ofType: null,
452+
},
453+
defaultValue: 'false',
454+
},
455+
],
436456
type: {
437457
kind: 'NON_NULL',
438458
name: null,
@@ -556,6 +576,32 @@ describe('Introspection', () => {
556576
isDeprecated: false,
557577
deprecationReason: null,
558578
},
579+
{
580+
name: 'isDeprecated',
581+
args: [],
582+
type: {
583+
kind: 'NON_NULL',
584+
name: null,
585+
ofType: {
586+
kind: 'SCALAR',
587+
name: 'Boolean',
588+
ofType: null,
589+
},
590+
},
591+
isDeprecated: false,
592+
deprecationReason: null,
593+
},
594+
{
595+
name: 'deprecationReason',
596+
args: [],
597+
type: {
598+
kind: 'SCALAR',
599+
name: 'String',
600+
ofType: null,
601+
},
602+
isDeprecated: false,
603+
deprecationReason: null,
604+
},
559605
],
560606
inputFields: null,
561607
interfaces: [],
@@ -853,7 +899,12 @@ describe('Introspection', () => {
853899
},
854900
{
855901
name: 'deprecated',
856-
locations: ['FIELD_DEFINITION', 'ENUM_VALUE'],
902+
locations: [
903+
'FIELD_DEFINITION',
904+
'ENUM_VALUE',
905+
'ARGUMENT_DEFINITION',
906+
'INPUT_FIELD_DEFINITION',
907+
],
857908
args: [
858909
{
859910
defaultValue: '"No longer supported"',

src/type/definition.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ export type GraphQLArgumentConfig = {|
827827
type: GraphQLInputType,
828828
defaultValue?: mixed,
829829
description?: ?string,
830+
deprecationReason?: ?string,
830831
astNode?: ?InputValueDefinitionNode,
831832
|};
832833
@@ -855,6 +856,8 @@ export type GraphQLArgument = {
855856
type: GraphQLInputType,
856857
defaultValue?: mixed,
857858
description?: ?string,
859+
isDeprecated?: boolean,
860+
deprecationReason?: ?string,
858861
astNode?: ?InputValueDefinitionNode,
859862
};
860863
@@ -1238,8 +1241,15 @@ function defineInputFieldMap(
12381241
);
12391242
const resultFieldMap = Object.create(null);
12401243
for (const fieldName of Object.keys(fieldMap)) {
1244+
const fieldConfig = fieldMap[fieldName];
1245+
invariant(
1246+
!fieldConfig.hasOwnProperty('isDeprecated'),
1247+
`${config.name}.${fieldName} should provide "deprecationReason" ` +
1248+
'instead of "isDeprecated".',
1249+
);
12411250
const field = {
1242-
...fieldMap[fieldName],
1251+
...fieldConfig,
1252+
isDeprecated: Boolean(fieldConfig.deprecationReason),
12431253
name: fieldName,
12441254
};
12451255
invariant(
@@ -1264,6 +1274,7 @@ export type GraphQLInputFieldConfig = {|
12641274
type: GraphQLInputType,
12651275
defaultValue?: mixed,
12661276
description?: ?string,
1277+
deprecationReason?: ?string,
12671278
astNode?: ?InputValueDefinitionNode,
12681279
|};
12691280

@@ -1274,6 +1285,8 @@ export type GraphQLInputField = {
12741285
type: GraphQLInputType,
12751286
defaultValue?: mixed,
12761287
description?: ?string,
1288+
isDeprecated?: boolean,
1289+
deprecationReason?: ?string,
12771290
astNode?: ?InputValueDefinitionNode,
12781291
};
12791292

src/type/directives.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ export const DEFAULT_DEPRECATION_REASON = 'No longer supported';
147147
export const GraphQLDeprecatedDirective = new GraphQLDirective({
148148
name: 'deprecated',
149149
description: 'Marks an element of a GraphQL schema as no longer supported.',
150-
locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE],
150+
locations: [
151+
DirectiveLocation.FIELD_DEFINITION,
152+
DirectiveLocation.ENUM_VALUE,
153+
DirectiveLocation.ARGUMENT_DEFINITION,
154+
DirectiveLocation.INPUT_FIELD_DEFINITION,
155+
],
151156
args: {
152157
reason: {
153158
type: GraphQLString,

src/type/introspection.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ export const __Type = new GraphQLObjectType({
232232
fields: {
233233
type: GraphQLList(GraphQLNonNull(__Field)),
234234
args: {
235-
includeDeprecated: { type: GraphQLBoolean, defaultValue: false },
235+
includeDeprecated: {
236+
type: GraphQLBoolean,
237+
defaultValue: false,
238+
},
236239
},
237240
resolve(type, { includeDeprecated }) {
238241
if (isObjectType(type) || isInterfaceType(type)) {
@@ -264,7 +267,10 @@ export const __Type = new GraphQLObjectType({
264267
enumValues: {
265268
type: GraphQLList(GraphQLNonNull(__EnumValue)),
266269
args: {
267-
includeDeprecated: { type: GraphQLBoolean, defaultValue: false },
270+
includeDeprecated: {
271+
type: GraphQLBoolean,
272+
defaultValue: false,
273+
},
268274
},
269275
resolve(type, { includeDeprecated }) {
270276
if (isEnumType(type)) {
@@ -278,9 +284,19 @@ export const __Type = new GraphQLObjectType({
278284
},
279285
inputFields: {
280286
type: GraphQLList(GraphQLNonNull(__InputValue)),
281-
resolve(type) {
287+
args: {
288+
includeDeprecated: {
289+
type: GraphQLBoolean,
290+
defaultValue: false,
291+
},
292+
},
293+
resolve(type, { includeDeprecated }) {
282294
if (isInputObjectType(type)) {
283-
return objectValues(type.getFields());
295+
let values = objectValues(type.getFields());
296+
if (!includeDeprecated) {
297+
values = values.filter(value => !value.deprecationReason);
298+
}
299+
return values;
284300
}
285301
},
286302
},
@@ -307,7 +323,22 @@ export const __Field = new GraphQLObjectType({
307323
},
308324
args: {
309325
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
310-
resolve: field => field.args || [],
326+
args: {
327+
includeDeprecated: {
328+
type: GraphQLBoolean,
329+
defaultValue: false,
330+
},
331+
},
332+
// resolve: field => field.args || [],
333+
resolve(field, { includeDeprecated }) {
334+
let args = field.args || [];
335+
336+
if (!includeDeprecated) {
337+
args = args.filter(arg => !arg.deprecationReason);
338+
}
339+
340+
return args;
341+
},
311342
},
312343
type: {
313344
type: GraphQLNonNull(__Type),
@@ -353,6 +384,14 @@ export const __InputValue = new GraphQLObjectType({
353384
? null
354385
: print(astFromValue(inputVal.defaultValue, inputVal.type)),
355386
},
387+
isDeprecated: {
388+
type: GraphQLNonNull(GraphQLBoolean),
389+
resolve: obj => obj.isDeprecated,
390+
},
391+
deprecationReason: {
392+
type: GraphQLString,
393+
resolve: obj => obj.deprecationReason,
394+
},
356395
}),
357396
});
358397

src/utilities/__tests__/schemaPrinter-test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ describe('Type System Printer', () => {
635635
(as specified by [CommonMark](https://commonmark.org/).
636636
"""
637637
reason: String = "No longer supported"
638-
) on FIELD_DEFINITION | ENUM_VALUE
638+
) on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
639639
640640
"""
641641
A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
@@ -734,7 +734,7 @@ describe('Type System Printer', () => {
734734
type __Field {
735735
name: String!
736736
description: String
737-
args: [__InputValue!]!
737+
args(includeDeprecated: Boolean = false): [__InputValue!]!
738738
type: __Type!
739739
isDeprecated: Boolean!
740740
deprecationReason: String
@@ -754,6 +754,8 @@ describe('Type System Printer', () => {
754754
A GraphQL-formatted string representing the default value for this input value.
755755
"""
756756
defaultValue: String
757+
isDeprecated: Boolean!
758+
deprecationReason: String
757759
}
758760
759761
"""
@@ -800,7 +802,7 @@ describe('Type System Printer', () => {
800802
interfaces: [__Type!]
801803
possibleTypes: [__Type!]
802804
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
803-
inputFields: [__InputValue!]
805+
inputFields(includeDeprecated: Boolean = false): [__InputValue!]
804806
ofType: __Type
805807
}
806808
@@ -870,7 +872,7 @@ describe('Type System Printer', () => {
870872
# for how to access supported similar data. Formatted using the Markdown syntax
871873
# (as specified by [CommonMark](https://commonmark.org/).
872874
reason: String = "No longer supported"
873-
) on FIELD_DEFINITION | ENUM_VALUE
875+
) on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
874876
875877
# A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
876878
#
@@ -961,7 +963,7 @@ describe('Type System Printer', () => {
961963
type __Field {
962964
name: String!
963965
description: String
964-
args: [__InputValue!]!
966+
args(includeDeprecated: Boolean = false): [__InputValue!]!
965967
type: __Type!
966968
isDeprecated: Boolean!
967969
deprecationReason: String
@@ -977,6 +979,8 @@ describe('Type System Printer', () => {
977979
978980
# A GraphQL-formatted string representing the default value for this input value.
979981
defaultValue: String
982+
isDeprecated: Boolean!
983+
deprecationReason: String
980984
}
981985
982986
# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all
@@ -1015,7 +1019,7 @@ describe('Type System Printer', () => {
10151019
interfaces: [__Type!]
10161020
possibleTypes: [__Type!]
10171021
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
1018-
inputFields: [__InputValue!]
1022+
inputFields(includeDeprecated: Boolean = false): [__InputValue!]
10191023
ofType: __Type
10201024
}
10211025

src/utilities/buildASTSchema.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,10 @@ export class ASTDefinitionBuilder {
441441
* deprecation reason.
442442
*/
443443
function getDeprecationReason(
444-
node: EnumValueDefinitionNode | FieldDefinitionNode,
444+
node:
445+
| EnumValueDefinitionNode
446+
| FieldDefinitionNode
447+
| InputValueDefinitionNode,
445448
): ?string {
446449
const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node);
447450
return deprecated && (deprecated.reason: any);

0 commit comments

Comments
 (0)