Skip to content

FIX issue#26217 - Wrong fields selection while using fragments on GraphQL #26218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

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

use GraphQL\Language\AST\SelectionNode;
use Magento\Framework\GraphQl\Query\FieldTranslator;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

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

/**
* Return field names for all requested product fields.
*
* @param ResolveInfo $info
* @return string[]
*/
private function getProductFields(ResolveInfo $info): array
{
$fieldNames = [];
foreach ($info->fieldNodes as $node) {
if ($node->name->value !== 'products' && $node->name->value !== 'variants') {
continue;
}
foreach ($node->selectionSet->selections as $selection) {
if ($selection->name->value !== 'items' && $selection->name->value !== 'product') {
continue;
}
$fieldNames[] = $this->collectProductFieldNames($selection, $fieldNames);
}
}
if (!empty($fieldNames)) {
$fieldNames = array_merge(...$fieldNames);
}
return $fieldNames;
}

/**
* Collect field names for each node in selection
*
* @param SelectionNode $selection
* @param array $fieldNames
* @return array
*/
private function collectProductFieldNames(SelectionNode $selection, array $fieldNames = []): array
{
foreach ($selection->selectionSet->selections as $itemSelection) {
if ($itemSelection->kind === 'InlineFragment') {
foreach ($itemSelection->selectionSet->selections as $inlineSelection) {
if ($inlineSelection->kind === 'InlineFragment') {
continue;
}
$fieldNames[] = $this->fieldTranslator->translate($inlineSelection->name->value);
foreach ($sectionNames as $sectionName) {
if (isset($productFields[$sectionName])) {
foreach (array_keys($productFields[$sectionName]) as $fieldName) {
$fieldNames[] = $this->fieldTranslator->translate($fieldName);
}
continue;
}
$fieldNames[] = $this->fieldTranslator->translate($itemSelection->name->value);
}

return $fieldNames;
return array_unique($fieldNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for simple product fragment.
*/
class ProductFragmentTest extends GraphQlAbstract
{
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testSimpleProductFragment()
{
$sku = 'simple';
$name = 'Simple Product';
$price = 10;

$query = <<<QUERY
query GetProduct {
products(filter: { sku: { eq: "$sku" } }) {
items {
sku
...BasicProductInformation
}
}
}

fragment BasicProductInformation on ProductInterface {
sku
name
price {
regularPrice {
amount {
value
}
}
}
}
QUERY;
$result = $this->graphQlQuery($query);
$actualProductData = $result['products']['items'][0];
$this->assertNotEmpty($actualProductData);
$this->assertEquals($name, $actualProductData['name']);
$this->assertEquals($price, $actualProductData['price']['regularPrice']['amount']['value']);
}
}