Skip to content

Commit 9735ac7

Browse files
Add NoDeprecatedCustomRule and deprecate findDeprecatedUsages (#2605)
1 parent 00077f1 commit 9735ac7

15 files changed

+222
-123
lines changed

.nycrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ exclude:
1414
- 'src/validation/rules/UniqueFieldDefinitionNames.js'
1515
- 'src/validation/rules/UniqueTypeNames.js'
1616
- 'src/validation/rules/UniqueOperationTypes.js'
17+
- 'src/utilities/findDeprecatedUsages.js'
1718
clean: true
1819
temp-directory: 'coverage/tests'
1920
report-dir: 'coverage/tests'

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ export {
336336
UniqueDirectiveNamesRule,
337337
PossibleTypeExtensionsRule,
338338
// Custom validation rules
339+
NoDeprecatedCustomRule,
339340
NoSchemaIntrospectionCustomRule,
340341
ValidationRule,
341342
} from './validation/index';
@@ -416,7 +417,7 @@ export {
416417
DangerousChangeType,
417418
findBreakingChanges,
418419
findDangerousChanges,
419-
// Report all deprecated usage within a GraphQL document.
420+
// @deprecated: Report all deprecated usage within a GraphQL document.
420421
findDeprecatedUsages,
421422
} from './utilities/index';
422423

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ export {
334334
UniqueDirectiveNamesRule,
335335
PossibleTypeExtensionsRule,
336336
// Custom validation rules
337+
NoDeprecatedCustomRule,
337338
NoSchemaIntrospectionCustomRule,
338339
} from './validation/index';
339340

@@ -416,7 +417,7 @@ export {
416417
DangerousChangeType,
417418
findBreakingChanges,
418419
findDangerousChanges,
419-
// Report all deprecated usage within a GraphQL document.
420+
// @deprecated: Report all deprecated usage within a GraphQL document.
420421
findDeprecatedUsages,
421422
} from './utilities/index';
422423

src/utilities/__tests__/findDeprecatedUsages-test.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/utilities/findDeprecatedUsages.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import { GraphQLSchema } from '../type/schema';
66
* A validation rule which reports deprecated usages.
77
*
88
* Returns a list of GraphQLError instances describing each deprecated use.
9+
*
10+
* @deprecated Please use `validate` with `NoDeprecatedCustomRule` instead:
11+
*
12+
* ```
13+
* import { validate, NoDeprecatedCustomRule } from 'graphql'
14+
*
15+
* const errors = validate(schema, document, [NoDeprecatedCustomRule])
16+
* ```
917
*/
1018
export function findDeprecatedUsages(
1119
schema: GraphQLSchema,
1220
ast: DocumentNode,
13-
): Array<GraphQLError>;
21+
): ReadonlyArray<GraphQLError>;

src/utilities/findDeprecatedUsages.js

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,27 @@ import { GraphQLError } from '../error/GraphQLError';
44

55
import type { DocumentNode } from '../language/ast';
66

7-
import { visit } from '../language/visitor';
8-
97
import type { GraphQLSchema } from '../type/schema';
108

11-
import { getNamedType } from '../type/definition';
12-
13-
import { TypeInfo, visitWithTypeInfo } from './TypeInfo';
9+
import { validate } from '../validation/validate';
10+
import { NoDeprecatedCustomRule } from '../validation/rules/custom/NoDeprecatedCustomRule';
1411

1512
/**
1613
* A validation rule which reports deprecated usages.
1714
*
1815
* Returns a list of GraphQLError instances describing each deprecated use.
16+
*
17+
* @deprecated Please use `validate` with `NoDeprecatedCustomRule` instead:
18+
*
19+
* ```
20+
* import { validate, NoDeprecatedCustomRule } from 'graphql'
21+
*
22+
* const errors = validate(schema, document, [NoDeprecatedCustomRule])
23+
* ```
1924
*/
2025
export function findDeprecatedUsages(
2126
schema: GraphQLSchema,
2227
ast: DocumentNode,
23-
): Array<GraphQLError> {
24-
const errors = [];
25-
const typeInfo = new TypeInfo(schema);
26-
27-
visit(
28-
ast,
29-
visitWithTypeInfo(typeInfo, {
30-
Field(node) {
31-
const parentType = typeInfo.getParentType();
32-
const fieldDef = typeInfo.getFieldDef();
33-
if (parentType && fieldDef?.deprecationReason != null) {
34-
errors.push(
35-
new GraphQLError(
36-
`The field "${parentType.name}.${fieldDef.name}" is deprecated. ` +
37-
fieldDef.deprecationReason,
38-
node,
39-
),
40-
);
41-
}
42-
},
43-
EnumValue(node) {
44-
const type = getNamedType(typeInfo.getInputType());
45-
const enumVal = typeInfo.getEnumValue();
46-
if (type && enumVal?.deprecationReason != null) {
47-
errors.push(
48-
new GraphQLError(
49-
`The enum value "${type.name}.${enumVal.name}" is deprecated. ` +
50-
enumVal.deprecationReason,
51-
node,
52-
),
53-
);
54-
}
55-
},
56-
}),
57-
);
58-
59-
return errors;
28+
): $ReadOnlyArray<GraphQLError> {
29+
return validate(schema, ast, [NoDeprecatedCustomRule]);
6030
}

src/utilities/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,5 @@ export {
112112
DangerousChange,
113113
} from './findBreakingChanges';
114114

115-
// Report all deprecated usage within a GraphQL document.
115+
// @deprecated: Report all deprecated usage within a GraphQL document.
116116
export { findDeprecatedUsages } from './findDeprecatedUsages';

src/utilities/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,5 @@ export {
111111
} from './findBreakingChanges';
112112
export type { BreakingChange, DangerousChange } from './findBreakingChanges';
113113

114-
// Report all deprecated usage within a GraphQL document.
114+
// @deprecated: Report all deprecated usage within a GraphQL document.
115115
export { findDeprecatedUsages } from './findDeprecatedUsages';

src/validation/ValidationContext.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
GraphQLCompositeType,
1919
GraphQLField,
2020
GraphQLArgument,
21+
GraphQLEnumValue,
2122
} from '../type/definition';
2223
import { TypeInfo } from '../utilities/TypeInfo';
2324

@@ -90,6 +91,8 @@ export class ValidationContext extends ASTValidationContext {
9091
getDirective(): Maybe<GraphQLDirective>;
9192

9293
getArgument(): Maybe<GraphQLArgument>;
94+
95+
getEnumValue(): Maybe<GraphQLEnumValue>;
9396
}
9497

9598
export type ValidationRule = (context: ValidationContext) => ASTVisitor;

src/validation/ValidationContext.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
GraphQLCompositeType,
2626
GraphQLField,
2727
GraphQLArgument,
28+
GraphQLEnumValue,
2829
} from '../type/definition';
2930

3031
import { TypeInfo, visitWithTypeInfo } from '../utilities/TypeInfo';
@@ -245,6 +246,10 @@ export class ValidationContext extends ASTValidationContext {
245246
getArgument(): ?GraphQLArgument {
246247
return this._typeInfo.getArgument();
247248
}
249+
250+
getEnumValue(): ?GraphQLEnumValue {
251+
return this._typeInfo.getEnumValue();
252+
}
248253
}
249254

250255
export type ValidationRule = (ValidationContext) => ASTVisitor;

0 commit comments

Comments
 (0)