NoDeprecatedCustomRule.mjs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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: function Field(node) {
  17. var fieldDef = context.getFieldDef();
  18. var deprecationReason = fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.deprecationReason;
  19. if (fieldDef && deprecationReason != null) {
  20. var parentType = context.getParentType();
  21. parentType != null || invariant(0);
  22. context.reportError(new GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated. ").concat(deprecationReason), node));
  23. }
  24. },
  25. Argument: function Argument(node) {
  26. var argDef = context.getArgument();
  27. var deprecationReason = argDef === null || argDef === void 0 ? void 0 : argDef.deprecationReason;
  28. if (argDef && deprecationReason != null) {
  29. var directiveDef = context.getDirective();
  30. if (directiveDef != null) {
  31. context.reportError(new GraphQLError("Directive \"@".concat(directiveDef.name, "\" argument \"").concat(argDef.name, "\" is deprecated. ").concat(deprecationReason), node));
  32. } else {
  33. var parentType = context.getParentType();
  34. var fieldDef = context.getFieldDef();
  35. parentType != null && fieldDef != null || invariant(0);
  36. context.reportError(new GraphQLError("Field \"".concat(parentType.name, ".").concat(fieldDef.name, "\" argument \"").concat(argDef.name, "\" is deprecated. ").concat(deprecationReason), node));
  37. }
  38. }
  39. },
  40. ObjectField: function ObjectField(node) {
  41. var inputObjectDef = getNamedType(context.getParentInputType());
  42. if (isInputObjectType(inputObjectDef)) {
  43. var inputFieldDef = inputObjectDef.getFields()[node.name.value]; // flowlint-next-line unnecessary-optional-chain:off
  44. var deprecationReason = inputFieldDef === null || inputFieldDef === void 0 ? void 0 : inputFieldDef.deprecationReason;
  45. if (deprecationReason != null) {
  46. context.reportError(new GraphQLError("The input field ".concat(inputObjectDef.name, ".").concat(inputFieldDef.name, " is deprecated. ").concat(deprecationReason), node));
  47. }
  48. }
  49. },
  50. EnumValue: function EnumValue(node) {
  51. var enumValueDef = context.getEnumValue();
  52. var deprecationReason = enumValueDef === null || enumValueDef === void 0 ? void 0 : enumValueDef.deprecationReason;
  53. if (enumValueDef && deprecationReason != null) {
  54. var enumTypeDef = getNamedType(context.getInputType());
  55. enumTypeDef != null || invariant(0);
  56. context.reportError(new GraphQLError("The enum value \"".concat(enumTypeDef.name, ".").concat(enumValueDef.name, "\" is deprecated. ").concat(deprecationReason), node));
  57. }
  58. }
  59. };
  60. }