NoDeprecatedCustomRule.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.NoDeprecatedCustomRule = NoDeprecatedCustomRule;
  6. var _invariant = _interopRequireDefault(require("../../../jsutils/invariant.js"));
  7. var _GraphQLError = require("../../../error/GraphQLError.js");
  8. var _definition = require("../../../type/definition.js");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * No deprecated
  12. *
  13. * A GraphQL document is only valid if all selected fields and all used enum values have not been
  14. * deprecated.
  15. *
  16. * Note: This rule is optional and is not part of the Validation section of the GraphQL
  17. * Specification. The main purpose of this rule is detection of deprecated usages and not
  18. * necessarily to forbid their use when querying a service.
  19. */
  20. function NoDeprecatedCustomRule(context) {
  21. return {
  22. Field: function Field(node) {
  23. var fieldDef = context.getFieldDef();
  24. var deprecationReason = fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.deprecationReason;
  25. if (fieldDef && deprecationReason != null) {
  26. var parentType = context.getParentType();
  27. parentType != null || (0, _invariant.default)(0);
  28. context.reportError(new _GraphQLError.GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated. ").concat(deprecationReason), node));
  29. }
  30. },
  31. Argument: function Argument(node) {
  32. var argDef = context.getArgument();
  33. var deprecationReason = argDef === null || argDef === void 0 ? void 0 : argDef.deprecationReason;
  34. if (argDef && deprecationReason != null) {
  35. var directiveDef = context.getDirective();
  36. if (directiveDef != null) {
  37. context.reportError(new _GraphQLError.GraphQLError("Directive \"@".concat(directiveDef.name, "\" argument \"").concat(argDef.name, "\" is deprecated. ").concat(deprecationReason), node));
  38. } else {
  39. var parentType = context.getParentType();
  40. var fieldDef = context.getFieldDef();
  41. parentType != null && fieldDef != null || (0, _invariant.default)(0);
  42. context.reportError(new _GraphQLError.GraphQLError("Field \"".concat(parentType.name, ".").concat(fieldDef.name, "\" argument \"").concat(argDef.name, "\" is deprecated. ").concat(deprecationReason), node));
  43. }
  44. }
  45. },
  46. ObjectField: function ObjectField(node) {
  47. var inputObjectDef = (0, _definition.getNamedType)(context.getParentInputType());
  48. if ((0, _definition.isInputObjectType)(inputObjectDef)) {
  49. var inputFieldDef = inputObjectDef.getFields()[node.name.value]; // flowlint-next-line unnecessary-optional-chain:off
  50. var deprecationReason = inputFieldDef === null || inputFieldDef === void 0 ? void 0 : inputFieldDef.deprecationReason;
  51. if (deprecationReason != null) {
  52. context.reportError(new _GraphQLError.GraphQLError("The input field ".concat(inputObjectDef.name, ".").concat(inputFieldDef.name, " is deprecated. ").concat(deprecationReason), node));
  53. }
  54. }
  55. },
  56. EnumValue: function EnumValue(node) {
  57. var enumValueDef = context.getEnumValue();
  58. var deprecationReason = enumValueDef === null || enumValueDef === void 0 ? void 0 : enumValueDef.deprecationReason;
  59. if (enumValueDef && deprecationReason != null) {
  60. var enumTypeDef = (0, _definition.getNamedType)(context.getInputType());
  61. enumTypeDef != null || (0, _invariant.default)(0);
  62. context.reportError(new _GraphQLError.GraphQLError("The enum value \"".concat(enumTypeDef.name, ".").concat(enumValueDef.name, "\" is deprecated. ").concat(deprecationReason), node));
  63. }
  64. }
  65. };
  66. }