UniqueFragmentNames.mjs 898 B

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