NoDeprecatedCustomRule.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.NoDeprecatedCustomRule = NoDeprecatedCustomRule;
  6. var _invariant = require('../../../jsutils/invariant.js');
  7. var _GraphQLError = require('../../../error/GraphQLError.js');
  8. var _definition = require('../../../type/definition.js');
  9. /**
  10. * No deprecated
  11. *
  12. * A GraphQL document is only valid if all selected fields and all used enum values have not been
  13. * deprecated.
  14. *
  15. * Note: This rule is optional and is not part of the Validation section of the GraphQL
  16. * Specification. The main purpose of this rule is detection of deprecated usages and not
  17. * necessarily to forbid their use when querying a service.
  18. */
  19. function NoDeprecatedCustomRule(context) {
  20. return {
  21. Field(node) {
  22. const fieldDef = context.getFieldDef();
  23. const deprecationReason =
  24. fieldDef === null || fieldDef === void 0
  25. ? void 0
  26. : fieldDef.deprecationReason;
  27. if (fieldDef && deprecationReason != null) {
  28. const parentType = context.getParentType();
  29. parentType != null || (0, _invariant.invariant)(false);
  30. context.reportError(
  31. new _GraphQLError.GraphQLError(
  32. `The field ${parentType.name}.${fieldDef.name} is deprecated. ${deprecationReason}`,
  33. {
  34. nodes: node,
  35. },
  36. ),
  37. );
  38. }
  39. },
  40. Argument(node) {
  41. const argDef = context.getArgument();
  42. const deprecationReason =
  43. argDef === null || argDef === void 0
  44. ? void 0
  45. : argDef.deprecationReason;
  46. if (argDef && deprecationReason != null) {
  47. const directiveDef = context.getDirective();
  48. if (directiveDef != null) {
  49. context.reportError(
  50. new _GraphQLError.GraphQLError(
  51. `Directive "@${directiveDef.name}" argument "${argDef.name}" is deprecated. ${deprecationReason}`,
  52. {
  53. nodes: node,
  54. },
  55. ),
  56. );
  57. } else {
  58. const parentType = context.getParentType();
  59. const fieldDef = context.getFieldDef();
  60. (parentType != null && fieldDef != null) ||
  61. (0, _invariant.invariant)(false);
  62. context.reportError(
  63. new _GraphQLError.GraphQLError(
  64. `Field "${parentType.name}.${fieldDef.name}" argument "${argDef.name}" is deprecated. ${deprecationReason}`,
  65. {
  66. nodes: node,
  67. },
  68. ),
  69. );
  70. }
  71. }
  72. },
  73. ObjectField(node) {
  74. const inputObjectDef = (0, _definition.getNamedType)(
  75. context.getParentInputType(),
  76. );
  77. if ((0, _definition.isInputObjectType)(inputObjectDef)) {
  78. const inputFieldDef = inputObjectDef.getFields()[node.name.value];
  79. const deprecationReason =
  80. inputFieldDef === null || inputFieldDef === void 0
  81. ? void 0
  82. : inputFieldDef.deprecationReason;
  83. if (deprecationReason != null) {
  84. context.reportError(
  85. new _GraphQLError.GraphQLError(
  86. `The input field ${inputObjectDef.name}.${inputFieldDef.name} is deprecated. ${deprecationReason}`,
  87. {
  88. nodes: node,
  89. },
  90. ),
  91. );
  92. }
  93. }
  94. },
  95. EnumValue(node) {
  96. const enumValueDef = context.getEnumValue();
  97. const deprecationReason =
  98. enumValueDef === null || enumValueDef === void 0
  99. ? void 0
  100. : enumValueDef.deprecationReason;
  101. if (enumValueDef && deprecationReason != null) {
  102. const enumTypeDef = (0, _definition.getNamedType)(
  103. context.getInputType(),
  104. );
  105. enumTypeDef != null || (0, _invariant.invariant)(false);
  106. context.reportError(
  107. new _GraphQLError.GraphQLError(
  108. `The enum value "${enumTypeDef.name}.${enumValueDef.name}" is deprecated. ${deprecationReason}`,
  109. {
  110. nodes: node,
  111. },
  112. ),
  113. );
  114. }
  115. },
  116. };
  117. }