NoDeprecatedCustomRule.js.flow 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // @flow strict
  2. import { GraphQLError } from '../../../error/GraphQLError';
  3. import type { EnumValueNode, FieldNode } from '../../../language/ast';
  4. import type { ASTVisitor } from '../../../language/visitor';
  5. import { getNamedType } from '../../../type/definition';
  6. import type { ValidationContext } from '../../ValidationContext';
  7. /**
  8. * No deprecated
  9. *
  10. * A GraphQL document is only valid if all selected fields and all used enum values have not been
  11. * deprecated.
  12. *
  13. * Note: This rule is optional and is not part of the Validation section of the GraphQL
  14. * Specification. The main purpose of this rule is detection of deprecated usages and not
  15. * necessarily to forbid their use when querying a service.
  16. */
  17. export function NoDeprecatedCustomRule(context: ValidationContext): ASTVisitor {
  18. return {
  19. Field(node: FieldNode) {
  20. const fieldDef = context.getFieldDef();
  21. const parentType = context.getParentType();
  22. if (parentType && fieldDef?.deprecationReason != null) {
  23. context.reportError(
  24. new GraphQLError(
  25. `The field ${parentType.name}.${fieldDef.name} is deprecated. ` +
  26. fieldDef.deprecationReason,
  27. node,
  28. ),
  29. );
  30. }
  31. },
  32. EnumValue(node: EnumValueNode) {
  33. const type = getNamedType(context.getInputType());
  34. const enumValue = context.getEnumValue();
  35. if (type && enumValue?.deprecationReason != null) {
  36. context.reportError(
  37. new GraphQLError(
  38. `The enum value "${type.name}.${enumValue.name}" is deprecated. ` +
  39. enumValue.deprecationReason,
  40. node,
  41. ),
  42. );
  43. }
  44. },
  45. };
  46. }