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,28 +1,32 @@
|
||||
<?php
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FragmentDefinitionNode;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Language\VisitorOperation;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_pop;
|
||||
use function array_slice;
|
||||
use function count;
|
||||
use function implode;
|
||||
use function sprintf;
|
||||
|
||||
class NoFragmentCycles extends AbstractValidationRule
|
||||
class NoFragmentCycles extends ValidationRule
|
||||
{
|
||||
static function cycleErrorMessage($fragName, array $spreadNames = [])
|
||||
{
|
||||
$via = !empty($spreadNames) ? ' via ' . implode(', ', $spreadNames) : '';
|
||||
return "Cannot spread fragment \"$fragName\" within itself$via.";
|
||||
}
|
||||
|
||||
/** @var bool[] */
|
||||
public $visitedFrags;
|
||||
|
||||
/** @var FragmentSpreadNode[] */
|
||||
public $spreadPath;
|
||||
|
||||
/** @var (int|null)[] */
|
||||
public $spreadPathIndexByName;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
@@ -38,26 +42,29 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
$this->spreadPathIndexByName = [];
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => function () {
|
||||
NodeKind::OPERATION_DEFINITION => static function () : VisitorOperation {
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {
|
||||
if (!isset($this->visitedFrags[$node->name->value])) {
|
||||
$this->detectCycleRecursive($node, $context);
|
||||
}
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) : VisitorOperation {
|
||||
$this->detectCycleRecursive($node, $context);
|
||||
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private function detectCycleRecursive(FragmentDefinitionNode $fragment, ValidationContext $context)
|
||||
{
|
||||
$fragmentName = $fragment->name->value;
|
||||
if (isset($this->visitedFrags[$fragment->name->value])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fragmentName = $fragment->name->value;
|
||||
$this->visitedFrags[$fragmentName] = true;
|
||||
|
||||
$spreadNodes = $context->getFragmentSpreads($fragment);
|
||||
|
||||
if (empty($spreadNodes)) {
|
||||
if (count($spreadNodes) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -66,39 +73,40 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
for ($i = 0; $i < count($spreadNodes); $i++) {
|
||||
$spreadNode = $spreadNodes[$i];
|
||||
$spreadName = $spreadNode->name->value;
|
||||
$cycleIndex = isset($this->spreadPathIndexByName[$spreadName]) ? $this->spreadPathIndexByName[$spreadName] : null;
|
||||
$cycleIndex = $this->spreadPathIndexByName[$spreadName] ?? null;
|
||||
|
||||
$this->spreadPath[] = $spreadNode;
|
||||
if ($cycleIndex === null) {
|
||||
$this->spreadPath[] = $spreadNode;
|
||||
if (empty($this->visitedFrags[$spreadName])) {
|
||||
$spreadFragment = $context->getFragment($spreadName);
|
||||
if ($spreadFragment) {
|
||||
$this->detectCycleRecursive($spreadFragment, $context);
|
||||
}
|
||||
$spreadFragment = $context->getFragment($spreadName);
|
||||
if ($spreadFragment) {
|
||||
$this->detectCycleRecursive($spreadFragment, $context);
|
||||
}
|
||||
array_pop($this->spreadPath);
|
||||
} else {
|
||||
$cyclePath = array_slice($this->spreadPath, $cycleIndex);
|
||||
$nodes = $cyclePath;
|
||||
|
||||
if (is_array($spreadNode)) {
|
||||
$nodes = array_merge($nodes, $spreadNode);
|
||||
} else {
|
||||
$nodes[] = $spreadNode;
|
||||
}
|
||||
$cyclePath = array_slice($this->spreadPath, $cycleIndex);
|
||||
$fragmentNames = Utils::map(array_slice($cyclePath, 0, -1), static function ($s) {
|
||||
return $s->name->value;
|
||||
});
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::cycleErrorMessage(
|
||||
$spreadName,
|
||||
Utils::map($cyclePath, function ($s) {
|
||||
return $s->name->value;
|
||||
})
|
||||
),
|
||||
$nodes
|
||||
self::cycleErrorMessage($spreadName, $fragmentNames),
|
||||
$cyclePath
|
||||
));
|
||||
}
|
||||
array_pop($this->spreadPath);
|
||||
}
|
||||
|
||||
$this->spreadPathIndexByName[$fragmentName] = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $spreadNames
|
||||
*/
|
||||
public static function cycleErrorMessage($fragName, array $spreadNames = [])
|
||||
{
|
||||
return sprintf(
|
||||
'Cannot spread fragment "%s" within itself%s.',
|
||||
$fragName,
|
||||
count($spreadNames) > 0 ? ' via ' . implode(', ', $spreadNames) : ''
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user