UniqueOperationNamesRule.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.UniqueOperationNamesRule = UniqueOperationNamesRule;
  6. var _GraphQLError = require('../../error/GraphQLError.js');
  7. /**
  8. * Unique operation names
  9. *
  10. * A GraphQL document is only valid if all defined operations have unique names.
  11. *
  12. * See https://spec.graphql.org/draft/#sec-Operation-Name-Uniqueness
  13. */
  14. function UniqueOperationNamesRule(context) {
  15. const knownOperationNames = Object.create(null);
  16. return {
  17. OperationDefinition(node) {
  18. const operationName = node.name;
  19. if (operationName) {
  20. if (knownOperationNames[operationName.value]) {
  21. context.reportError(
  22. new _GraphQLError.GraphQLError(
  23. `There can be only one operation named "${operationName.value}".`,
  24. {
  25. nodes: [
  26. knownOperationNames[operationName.value],
  27. operationName,
  28. ],
  29. },
  30. ),
  31. );
  32. } else {
  33. knownOperationNames[operationName.value] = operationName;
  34. }
  35. }
  36. return false;
  37. },
  38. FragmentDefinition: () => false,
  39. };
  40. }