Skip to content

Commit 2b5720c

Browse files
committed
FIX issue#26217 - Wrong fields selection while using fragments on GraphQL products query
1 parent 10ab84e commit 2b5720c

File tree

1 file changed

+7
-47
lines changed

1 file changed

+7
-47
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/FieldSelection.php

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Products\Query;
99

10-
use GraphQL\Language\AST\SelectionNode;
1110
use Magento\Framework\GraphQl\Query\FieldTranslator;
1211
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1312

@@ -37,57 +36,18 @@ public function __construct(FieldTranslator $fieldTranslator)
3736
*/
3837
public function getProductsFieldSelection(ResolveInfo $resolveInfo): array
3938
{
40-
return $this->getProductFields($resolveInfo);
41-
}
39+
$productFields = $resolveInfo->getFieldSelection(1);
40+
$sectionNames = ['items', 'product'];
4241

43-
/**
44-
* Return field names for all requested product fields.
45-
*
46-
* @param ResolveInfo $info
47-
* @return string[]
48-
*/
49-
private function getProductFields(ResolveInfo $info): array
50-
{
5142
$fieldNames = [];
52-
foreach ($info->fieldNodes as $node) {
53-
if ($node->name->value !== 'products' && $node->name->value !== 'variants') {
54-
continue;
55-
}
56-
foreach ($node->selectionSet->selections as $selection) {
57-
if ($selection->name->value !== 'items' && $selection->name->value !== 'product') {
58-
continue;
59-
}
60-
$fieldNames[] = $this->collectProductFieldNames($selection, $fieldNames);
61-
}
62-
}
63-
if (!empty($fieldNames)) {
64-
$fieldNames = array_merge(...$fieldNames);
65-
}
66-
return $fieldNames;
67-
}
68-
69-
/**
70-
* Collect field names for each node in selection
71-
*
72-
* @param SelectionNode $selection
73-
* @param array $fieldNames
74-
* @return array
75-
*/
76-
private function collectProductFieldNames(SelectionNode $selection, array $fieldNames = []): array
77-
{
78-
foreach ($selection->selectionSet->selections as $itemSelection) {
79-
if ($itemSelection->kind === 'InlineFragment') {
80-
foreach ($itemSelection->selectionSet->selections as $inlineSelection) {
81-
if ($inlineSelection->kind === 'InlineFragment') {
82-
continue;
83-
}
84-
$fieldNames[] = $this->fieldTranslator->translate($inlineSelection->name->value);
43+
foreach ($sectionNames as $sectionName) {
44+
if (isset($productFields[$sectionName])) {
45+
foreach (array_keys($productFields[$sectionName]) as $fieldName) {
46+
$fieldNames[] = $this->fieldTranslator->translate($fieldName);
8547
}
86-
continue;
8748
}
88-
$fieldNames[] = $this->fieldTranslator->translate($itemSelection->name->value);
8949
}
9050

91-
return $fieldNames;
51+
return array_unique($fieldNames);
9252
}
9353
}

0 commit comments

Comments
 (0)