NoDeprecatedCustomRule.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. import { GraphQLError } from "../../../error/GraphQLError.mjs";
  2. import { getNamedType } from "../../../type/definition.mjs";
  3. /**
  4. * No deprecated
  5. *
  6. * A GraphQL document is only valid if all selected fields and all used enum values have not been
  7. * deprecated.
  8. *
  9. * Note: This rule is optional and is not part of the Validation section of the GraphQL
  10. * Specification. The main purpose of this rule is detection of deprecated usages and not
  11. * necessarily to forbid their use when querying a service.
  12. */
  13. export function NoDeprecatedCustomRule(context) {
  14. return {
  15. Field: function Field(node) {
  16. var fieldDef = context.getFieldDef();
  17. var parentType = context.getParentType();
  18. if (parentType && (fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.deprecationReason) != null) {
  19. context.reportError(new GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated. ") + fieldDef.deprecationReason, node));
  20. }
  21. },
  22. EnumValue: function EnumValue(node) {
  23. var type = getNamedType(context.getInputType());
  24. var enumValue = context.getEnumValue();
  25. if (type && (enumValue === null || enumValue === void 0 ? void 0 : enumValue.deprecationReason) != null) {
  26. context.reportError(new GraphQLError("The enum value \"".concat(type.name, ".").concat(enumValue.name, "\" is deprecated. ") + enumValue.deprecationReason, node));
  27. }
  28. }
  29. };
  30. }