Skip to content

Commit

Permalink
Fixed support for inline fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcop1 committed Oct 23, 2024
1 parent 9f4a052 commit ea9f24c
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions src/GraphQL/FieldHelper/AbstractFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,52 @@ public function processSelections(&$data, $selections, $container, $args, $conte
}

foreach ($selections as $selectionNode) {
if ($selectionNode instanceof FieldNode) {
$this->doExtractData($selectionNode, $data, $container, $args, $context, $resolveInfo);
} elseif ($selectionNode instanceof InlineFragmentNode) {
$inlineSelectionSetNode = $selectionNode->selectionSet;
/** @var NodeList $inlineSelections */
$inlineSelections = $inlineSelectionSetNode->selections;
$count = $inlineSelections->count();
for ($i = 0; $i < $count; $i++) {
$inlineNode = $inlineSelections[$i];
if ($inlineNode instanceof FieldNode) {
$this->doExtractData($inlineNode, $data, $container, $args, $context, $resolveInfo);
}
$this->processSelectionNode($data, $selectionNode, $container, $args, $context, $resolveInfo);
}
}

private function processSelectionNode(
&$data,
$selectionNode,
$container,
$args,
$context,
ResolveInfo $resolveInfo
): void
{
if(!$selectionNode) {
return;
}

if ($selectionNode instanceof FieldNode) {
$this->doExtractData($selectionNode, $data, $container, $args, $context, $resolveInfo);
} elseif ($selectionNode instanceof InlineFragmentNode) {
$inlineSelectionSetNode = $selectionNode->selectionSet;
/** @var NodeList $inlineSelections */
$inlineSelections = $inlineSelectionSetNode->selections;
$count = $inlineSelections->count();
for ($i = 0; $i < $count; $i++) {
$inlineNode = $inlineSelections[$i];
if ($inlineNode instanceof FieldNode) {
$this->doExtractData($inlineNode, $data, $container, $args, $context, $resolveInfo);

continue;
}
} elseif ($selectionNode instanceof FragmentSpreadNode) {
$fragmentName = $selectionNode->name->value;
$knownFragments = $resolveInfo->fragments;
$resolvedFragment = $knownFragments[$fragmentName];
if ($resolvedFragment) {
$fragmentSelectionSet = $resolvedFragment->selectionSet;
$fragmentSelections = $fragmentSelectionSet->selections;
$this->processSelections($data, $fragmentSelections, $container, $args, $context, $resolveInfo);
if($inlineNode instanceof FragmentSpreadNode) {
$this->processSelectionNode($data, $inlineNode, $container, $args, $context, $resolveInfo);

continue;
}
}
} elseif ($selectionNode instanceof FragmentSpreadNode) {
$fragmentName = $selectionNode->name->value;
$knownFragments = $resolveInfo->fragments;
$resolvedFragment = $knownFragments[$fragmentName];
if ($resolvedFragment) {
$fragmentSelectionSet = $resolvedFragment->selectionSet;
$fragmentSelections = $fragmentSelectionSet->selections;
$this->processSelections($data, $fragmentSelections, $container, $args, $context, $resolveInfo);
}
}
}
}

0 comments on commit ea9f24c

Please sign in to comment.