UniqueVariableNames.mjs 930 B

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