Files
GermanAirlinesVA-GraphQL/vendor/webonyx/graphql-php/src/Validator/Rules/UniqueVariableNames.php
T
Your Name aae17f10a6 new Deps
2021-07-26 19:46:18 +02:00

46 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error;
use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Validator\ValidationContext;
use function sprintf;
class UniqueVariableNames extends ValidationRule
{
/** @var NameNode[] */
public $knownVariableNames;
public function getVisitor(ValidationContext $context)
{
$this->knownVariableNames = [];
return [
NodeKind::OPERATION_DEFINITION => function () : void {
$this->knownVariableNames = [];
},
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use ($context) : void {
$variableName = $node->variable->name->value;
if (! isset($this->knownVariableNames[$variableName])) {
$this->knownVariableNames[$variableName] = $node->variable->name;
} else {
$context->reportError(new Error(
self::duplicateVariableMessage($variableName),
[$this->knownVariableNames[$variableName], $node->variable->name]
));
}
},
];
}
public static function duplicateVariableMessage($variableName)
{
return sprintf('There can be only one variable named "%s".', $variableName);
}
}