UniqueFragmentNamesRule.mjs 798 B

1234567891011121314151617181920212223242526
  1. import { GraphQLError } from "../../error/GraphQLError.mjs";
  2. /**
  3. * Unique fragment names
  4. *
  5. * A GraphQL document is only valid if all defined fragments have unique names.
  6. */
  7. export function UniqueFragmentNamesRule(context) {
  8. var knownFragmentNames = Object.create(null);
  9. return {
  10. OperationDefinition: function OperationDefinition() {
  11. return false;
  12. },
  13. FragmentDefinition: function FragmentDefinition(node) {
  14. var fragmentName = node.name.value;
  15. if (knownFragmentNames[fragmentName]) {
  16. context.reportError(new GraphQLError("There can be only one fragment named \"".concat(fragmentName, "\"."), [knownFragmentNames[fragmentName], node.name]));
  17. } else {
  18. knownFragmentNames[fragmentName] = node.name;
  19. }
  20. return false;
  21. }
  22. };
  23. }