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,21 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\ArgumentNode;
|
||||
use GraphQL\Language\AST\FragmentDefinitionNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NameNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Language\VisitorOperation;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class UniqueFragmentNames extends AbstractValidationRule
|
||||
class UniqueFragmentNames extends ValidationRule
|
||||
{
|
||||
static function duplicateFragmentNameMessage($fragName)
|
||||
{
|
||||
return "There can be only one fragment named \"$fragName\".";
|
||||
}
|
||||
|
||||
/** @var NameNode[] */
|
||||
public $knownFragmentNames;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
@@ -23,21 +23,27 @@ class UniqueFragmentNames extends AbstractValidationRule
|
||||
$this->knownFragmentNames = [];
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => function () {
|
||||
NodeKind::OPERATION_DEFINITION => static function () : VisitorOperation {
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) : VisitorOperation {
|
||||
$fragmentName = $node->name->value;
|
||||
if (!empty($this->knownFragmentNames[$fragmentName])) {
|
||||
if (! isset($this->knownFragmentNames[$fragmentName])) {
|
||||
$this->knownFragmentNames[$fragmentName] = $node->name;
|
||||
} else {
|
||||
$context->reportError(new Error(
|
||||
self::duplicateFragmentNameMessage($fragmentName),
|
||||
[ $this->knownFragmentNames[$fragmentName], $node->name ]
|
||||
[$this->knownFragmentNames[$fragmentName], $node->name]
|
||||
));
|
||||
} else {
|
||||
$this->knownFragmentNames[$fragmentName] = $node->name;
|
||||
}
|
||||
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function duplicateFragmentNameMessage($fragName)
|
||||
{
|
||||
return sprintf('There can be only one fragment named "%s".', $fragName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user