KnownFragmentNames.js.flow 851 B

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