Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
@@ -1,51 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NameNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\ObjectFieldNode;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Language\VisitorOperation;
|
||||
use GraphQL\Validator\ASTValidationContext;
|
||||
use GraphQL\Validator\SDLValidationContext;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_pop;
|
||||
use function sprintf;
|
||||
|
||||
class UniqueInputFieldNames extends AbstractValidationRule
|
||||
class UniqueInputFieldNames extends ValidationRule
|
||||
{
|
||||
static function duplicateInputFieldMessage($fieldName)
|
||||
{
|
||||
return "There can be only one input field named \"$fieldName\".";
|
||||
}
|
||||
|
||||
/** @var array<string, NameNode> */
|
||||
public $knownNames;
|
||||
|
||||
/** @var array<array<string, NameNode>> */
|
||||
public $knownNameStack;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$this->knownNames = [];
|
||||
return $this->getASTVisitor($context);
|
||||
}
|
||||
|
||||
public function getSDLVisitor(SDLValidationContext $context)
|
||||
{
|
||||
return $this->getASTVisitor($context);
|
||||
}
|
||||
|
||||
public function getASTVisitor(ASTValidationContext $context)
|
||||
{
|
||||
$this->knownNames = [];
|
||||
$this->knownNameStack = [];
|
||||
|
||||
return [
|
||||
NodeKind::OBJECT => [
|
||||
'enter' => function() {
|
||||
NodeKind::OBJECT => [
|
||||
'enter' => function () : void {
|
||||
$this->knownNameStack[] = $this->knownNames;
|
||||
$this->knownNames = [];
|
||||
$this->knownNames = [];
|
||||
},
|
||||
'leave' => function() {
|
||||
'leave' => function () : void {
|
||||
$this->knownNames = array_pop($this->knownNameStack);
|
||||
}
|
||||
},
|
||||
],
|
||||
NodeKind::OBJECT_FIELD => function(ObjectFieldNode $node) use ($context) {
|
||||
NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) use ($context) : VisitorOperation {
|
||||
$fieldName = $node->name->value;
|
||||
|
||||
if (!empty($this->knownNames[$fieldName])) {
|
||||
if (isset($this->knownNames[$fieldName])) {
|
||||
$context->reportError(new Error(
|
||||
self::duplicateInputFieldMessage($fieldName),
|
||||
[ $this->knownNames[$fieldName], $node->name ]
|
||||
[$this->knownNames[$fieldName], $node->name]
|
||||
));
|
||||
} else {
|
||||
$this->knownNames[$fieldName] = $node->name;
|
||||
}
|
||||
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function duplicateInputFieldMessage($fieldName)
|
||||
{
|
||||
return sprintf('There can be only one input field named "%s".', $fieldName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user