UniqueVariableNamesRule.mjs 831 B

123456789101112131415161718192021222324
  1. import { GraphQLError } from "../../error/GraphQLError.mjs";
  2. /**
  3. * Unique variable names
  4. *
  5. * A GraphQL operation is only valid if all its variables are uniquely named.
  6. */
  7. export function UniqueVariableNamesRule(context) {
  8. var knownVariableNames = Object.create(null);
  9. return {
  10. OperationDefinition: function OperationDefinition() {
  11. knownVariableNames = Object.create(null);
  12. },
  13. VariableDefinition: function VariableDefinition(node) {
  14. var variableName = node.variable.name.value;
  15. if (knownVariableNames[variableName]) {
  16. context.reportError(new GraphQLError("There can be only one variable named \"$".concat(variableName, "\"."), [knownVariableNames[variableName], node.variable.name]));
  17. } else {
  18. knownVariableNames[variableName] = node.variable.name;
  19. }
  20. }
  21. };
  22. }