findDeprecatedUsages.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { GraphQLError } from '../error/GraphQLError';
  2. import { visit, visitWithTypeInfo } from '../language/visitor';
  3. import { getNamedType } from '../type/definition';
  4. import { 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. export function findDeprecatedUsages(schema, ast) {
  11. var errors = [];
  12. var typeInfo = new TypeInfo(schema);
  13. visit(ast, visitWithTypeInfo(typeInfo, {
  14. Field: function Field(node) {
  15. var fieldDef = typeInfo.getFieldDef();
  16. if (fieldDef && fieldDef.isDeprecated) {
  17. var parentType = typeInfo.getParentType();
  18. if (parentType) {
  19. var reason = fieldDef.deprecationReason;
  20. errors.push(new GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated.") + (reason ? ' ' + reason : ''), node));
  21. }
  22. }
  23. },
  24. EnumValue: function EnumValue(node) {
  25. var enumVal = typeInfo.getEnumValue();
  26. if (enumVal && enumVal.isDeprecated) {
  27. var type = getNamedType(typeInfo.getInputType());
  28. if (type) {
  29. var reason = enumVal.deprecationReason;
  30. errors.push(new GraphQLError("The enum value ".concat(type.name, ".").concat(enumVal.name, " is deprecated.") + (reason ? ' ' + reason : ''), node));
  31. }
  32. }
  33. }
  34. }));
  35. return errors;
  36. }