KnownFragmentNamesRule.mjs 688 B

1234567891011121314151617181920212223242526
  1. import { GraphQLError } from '../../error/GraphQLError.mjs';
  2. /**
  3. * Known fragment names
  4. *
  5. * A GraphQL document is only valid if all `...Fragment` fragment spreads refer
  6. * to fragments defined in the same document.
  7. *
  8. * See https://spec.graphql.org/draft/#sec-Fragment-spread-target-defined
  9. */
  10. export function KnownFragmentNamesRule(context) {
  11. return {
  12. FragmentSpread(node) {
  13. const fragmentName = node.name.value;
  14. const fragment = context.getFragment(fragmentName);
  15. if (!fragment) {
  16. context.reportError(
  17. new GraphQLError(`Unknown fragment "${fragmentName}".`, {
  18. nodes: node.name,
  19. }),
  20. );
  21. }
  22. },
  23. };
  24. }