|
7 | 7 |
|
8 | 8 | namespace Magento\CatalogGraphQl\Model\Resolver\Products\Query;
|
9 | 9 |
|
10 |
| -use GraphQL\Language\AST\SelectionNode; |
11 | 10 | use Magento\Framework\GraphQl\Query\FieldTranslator;
|
12 | 11 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
13 | 12 |
|
@@ -37,57 +36,18 @@ public function __construct(FieldTranslator $fieldTranslator)
|
37 | 36 | */
|
38 | 37 | public function getProductsFieldSelection(ResolveInfo $resolveInfo): array
|
39 | 38 | {
|
40 |
| - return $this->getProductFields($resolveInfo); |
41 |
| - } |
| 39 | + $productFields = $resolveInfo->getFieldSelection(1); |
| 40 | + $sectionNames = ['items', 'product']; |
42 | 41 |
|
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 |
| - { |
51 | 42 | $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); |
85 | 47 | }
|
86 |
| - continue; |
87 | 48 | }
|
88 |
| - $fieldNames[] = $this->fieldTranslator->translate($itemSelection->name->value); |
89 | 49 | }
|
90 | 50 |
|
91 |
| - return $fieldNames; |
| 51 | + return array_unique($fieldNames); |
92 | 52 | }
|
93 | 53 | }
|
0 commit comments