UniqueFragmentNames.js 1.1 KB

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