NoDeprecatedCustomRule.mjs 3.6 KB

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