-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ProhibitDeprecatedFieldsRule.js
52 lines (47 loc) · 1.55 KB
/
ProhibitDeprecatedFieldsRule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// @flow strict
import { GraphQLError } from '../../../error/GraphQLError';
import { type EnumValueNode, type FieldNode } from '../../../language/ast';
import { type ASTVisitor } from '../../../language/visitor';
import { getNamedType } from '../../../type/definition';
import { type ValidationContext } from '../../ValidationContext';
/**
* Prohibit deprecated fields
*
* A GraphQL document is only valid if all selected fields and all used enum
* values have not been deprecated.
*
* Note: This rule is optional and is not part of the Validation section of the
* GraphQL Specification.
*/
export function ProhibitDeprecatedFieldsRule(
context: ValidationContext,
): ASTVisitor {
return {
Field(node: FieldNode) {
const fieldDef = context.getFieldDef();
const parentType = context.getParentType();
if (parentType && fieldDef && fieldDef.deprecationReason != null) {
context.reportError(
new GraphQLError(
`The field ${parentType.name}.${fieldDef.name} is deprecated. ` +
fieldDef.deprecationReason,
[node],
),
);
}
},
EnumValue(node: EnumValueNode) {
const type = getNamedType(context.getInputType());
const enumValue = context.getEnumValue();
if (type && enumValue?.deprecationReason != null) {
context.reportError(
new GraphQLError(
`The enum value "${type.name}.${enumValue.name}" is deprecated. ` +
enumValue.deprecationReason,
node,
),
);
}
},
};
}