Skip to content

Commit 3f129b5

Browse files
introspection: Add missing support for deprecated input values (#2855)
Fixes #2834
1 parent 8c6e7c7 commit 3f129b5

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

src/utilities/__tests__/buildClientSchema-test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ describe('Type System: build schema from introspection', () => {
486486

487487
it('builds a schema aware of deprecation', () => {
488488
const sdl = dedent`
489+
directive @someDirective(
490+
"""This is a shiny new argument"""
491+
shinyArg: SomeInputObject
492+
493+
"""This was our design mistake :("""
494+
oldArg: String @deprecated(reason: "Use shinyArg")
495+
) on QUERY
496+
489497
enum Color {
490498
"""So rosy"""
491499
RED
@@ -500,13 +508,32 @@ describe('Type System: build schema from introspection', () => {
500508
MAUVE @deprecated(reason: "No longer in fashion")
501509
}
502510
511+
input SomeInputObject {
512+
"""Nothing special about it, just deprecated for some unknown reason"""
513+
oldField: String @deprecated(reason: "Don't use it, use newField instead!")
514+
515+
"""Same field but with a new name"""
516+
newField: String
517+
}
518+
503519
type Query {
504520
"""This is a shiny string field"""
505521
shinyString: String
506522
507523
"""This is a deprecated string field"""
508524
deprecatedString: String @deprecated(reason: "Use shinyString")
525+
526+
"""Color of a week"""
509527
color: Color
528+
529+
"""Some random field"""
530+
someField(
531+
"""This is a shiny new argument"""
532+
shinyArg: SomeInputObject
533+
534+
"""This was our design mistake :("""
535+
oldArg: String @deprecated(reason: "Use shinyArg")
536+
): String
510537
}
511538
`;
512539

@@ -515,8 +542,14 @@ describe('Type System: build schema from introspection', () => {
515542

516543
it('builds a schema with empty deprecation reasons', () => {
517544
const sdl = dedent`
545+
directive @someDirective(someArg: SomeInputObject @deprecated(reason: "")) on QUERY
546+
518547
type Query {
519-
someField: String @deprecated(reason: "")
548+
someField(someArg: SomeInputObject @deprecated(reason: "")): SomeEnum @deprecated(reason: "")
549+
}
550+
551+
input SomeInputObject {
552+
someInputField: String @deprecated(reason: "")
520553
}
521554
522555
enum SomeEnum {

src/utilities/__tests__/getIntrospectionQuery-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,46 @@ describe('getIntrospectionQuery', () => {
7474
'specifiedByUrl',
7575
);
7676
});
77+
78+
it('include "isDeprecated" field on input values', () => {
79+
expectIntrospectionQuery().toMatch('isDeprecated', 2);
80+
81+
expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch(
82+
'isDeprecated',
83+
3,
84+
);
85+
86+
expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch(
87+
'isDeprecated',
88+
2,
89+
);
90+
});
91+
92+
it('include "deprecationReason" field on input values', () => {
93+
expectIntrospectionQuery().toMatch('deprecationReason', 2);
94+
95+
expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch(
96+
'deprecationReason',
97+
3,
98+
);
99+
100+
expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch(
101+
'deprecationReason',
102+
2,
103+
);
104+
});
105+
106+
it('include deprecated input field and args', () => {
107+
expectIntrospectionQuery().toMatch('includeDeprecated: true', 2);
108+
109+
expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch(
110+
'includeDeprecated: true',
111+
5,
112+
);
113+
114+
expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch(
115+
'includeDeprecated: true',
116+
2,
117+
);
118+
});
77119
});

src/utilities/buildClientSchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ export function buildClientSchema(
377377
description: inputValueIntrospection.description,
378378
type,
379379
defaultValue,
380+
deprecationReason: inputValueIntrospection.deprecationReason,
380381
};
381382
}
382383

src/utilities/getIntrospectionQuery.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export interface IntrospectionOptions {
1818
// Whether to include `description` field on schema.
1919
// Default: false
2020
schemaDescription?: boolean;
21+
22+
// Whether target GraphQL server support deprecation of input values.
23+
// Default: false
24+
inputValueDeprecation?: boolean;
2125
}
2226

2327
export function getIntrospectionQuery(options?: IntrospectionOptions): string;
@@ -169,6 +173,8 @@ export interface IntrospectionInputValue {
169173
readonly description?: Maybe<string>;
170174
readonly type: IntrospectionInputTypeRef;
171175
readonly defaultValue?: Maybe<string>;
176+
readonly isDeprecated?: boolean;
177+
readonly deprecationReason?: Maybe<string>;
172178
}
173179

174180
export interface IntrospectionEnumValue {

src/utilities/getIntrospectionQuery.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export type IntrospectionOptions = {|
1616
// Whether to include `description` field on schema.
1717
// Default: false
1818
schemaDescription?: boolean,
19+
20+
// Whether target GraphQL server support deprecation of input values.
21+
// Default: false
22+
inputValueDeprecation?: boolean,
1923
|};
2024

2125
export function getIntrospectionQuery(options?: IntrospectionOptions): string {
@@ -24,6 +28,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
2428
specifiedByUrl: false,
2529
directiveIsRepeatable: false,
2630
schemaDescription: false,
31+
inputValueDeprecation: false,
2732
...options,
2833
};
2934

@@ -38,6 +43,10 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
3843
? descriptions
3944
: '';
4045

46+
function inputDeprecation(str) {
47+
return optionsWithDefault.inputValueDeprecation ? str : '';
48+
}
49+
4150
return `
4251
query IntrospectionQuery {
4352
__schema {
@@ -53,7 +62,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
5362
${descriptions}
5463
${directiveIsRepeatable}
5564
locations
56-
args {
65+
args${inputDeprecation('(includeDeprecated: true)')} {
5766
...InputValue
5867
}
5968
}
@@ -68,7 +77,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
6877
fields(includeDeprecated: true) {
6978
name
7079
${descriptions}
71-
args {
80+
args${inputDeprecation('(includeDeprecated: true)')} {
7281
...InputValue
7382
}
7483
type {
@@ -77,7 +86,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
7786
isDeprecated
7887
deprecationReason
7988
}
80-
inputFields {
89+
inputFields${inputDeprecation('(includeDeprecated: true)')} {
8190
...InputValue
8291
}
8392
interfaces {
@@ -99,6 +108,8 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
99108
${descriptions}
100109
type { ...TypeRef }
101110
defaultValue
111+
${inputDeprecation('isDeprecated')}
112+
${inputDeprecation('deprecationReason')}
102113
}
103114
104115
fragment TypeRef on __Type {
@@ -280,6 +291,8 @@ export type IntrospectionInputValue = {|
280291
+description?: ?string,
281292
+type: IntrospectionInputTypeRef,
282293
+defaultValue: ?string,
294+
+isDeprecated?: boolean,
295+
+deprecationReason?: ?string,
283296
|};
284297

285298
export type IntrospectionEnumValue = {|

src/utilities/introspectionFromSchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function introspectionFromSchema(
2929
specifiedByUrl: true,
3030
directiveIsRepeatable: true,
3131
schemaDescription: true,
32+
inputValueDeprecation: true,
3233
...options,
3334
};
3435

0 commit comments

Comments
 (0)