UniqueOperationNames.mjs 994 B

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