Skip to content

Commit 784c70d

Browse files
IvanGoncharovsaihaj
authored andcommitted
introspection: Add missing support for deprecated input values (graphql#2855)
Fixes graphql#2834
1 parent 4da6b13 commit 784c70d

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
@@ -483,6 +483,14 @@ describe('Type System: build schema from introspection', () => {
483483

484484
it('builds a schema aware of deprecation', () => {
485485
const sdl = dedent`
486+
directive @someDirective(
487+
"""This is a shiny new argument"""
488+
shinyArg: SomeInputObject
489+
490+
"""This was our design mistake :("""
491+
oldArg: String @deprecated(reason: "Use shinyArg")
492+
) on QUERY
493+
486494
enum Color {
487495
"""So rosy"""
488496
RED
@@ -497,13 +505,32 @@ describe('Type System: build schema from introspection', () => {
497505
MAUVE @deprecated(reason: "No longer in fashion")
498506
}
499507
508+
input SomeInputObject {
509+
"""Nothing special about it, just deprecated for some unknown reason"""
510+
oldField: String @deprecated(reason: "Don't use it, use newField instead!")
511+
512+
"""Same field but with a new name"""
513+
newField: String
514+
}
515+
500516
type Query {
501517
"""This is a shiny string field"""
502518
shinyString: String
503519
504520
"""This is a deprecated string field"""
505521
deprecatedString: String @deprecated(reason: "Use shinyString")
522+
523+
"""Color of a week"""
506524
color: Color
525+
526+
"""Some random field"""
527+
someField(
528+
"""This is a shiny new argument"""
529+
shinyArg: SomeInputObject
530+
531+
"""This was our design mistake :("""
532+
oldArg: String @deprecated(reason: "Use shinyArg")
533+
): String
507534
}
508535
`;
509536

@@ -512,8 +539,14 @@ describe('Type System: build schema from introspection', () => {
512539

513540
it('builds a schema with empty deprecation reasons', () => {
514541
const sdl = dedent`
542+
directive @someDirective(someArg: SomeInputObject @deprecated(reason: "")) on QUERY
543+
515544
type Query {
516-
someField: String @deprecated(reason: "")
545+
someField(someArg: SomeInputObject @deprecated(reason: "")): SomeEnum @deprecated(reason: "")
546+
}
547+
548+
input SomeInputObject {
549+
someInputField: String @deprecated(reason: "")
517550
}
518551
519552
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)