UniqueOperationNames.js 1.2 KB

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