All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.validation.rules.UniqueVariableNamesRule Maven / Gradle / Ivy

package graphql.validation.rules;

import graphql.Internal;
import graphql.language.OperationDefinition;
import graphql.language.VariableDefinition;
import graphql.validation.AbstractRule;
import graphql.validation.ValidationContext;
import graphql.validation.ValidationErrorCollector;
import graphql.validation.ValidationErrorType;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Unique variable names
 * 

* A GraphQL operation is only valid if all its variables are uniquely named. */ @Internal public class UniqueVariableNamesRule extends AbstractRule { public UniqueVariableNamesRule(ValidationContext validationContext, ValidationErrorCollector validationErrorCollector) { super(validationContext, validationErrorCollector); } @Override public void checkOperationDefinition(OperationDefinition operationDefinition) { List variableDefinitions = operationDefinition.getVariableDefinitions(); if (variableDefinitions == null || variableDefinitions.size() <= 1) { return; } Set variableNameList = new LinkedHashSet<>(variableDefinitions.size()); for (VariableDefinition variableDefinition : variableDefinitions) { if (variableNameList.contains(variableDefinition.getName())) { addError(ValidationErrorType.DuplicateVariableName, variableDefinition.getSourceLocation(), duplicateVariableNameMessage(variableDefinition.getName())); } else { variableNameList.add(variableDefinition.getName()); } } } static String duplicateVariableNameMessage(String variableName) { return String.format("There can be only one variable named '%s'", variableName); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy