Skip to content

Commit 1de0335

Browse files
committed
MC-35361: [GraphQL] - Grouped Products - No data returns for arrays like "product_links"
1 parent 3b19d14 commit 1de0335

File tree

6 files changed

+113
-37
lines changed

6 files changed

+113
-37
lines changed

app/code/Magento/CatalogGraphQl/Model/ProductLinksTypeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
1111

1212
/**
13-
* {@inheritdoc}
13+
* @inheritdoc
1414
*/
1515
class ProductLinksTypeResolver implements TypeResolverInterface
1616
{
@@ -20,9 +20,9 @@ class ProductLinksTypeResolver implements TypeResolverInterface
2020
private $linkTypes = ['related', 'upsell', 'crosssell'];
2121

2222
/**
23-
* {@inheritdoc}
23+
* @inheritdoc
2424
*/
25-
public function resolveType(array $data) : string
25+
public function resolveType(array $data): string
2626
{
2727
if (isset($data['link_type'])) {
2828
$linkType = $data['link_type'];

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/BatchProductLinks.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ class BatchProductLinks implements BatchServiceContractResolverInterface
2222
/**
2323
* @var string[]
2424
*/
25-
private static $linkTypes = ['related', 'upsell', 'crosssell'];
25+
private $linkTypes;
26+
27+
/**
28+
* @param array $linkTypes
29+
*/
30+
public function __construct(array $linkTypes)
31+
{
32+
$this->linkTypes = $linkTypes;
33+
}
2634

2735
/**
2836
* @inheritDoc
@@ -44,7 +52,7 @@ public function convertToServiceArgument(ResolveRequestInterface $request)
4452
/** @var \Magento\Catalog\Model\Product $product */
4553
$product = $value['model'];
4654

47-
return new ListCriteria((string)$product->getId(), self::$linkTypes, $product);
55+
return new ListCriteria((string)$product->getId(), $this->linkTypes, $product);
4856
}
4957

5058
/**

app/code/Magento/CatalogGraphQl/etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,14 @@
7474
<preference type="\Magento\CatalogGraphQl\Model\Resolver\Product\Price\Provider" for="\Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderInterface"/>
7575

7676
<preference type="Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search" for="Magento\CatalogGraphQl\Model\Resolver\Products\Query\ProductQueryInterface"/>
77+
78+
<type name="\Magento\CatalogGraphQl\Model\Resolver\Product\BatchProductLinks">
79+
<arguments>
80+
<argument name="linkTypes" xsi:type="array">
81+
<item name="related" xsi:type="string">related</item>
82+
<item name="upsell" xsi:type="string">upsell</item>
83+
<item name="crosssell" xsi:type="string">crosssell</item>
84+
</argument>
85+
</arguments>
86+
</type>
7787
</config>

app/code/Magento/GroupedProductGraphQl/Model/GroupedProductLinksTypeResolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
1111

1212
/**
13-
* {@inheritdoc}
13+
* @inheritdoc
1414
*/
1515
class GroupedProductLinksTypeResolver implements TypeResolverInterface
1616
{
@@ -20,14 +20,14 @@ class GroupedProductLinksTypeResolver implements TypeResolverInterface
2020
private $linkTypes = ['associated'];
2121

2222
/**
23-
* {@inheritdoc}
23+
* @inheritdoc
2424
*/
25-
public function resolveType(array $data) : string
25+
public function resolveType(array $data): string
2626
{
2727
if (isset($data['link_type'])) {
2828
$linkType = $data['link_type'];
2929
if (in_array($linkType, $this->linkTypes)) {
30-
return 'GroupedProductLinks';
30+
return 'ProductLinks';
3131
}
3232
}
3333
return '';

app/code/Magento/GroupedProductGraphQl/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="\Magento\CatalogGraphQl\Model\Resolver\Product\BatchProductLinks">
17+
<arguments>
18+
<argument name="linkTypes" xsi:type="array">
19+
<item name="associated" xsi:type="string">associated</item>
20+
</argument>
21+
</arguments>
22+
</type>
1623
</config>

dev/tests/api-functional/testsuite/Magento/GraphQl/GroupedProduct/GroupedProductViewTest.php

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,46 @@
77

88
namespace Magento\GraphQl\GroupedProduct;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\TestFramework\ObjectManager;
1213
use Magento\TestFramework\TestCase\GraphQlAbstract;
1314

15+
/**
16+
* Class to test GraphQl response with grouped products
17+
*/
1418
class GroupedProductViewTest extends GraphQlAbstract
1519
{
20+
/**
21+
* @var ProductRepositoryInterface
22+
*/
23+
private $productRepository;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
$this->productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
32+
}
1633

1734
/**
1835
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
1936
*/
2037
public function testAllFieldsGroupedProduct()
2138
{
2239
$productSku = 'grouped-product';
23-
$query
24-
= <<<QUERY
40+
$query = <<<QUERY
2541
{
2642
products(filter: {sku: {eq: "{$productSku}"}}) {
27-
items {
43+
items {
2844
id
2945
attribute_set_id
3046
created_at
3147
name
3248
sku
33-
type_id
49+
type_id
3450
... on GroupedProduct {
3551
items{
3652
qty
@@ -39,57 +55,92 @@ public function testAllFieldsGroupedProduct()
3955
sku
4056
name
4157
type_id
42-
url_key
58+
url_key
4359
}
4460
}
61+
product_links{
62+
linked_product_sku
63+
position
64+
link_type
65+
}
4566
}
4667
}
4768
}
4869
}
4970
QUERY;
5071

5172
$response = $this->graphQlQuery($query);
52-
/** @var ProductRepositoryInterface $productRepository */
53-
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
54-
$groupedProduct = $productRepository->get($productSku, false, null, true);
73+
$groupedProduct = $this->productRepository->get($productSku, false, null, true);
5574

56-
$this->assertGroupedProductItems($groupedProduct, $response['products']['items'][0]);
75+
$this->assertNotEmpty(
76+
$response['products']['items'][0]['items'],
77+
"Precondition failed: 'Grouped product items' must not be empty"
78+
);
79+
$this->assertGroupedProductItems($groupedProduct, $response['products']['items'][0]['items']);
80+
$this->assertNotEmpty(
81+
$response['products']['items'][0]['product_links'],
82+
"Precondition failed: 'Linked product items' must not be empty"
83+
);
84+
$this->assertProductLinks($groupedProduct, $response['products']['items'][0]['product_links']);
5785
}
5886

59-
private function assertGroupedProductItems($product, $actualResponse)
87+
/**
88+
* @param ProductInterface $product
89+
* @param array $items
90+
*/
91+
private function assertGroupedProductItems(ProductInterface $product, array $items): void
6092
{
61-
$this->assertNotEmpty(
62-
$actualResponse['items'],
63-
"Precondition failed: 'grouped product items' must not be empty"
64-
);
65-
$this->assertCount(2, $actualResponse['items']);
93+
$this->assertCount(2, $items);
6694
$groupedProductLinks = $product->getProductLinks();
67-
foreach ($actualResponse['items'] as $itemIndex => $bundleItems) {
68-
$this->assertNotEmpty($bundleItems);
95+
foreach ($items as $itemIndex => $bundleItem) {
96+
$this->assertNotEmpty($bundleItem);
6997
$associatedProductSku = $groupedProductLinks[$itemIndex]->getLinkedProductSku();
70-
71-
$productsRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
72-
/** @var \Magento\Catalog\Model\Product $associatedProduct */
73-
$associatedProduct = $productsRepository->get($associatedProductSku);
98+
$associatedProduct = $this->productRepository->get($associatedProductSku);
7499

75100
$this->assertEquals(
76101
$groupedProductLinks[$itemIndex]->getExtensionAttributes()->getQty(),
77-
$actualResponse['items'][$itemIndex]['qty']
102+
$bundleItem['qty']
78103
);
79104
$this->assertEquals(
80105
$groupedProductLinks[$itemIndex]->getPosition(),
81-
$actualResponse['items'][$itemIndex]['position']
106+
$bundleItem['position']
82107
);
83108
$this->assertResponseFields(
84-
$actualResponse['items'][$itemIndex]['product'],
109+
$bundleItem['product'],
85110
[
86-
'sku' => $associatedProductSku,
87-
'type_id' => $groupedProductLinks[$itemIndex]->getLinkedProductType(),
88-
'url_key'=> $associatedProduct->getUrlKey(),
89-
'name' => $associatedProduct->getName()
111+
'sku' => $associatedProductSku,
112+
'type_id' => $groupedProductLinks[$itemIndex]->getLinkedProductType(),
113+
'url_key'=> $associatedProduct->getUrlKey(),
114+
'name' => $associatedProduct->getName()
90115

91116
]
92117
);
93118
}
94119
}
120+
121+
/**
122+
* @param ProductInterface $product
123+
* @param array $links
124+
* @return void
125+
*/
126+
private function assertProductLinks(ProductInterface $product, array $links): void
127+
{
128+
$this->assertCount(2, $links);
129+
$productLinks = $product->getProductLinks();
130+
foreach ($links as $itemIndex => $linkedItem) {
131+
$this->assertNotEmpty($linkedItem);
132+
$this->assertEquals(
133+
$productLinks[$itemIndex]->getPosition(),
134+
$linkedItem['position']
135+
);
136+
$this->assertEquals(
137+
$productLinks[$itemIndex]->getLinkedProductSku(),
138+
$linkedItem['linked_product_sku']
139+
);
140+
$this->assertEquals(
141+
$productLinks[$itemIndex]->getLinkType(),
142+
$linkedItem['link_type']
143+
);
144+
}
145+
}
95146
}

0 commit comments

Comments
 (0)