findDeprecatedUsages.js 1.6 KB

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