UniqueOperationNamesRule.mjs 882 B

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