Skip to content

Commit 1d5850f

Browse files
smitt04modosc
authored andcommitted
Add deprecated directive to arguments and input values
1 parent 3f859d4 commit 1d5850f

File tree

5 files changed

+116
-10
lines changed

5 files changed

+116
-10
lines changed

src/type/__tests__/introspection-test.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,17 @@ describe('Introspection', () => {
325325
},
326326
{
327327
name: 'inputFields',
328-
args: [],
328+
args: [
329+
{
330+
name: 'includeDeprecated',
331+
type: {
332+
kind: 'SCALAR',
333+
name: 'Boolean',
334+
ofType: null,
335+
},
336+
defaultValue: 'false',
337+
},
338+
],
329339
type: {
330340
kind: 'LIST',
331341
name: null,
@@ -441,7 +451,17 @@ describe('Introspection', () => {
441451
},
442452
{
443453
name: 'args',
444-
args: [],
454+
args: [
455+
{
456+
name: 'includeDeprecated',
457+
type: {
458+
kind: 'SCALAR',
459+
name: 'Boolean',
460+
ofType: null,
461+
},
462+
defaultValue: 'false',
463+
},
464+
],
445465
type: {
446466
kind: 'NON_NULL',
447467
name: null,
@@ -565,6 +585,32 @@ describe('Introspection', () => {
565585
isDeprecated: false,
566586
deprecationReason: null,
567587
},
588+
{
589+
name: 'isDeprecated',
590+
args: [],
591+
type: {
592+
kind: 'NON_NULL',
593+
name: null,
594+
ofType: {
595+
kind: 'SCALAR',
596+
name: 'Boolean',
597+
ofType: null,
598+
},
599+
},
600+
isDeprecated: false,
601+
deprecationReason: null,
602+
},
603+
{
604+
name: 'deprecationReason',
605+
args: [],
606+
type: {
607+
kind: 'SCALAR',
608+
name: 'String',
609+
ofType: null,
610+
},
611+
isDeprecated: false,
612+
deprecationReason: null,
613+
},
568614
],
569615
inputFields: null,
570616
interfaces: [],
@@ -880,7 +926,12 @@ describe('Introspection', () => {
880926
{
881927
name: 'deprecated',
882928
isRepeatable: false,
883-
locations: ['FIELD_DEFINITION', 'ENUM_VALUE'],
929+
locations: [
930+
'FIELD_DEFINITION',
931+
'ENUM_VALUE',
932+
'ARGUMENT_DEFINITION',
933+
'INPUT_FIELD_DEFINITION',
934+
],
884935
args: [
885936
{
886937
defaultValue: '"No longer supported"',

src/type/definition.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ export type GraphQLArgumentConfig = {|
949949
type: GraphQLInputType,
950950
defaultValue?: mixed,
951951
extensions?: ?ReadOnlyObjMapLike<mixed>,
952+
deprecationReason?: ?string,
952953
astNode?: ?InputValueDefinitionNode,
953954
|};
954955

@@ -978,6 +979,8 @@ export type GraphQLArgument = {|
978979
description: ?string,
979980
type: GraphQLInputType,
980981
defaultValue: mixed,
982+
isDeprecated?:boolean,
983+
deprecationReason?: ? string,
981984
extensions: ?ReadOnlyObjMap<mixed>,
982985
astNode: ?InputValueDefinitionNode,
983986
|};
@@ -1517,13 +1520,20 @@ function defineInputFieldMap(
15171520
isPlainObj(fieldMap),
15181521
`${config.name} fields must be an object with field names as keys or a function which returns such an object.`,
15191522
);
1523+
15201524
return mapValue(fieldMap, (fieldConfig, fieldName) => {
15211525
devAssert(
15221526
!('resolve' in fieldConfig),
15231527
`${config.name}.${fieldName} field has a resolve property, but Input Types cannot define resolvers.`,
15241528
);
1529+
devAssert(
1530+
!fieldConfig.hasOwnProperty('isDeprecated'),
1531+
`${config.name}.${fieldName} should provide "deprecationReason" `+
1532+
'instead of "isDeprecated".',
1533+
);
15251534

15261535
return {
1536+
isDeprecated: Boolean(fieldConfig.deprecationReason),
15271537
name: fieldName,
15281538
description: fieldConfig.description,
15291539
type: fieldConfig.type,
@@ -1547,6 +1557,7 @@ export type GraphQLInputFieldConfig = {|
15471557
description?: ?string,
15481558
type: GraphQLInputType,
15491559
defaultValue?: mixed,
1560+
deprecationReason?: ? string,
15501561
extensions?: ?ReadOnlyObjMapLike<mixed>,
15511562
astNode?: ?InputValueDefinitionNode,
15521563
|};
@@ -1558,6 +1569,8 @@ export type GraphQLInputField = {|
15581569
description: ?string,
15591570
type: GraphQLInputType,
15601571
defaultValue: mixed,
1572+
isDeprecated?:boolean,
1573+
deprecationReason?: ?string,
15611574
extensions: ?ReadOnlyObjMap<mixed>,
15621575
astNode: ?InputValueDefinitionNode,
15631576
|};

src/type/directives.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ export const DEFAULT_DEPRECATION_REASON = 'No longer supported';
181181
export const GraphQLDeprecatedDirective = new GraphQLDirective({
182182
name: 'deprecated',
183183
description: 'Marks an element of a GraphQL schema as no longer supported.',
184-
locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE],
184+
locations: [
185+
DirectiveLocation.FIELD_DEFINITION,
186+
DirectiveLocation.ENUM_VALUE,
187+
DirectiveLocation.ARGUMENT_DEFINITION,
188+
DirectiveLocation.INPUT_FIELD_DEFINITION,
189+
],
185190
args: {
186191
reason: {
187192
type: GraphQLString,

src/type/introspection.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,19 @@ export const __Type = new GraphQLObjectType({
288288
},
289289
inputFields: {
290290
type: GraphQLList(GraphQLNonNull(__InputValue)),
291-
resolve(type) {
291+
args: {
292+
includeDeprecated: {
293+
type: GraphQLBoolean,
294+
defaultValue: false,
295+
},
296+
},
297+
resolve(type, {includeDeprecated}) {
292298
if (isInputObjectType(type)) {
293-
return objectValues(type.getFields());
299+
let values=objectValues(type.getFields());
300+
if (!includeDeprecated) {
301+
values=values.filter(value => !value.deprecationReason);
302+
}
303+
return values;
294304
}
295305
},
296306
},
@@ -317,7 +327,22 @@ export const __Field = new GraphQLObjectType({
317327
},
318328
args: {
319329
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
320-
resolve: field => field.args,
330+
args: {
331+
includeDeprecated: {
332+
type: GraphQLBoolean,
333+
defaultValue: false,
334+
},
335+
},
336+
// resolve: field => field.args || [],
337+
resolve(field, {includeDeprecated}) {
338+
let args=field.args||[];
339+
340+
if (!includeDeprecated) {
341+
args=args.filter(arg => !arg.deprecationReason);
342+
}
343+
344+
return args;
345+
},
321346
},
322347
type: {
323348
type: GraphQLNonNull(__Type),
@@ -362,6 +387,14 @@ export const __InputValue = new GraphQLObjectType({
362387
return valueAST ? print(valueAST) : null;
363388
},
364389
},
390+
isDeprecated: {
391+
type: GraphQLNonNull(GraphQLBoolean),
392+
resolve: obj => obj.isDeprecated,
393+
},
394+
deprecationReason: {
395+
type: GraphQLString,
396+
resolve: obj => obj.deprecationReason,
397+
},
365398
}: GraphQLFieldConfigMap<GraphQLInputField, mixed>),
366399
});
367400

src/utilities/__tests__/schemaPrinter-test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ describe('Type System Printer', () => {
637637
Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).
638638
"""
639639
reason: String = "No longer supported"
640-
) on FIELD_DEFINITION | ENUM_VALUE
640+
) on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
641641
642642
"""
643643
A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.
@@ -721,7 +721,7 @@ describe('Type System Printer', () => {
721721
type __Field {
722722
name: String!
723723
description: String
724-
args: [__InputValue!]!
724+
args(includeDeprecated: Boolean = false): [__InputValue!]!
725725
type: __Type!
726726
isDeprecated: Boolean!
727727
deprecationReason: String
@@ -739,6 +739,8 @@ describe('Type System Printer', () => {
739739
A GraphQL-formatted string representing the default value for this input value.
740740
"""
741741
defaultValue: String
742+
isDeprecated: Boolean!
743+
deprecationReason: String
742744
}
743745
744746
"""
@@ -851,7 +853,7 @@ describe('Type System Printer', () => {
851853
directive @deprecated(
852854
# Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).
853855
reason: String = "No longer supported"
854-
) on FIELD_DEFINITION | ENUM_VALUE
856+
) on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
855857
856858
# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.
857859
type __Schema {
@@ -933,6 +935,8 @@ describe('Type System Printer', () => {
933935
934936
# A GraphQL-formatted string representing the default value for this input value.
935937
defaultValue: String
938+
isDeprecated: Boolean!
939+
deprecationReason: String
936940
}
937941
938942
# One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.

0 commit comments

Comments
 (0)