GraphQL\Validator\ValidationContext::getFragmentSpreads PHP Method

getFragmentSpreads() public method

public getFragmentSpreads ( GraphQL\Language\AST\HasSelectionSet $node ) : FragmentSpreadNode[]
$node GraphQL\Language\AST\HasSelectionSet
return GraphQL\Language\AST\FragmentSpreadNode[]
    function getFragmentSpreads(HasSelectionSet $node)
    {
        $spreads = isset($this->fragmentSpreads[$node]) ? $this->fragmentSpreads[$node] : null;
        if (!$spreads) {
            $spreads = [];
            $setsToVisit = [$node->selectionSet];
            while (!empty($setsToVisit)) {
                $set = array_pop($setsToVisit);
                for ($i = 0; $i < count($set->selections); $i++) {
                    $selection = $set->selections[$i];
                    if ($selection->kind === NodeKind::FRAGMENT_SPREAD) {
                        $spreads[] = $selection;
                    } else {
                        if ($selection->selectionSet) {
                            $setsToVisit[] = $selection->selectionSet;
                        }
                    }
                }
            }
            $this->fragmentSpreads[$node] = $spreads;
        }
        return $spreads;
    }

Usage Example

Exemplo n.º 1
0
 private function detectCycleRecursive(FragmentDefinition $fragment, ValidationContext $context)
 {
     $fragmentName = $fragment->name->value;
     $this->visitedFrags[$fragmentName] = true;
     $spreadNodes = $context->getFragmentSpreads($fragment);
     if (empty($spreadNodes)) {
         return;
     }
     $this->spreadPathIndexByName[$fragmentName] = count($this->spreadPath);
     for ($i = 0; $i < count($spreadNodes); $i++) {
         $spreadNode = $spreadNodes[$i];
         $spreadName = $spreadNode->name->value;
         $cycleIndex = isset($this->spreadPathIndexByName[$spreadName]) ? $this->spreadPathIndexByName[$spreadName] : null;
         if ($cycleIndex === null) {
             $this->spreadPath[] = $spreadNode;
             if (empty($this->visitedFrags[$spreadName])) {
                 $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;
             }
             $context->reportError(new Error(self::cycleErrorMessage($spreadName, Utils::map($cyclePath, function ($s) {
                 return $s->name->value;
             })), $nodes));
         }
     }
     $this->spreadPathIndexByName[$fragmentName] = null;
 }