Skip to content

Commit 2ed1a1c

Browse files
ENGCOM-6535: FIX issue#26217 - Wrong fields selection while using fragments on GraphQL #26218
- Merge Pull Request #26218 from phoenix128/magento2:issue-26217-fields-selection-graphql-fragments - Merged commits: 1. 2b5720c 2. 631282e
2 parents 7ccd640 + 631282e commit 2ed1a1c

File tree

2 files changed

+61
-47
lines changed

2 files changed

+61
-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
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Catalog;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
/**
13+
* Test for simple product fragment.
14+
*/
15+
class ProductFragmentTest extends GraphQlAbstract
16+
{
17+
/**
18+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
19+
*/
20+
public function testSimpleProductFragment()
21+
{
22+
$sku = 'simple';
23+
$name = 'Simple Product';
24+
$price = 10;
25+
26+
$query = <<<QUERY
27+
query GetProduct {
28+
products(filter: { sku: { eq: "$sku" } }) {
29+
items {
30+
sku
31+
...BasicProductInformation
32+
}
33+
}
34+
}
35+
36+
fragment BasicProductInformation on ProductInterface {
37+
sku
38+
name
39+
price {
40+
regularPrice {
41+
amount {
42+
value
43+
}
44+
}
45+
}
46+
}
47+
QUERY;
48+
$result = $this->graphQlQuery($query);
49+
$actualProductData = $result['products']['items'][0];
50+
$this->assertNotEmpty($actualProductData);
51+
$this->assertEquals($name, $actualProductData['name']);
52+
$this->assertEquals($price, $actualProductData['price']['regularPrice']['amount']['value']);
53+
}
54+
}

0 commit comments

Comments
 (0)