UNPKG

1.42 kBJavaScriptView Raw
1import { GraphQLError } from '../error/GraphQLError';
2import { visit, visitWithTypeInfo } from '../language/visitor';
3import { getNamedType } from '../type/definition';
4import { TypeInfo } from './TypeInfo';
5/**
6 * A validation rule which reports deprecated usages.
7 *
8 * Returns a list of GraphQLError instances describing each deprecated use.
9 */
10
11export function findDeprecatedUsages(schema, ast) {
12 var errors = [];
13 var typeInfo = new TypeInfo(schema);
14 visit(ast, visitWithTypeInfo(typeInfo, {
15 Field: function Field(node) {
16 var fieldDef = typeInfo.getFieldDef();
17
18 if (fieldDef && fieldDef.isDeprecated) {
19 var parentType = typeInfo.getParentType();
20
21 if (parentType) {
22 var reason = fieldDef.deprecationReason;
23 errors.push(new GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated.") + (reason ? ' ' + reason : ''), node));
24 }
25 }
26 },
27 EnumValue: function EnumValue(node) {
28 var enumVal = typeInfo.getEnumValue();
29
30 if (enumVal && enumVal.isDeprecated) {
31 var type = getNamedType(typeInfo.getInputType());
32
33 if (type) {
34 var reason = enumVal.deprecationReason;
35 errors.push(new GraphQLError("The enum value ".concat(type.name, ".").concat(enumVal.name, " is deprecated.") + (reason ? ' ' + reason : ''), node));
36 }
37 }
38 }
39 }));
40 return errors;
41}