UNPKG

1.61 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9import { GraphQLError } from '../error/GraphQLError';
10import { visit, visitWithTypeInfo } from '../language/visitor';
11import { getNamedType } from '../type/definition';
12import { TypeInfo } from './TypeInfo';
13/**
14 * A validation rule which reports deprecated usages.
15 *
16 * Returns a list of GraphQLError instances describing each deprecated use.
17 */
18
19export function findDeprecatedUsages(schema, ast) {
20 var errors = [];
21 var typeInfo = new TypeInfo(schema);
22 visit(ast, visitWithTypeInfo(typeInfo, {
23 Field: function Field(node) {
24 var fieldDef = typeInfo.getFieldDef();
25
26 if (fieldDef && fieldDef.isDeprecated) {
27 var parentType = typeInfo.getParentType();
28
29 if (parentType) {
30 var reason = fieldDef.deprecationReason;
31 errors.push(new GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated.") + (reason ? ' ' + reason : ''), [node]));
32 }
33 }
34 },
35 EnumValue: function EnumValue(node) {
36 var enumVal = typeInfo.getEnumValue();
37
38 if (enumVal && enumVal.isDeprecated) {
39 var type = getNamedType(typeInfo.getInputType());
40
41 if (type) {
42 var reason = enumVal.deprecationReason;
43 errors.push(new GraphQLError("The enum value ".concat(type.name, ".").concat(enumVal.name, " is deprecated.") + (reason ? ' ' + reason : ''), [node]));
44 }
45 }
46 }
47 }));
48 return errors;
49}
\No newline at end of file