public function __invoke(ValidationContext $context) { $fragmentDefs = []; $spreadsWithinOperation = []; $fragAdjacencies = new \stdClass(); $spreadNames = new \stdClass(); return [Node::OPERATION_DEFINITION => function () use(&$spreadNames, &$spreadsWithinOperation) { $spreadNames = new \stdClass(); $spreadsWithinOperation[] = $spreadNames; }, Node::FRAGMENT_DEFINITION => function (FragmentDefinition $def) use(&$fragmentDefs, &$spreadNames, &$fragAdjacencies) { $fragmentDefs[] = $def; $spreadNames = new \stdClass(); $fragAdjacencies->{$def->name->value} = $spreadNames; }, Node::FRAGMENT_SPREAD => function (FragmentSpread $spread) use(&$spreadNames) { $spreadNames->{$spread->name->value} = true; }, Node::DOCUMENT => ['leave' => function () use(&$fragAdjacencies, &$spreadsWithinOperation, &$fragmentDefs) { $fragmentNameUsed = []; foreach ($spreadsWithinOperation as $spreads) { $this->reduceSpreadFragments($spreads, $fragmentNameUsed, $fragAdjacencies); } $errors = []; foreach ($fragmentDefs as $def) { if (empty($fragmentNameUsed[$def->name->value])) { $errors[] = new Error(Messages::unusedFragMessage($def->name->value), [$def]); } } return !empty($errors) ? $errors : null; }]]; }
private function unusedFrag($fragName, $line, $column) { return new FormattedError(Messages::unusedFragMessage($fragName), [new SourceLocation($line, $column)]); }