findDeprecatedUsages.js.flow 1.75 KB
Newer Older
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 53 54 55 56 57 58 59 60 61 62 63 64
// @flow strict

import { GraphQLError } from '../error/GraphQLError';

import { type DocumentNode } from '../language/ast';
import { visit, visitWithTypeInfo } from '../language/visitor';

import { getNamedType } from '../type/definition';
import { type GraphQLSchema } from '../type/schema';

import { TypeInfo } from './TypeInfo';

/**
 * A validation rule which reports deprecated usages.
 *
 * Returns a list of GraphQLError instances describing each deprecated use.
 */
export function findDeprecatedUsages(
  schema: GraphQLSchema,
  ast: DocumentNode,
): Array<GraphQLError> {
  const errors = [];
  const typeInfo = new TypeInfo(schema);

  visit(
    ast,
    visitWithTypeInfo(typeInfo, {
      Field(node) {
        const fieldDef = typeInfo.getFieldDef();
        if (fieldDef && fieldDef.isDeprecated) {
          const parentType = typeInfo.getParentType();
          if (parentType) {
            const reason = fieldDef.deprecationReason;
            errors.push(
              new GraphQLError(
                `The field ${parentType.name}.${fieldDef.name} is deprecated.` +
                  (reason ? ' ' + reason : ''),
                node,
              ),
            );
          }
        }
      },
      EnumValue(node) {
        const enumVal = typeInfo.getEnumValue();
        if (enumVal && enumVal.isDeprecated) {
          const type = getNamedType(typeInfo.getInputType());
          if (type) {
            const reason = enumVal.deprecationReason;
            errors.push(
              new GraphQLError(
                `The enum value ${type.name}.${enumVal.name} is deprecated.` +
                  (reason ? ' ' + reason : ''),
                node,
              ),
            );
          }
        }
      },
    }),
  );

  return errors;
}