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,20 +1,21 @@
|
||||
<?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\OperationDefinitionNode;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Language\VisitorOperation;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class UniqueOperationNames extends AbstractValidationRule
|
||||
class UniqueOperationNames extends ValidationRule
|
||||
{
|
||||
static function duplicateOperationNameMessage($operationName)
|
||||
{
|
||||
return "There can be only one operation named \"$operationName\".";
|
||||
}
|
||||
|
||||
/** @var NameNode[] */
|
||||
public $knownOperationNames;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
@@ -22,24 +23,30 @@ class UniqueOperationNames extends AbstractValidationRule
|
||||
$this->knownOperationNames = [];
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => function(OperationDefinitionNode $node) use ($context) {
|
||||
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use ($context) : VisitorOperation {
|
||||
$operationName = $node->name;
|
||||
|
||||
if ($operationName) {
|
||||
if (!empty($this->knownOperationNames[$operationName->value])) {
|
||||
if ($operationName !== null) {
|
||||
if (! isset($this->knownOperationNames[$operationName->value])) {
|
||||
$this->knownOperationNames[$operationName->value] = $operationName;
|
||||
} else {
|
||||
$context->reportError(new Error(
|
||||
self::duplicateOperationNameMessage($operationName->value),
|
||||
[ $this->knownOperationNames[$operationName->value], $operationName ]
|
||||
[$this->knownOperationNames[$operationName->value], $operationName]
|
||||
));
|
||||
} else {
|
||||
$this->knownOperationNames[$operationName->value] = $operationName;
|
||||
}
|
||||
}
|
||||
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function() {
|
||||
NodeKind::FRAGMENT_DEFINITION => static function () : VisitorOperation {
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function duplicateOperationNameMessage($operationName)
|
||||
{
|
||||
return sprintf('There can be only one operation named "%s".', $operationName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user