KnownFragmentNamesRule.js.flow 742 B

1234567891011121314151617181920212223242526
  1. // @flow strict
  2. import { GraphQLError } from '../../error/GraphQLError';
  3. import type { ASTVisitor } from '../../language/visitor';
  4. import type { ValidationContext } from '../ValidationContext';
  5. /**
  6. * Known fragment names
  7. *
  8. * A GraphQL document is only valid if all `...Fragment` fragment spreads refer
  9. * to fragments defined in the same document.
  10. */
  11. export function KnownFragmentNamesRule(context: ValidationContext): ASTVisitor {
  12. return {
  13. FragmentSpread(node) {
  14. const fragmentName = node.name.value;
  15. const fragment = context.getFragment(fragmentName);
  16. if (!fragment) {
  17. context.reportError(
  18. new GraphQLError(`Unknown fragment "${fragmentName}".`, node.name),
  19. );
  20. }
  21. },
  22. };
  23. }