findDeprecatedUsages.js.flow 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @flow strict
  2. import { GraphQLError } from '../error/GraphQLError';
  3. import { type DocumentNode } from '../language/ast';
  4. import { visit, visitWithTypeInfo } from '../language/visitor';
  5. import { getNamedType } from '../type/definition';
  6. import { type GraphQLSchema } from '../type/schema';
  7. import { TypeInfo } from './TypeInfo';
  8. /**
  9. * A validation rule which reports deprecated usages.
  10. *
  11. * Returns a list of GraphQLError instances describing each deprecated use.
  12. */
  13. export function findDeprecatedUsages(
  14. schema: GraphQLSchema,
  15. ast: DocumentNode,
  16. ): Array<GraphQLError> {
  17. const errors = [];
  18. const typeInfo = new TypeInfo(schema);
  19. visit(
  20. ast,
  21. visitWithTypeInfo(typeInfo, {
  22. Field(node) {
  23. const fieldDef = typeInfo.getFieldDef();
  24. if (fieldDef && fieldDef.isDeprecated) {
  25. const parentType = typeInfo.getParentType();
  26. if (parentType) {
  27. const reason = fieldDef.deprecationReason;
  28. errors.push(
  29. new GraphQLError(
  30. `The field ${parentType.name}.${fieldDef.name} is deprecated.` +
  31. (reason ? ' ' + reason : ''),
  32. node,
  33. ),
  34. );
  35. }
  36. }
  37. },
  38. EnumValue(node) {
  39. const enumVal = typeInfo.getEnumValue();
  40. if (enumVal && enumVal.isDeprecated) {
  41. const type = getNamedType(typeInfo.getInputType());
  42. if (type) {
  43. const reason = enumVal.deprecationReason;
  44. errors.push(
  45. new GraphQLError(
  46. `The enum value ${type.name}.${enumVal.name} is deprecated.` +
  47. (reason ? ' ' + reason : ''),
  48. node,
  49. ),
  50. );
  51. }
  52. }
  53. },
  54. }),
  55. );
  56. return errors;
  57. }