Skip to content

Commit cb82210

Browse files
author
Vitaliy Boyko
committed
graphQl-198: refactored upSell crossSell, adjusted tests
1 parent 2040d97 commit cb82210

File tree

14 files changed

+434
-320
lines changed

14 files changed

+434
-320
lines changed

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

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10-
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
10+
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductFieldsSelector;
1111
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Deferred\Product as ProductDataProvider;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1415
use Magento\Framework\GraphQl\Query\FieldTranslator;
15-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1616
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1717
use Magento\Framework\GraphQl\Query\ResolverInterface;
18+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1819

1920
/**
2021
* @inheritdoc
@@ -32,23 +33,33 @@ class Product implements ResolverInterface
3233
private $valueFactory;
3334

3435
/**
36+
* @deprecated
3537
* @var FieldTranslator
3638
*/
3739
private $fieldTranslator;
3840

41+
/**
42+
* @var ProductFieldsSelector
43+
*/
44+
private $productFieldsSelector;
45+
3946
/**
4047
* @param ProductDataProvider $productDataProvider
4148
* @param ValueFactory $valueFactory
4249
* @param FieldTranslator $fieldTranslator
50+
* @param ProductFieldsSelector $productFieldsSelector
4351
*/
4452
public function __construct(
4553
ProductDataProvider $productDataProvider,
4654
ValueFactory $valueFactory,
47-
FieldTranslator $fieldTranslator
55+
FieldTranslator $fieldTranslator,
56+
ProductFieldsSelector $productFieldsSelector
4857
) {
4958
$this->productDataProvider = $productDataProvider;
5059
$this->valueFactory = $valueFactory;
5160
$this->fieldTranslator = $fieldTranslator;
61+
$this->productFieldsSelector = $productFieldsSelector
62+
?? ObjectManager::getInstance()->get(ProductFieldsSelector::class);
5263
}
5364

5465
/**
@@ -60,7 +71,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6071
throw new GraphQlInputException(__('No child sku found for product link.'));
6172
}
6273
$this->productDataProvider->addProductSku($value['sku']);
63-
$fields = $this->getProductFields($info);
74+
$fields = $this->productFieldsSelector->getProductFieldsFromInfo($info);
6475
$this->productDataProvider->addEavAttributes($fields);
6576

6677
$result = function () use ($value) {
@@ -86,34 +97,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8697

8798
return $this->valueFactory->create($result);
8899
}
89-
90-
/**
91-
* Return field names for all requested product fields.
92-
*
93-
* @param ResolveInfo $info
94-
* @return string[]
95-
*/
96-
private function getProductFields(ResolveInfo $info) : array
97-
{
98-
$fieldNames = [];
99-
foreach ($info->fieldNodes as $node) {
100-
if ($node->name->value !== 'product') {
101-
continue;
102-
}
103-
foreach ($node->selectionSet->selections as $selectionNode) {
104-
if ($selectionNode->kind === 'InlineFragment') {
105-
foreach ($selectionNode->selectionSet->selections as $inlineSelection) {
106-
if ($inlineSelection->kind === 'InlineFragment') {
107-
continue;
108-
}
109-
$fieldNames[] = $this->fieldTranslator->translate($inlineSelection->name->value);
110-
}
111-
continue;
112-
}
113-
$fieldNames[] = $this->fieldTranslator->translate($selectionNode->name->value);
114-
}
115-
}
116-
117-
return $fieldNames;
118-
}
119100
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\CatalogGraphQl\Model\Resolver\Product;
9+
10+
use Magento\Framework\GraphQl\Query\FieldTranslator;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
13+
/**
14+
* Select Product Fields From Resolve Info
15+
*/
16+
class ProductFieldsSelector
17+
{
18+
/**
19+
* @var FieldTranslator
20+
*/
21+
private $fieldTranslator;
22+
23+
/**
24+
* @param FieldTranslator $fieldTranslator
25+
*/
26+
public function __construct(FieldTranslator $fieldTranslator)
27+
{
28+
$this->fieldTranslator = $fieldTranslator;
29+
}
30+
31+
/**
32+
* Return field names for all requested product fields.
33+
*
34+
* @param ResolveInfo $info
35+
* @param string $productNodeName
36+
* @return string[]
37+
*/
38+
public function getProductFieldsFromInfo(ResolveInfo $info, string $productNodeName = 'product') : array
39+
{
40+
$fieldNames = [];
41+
foreach ($info->fieldNodes as $node) {
42+
if ($node->name->value !== $productNodeName) {
43+
continue;
44+
}
45+
foreach ($node->selectionSet->selections as $selectionNode) {
46+
if ($selectionNode->kind === 'InlineFragment') {
47+
foreach ($selectionNode->selectionSet->selections as $inlineSelection) {
48+
if ($inlineSelection->kind === 'InlineFragment') {
49+
continue;
50+
}
51+
$fieldNames[] = $this->fieldTranslator->translate($inlineSelection->name->value);
52+
}
53+
continue;
54+
}
55+
$fieldNames[] = $this->fieldTranslator->translate($selectionNode->name->value);
56+
}
57+
}
58+
59+
return $fieldNames;
60+
}
61+
}

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Related/CrossSellProducts.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,38 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related;
99

10-
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\CrossSellDataProvider;
10+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\RelatedDataProvider;
1111
use Magento\Framework\GraphQl\Config\Element\Field;
12-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
13-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1412
use Magento\Framework\GraphQl\Query\ResolverInterface;
1513
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1614

17-
15+
/**
16+
* CrossSell Products Resolver
17+
*/
1818
class CrossSellProducts implements ResolverInterface
1919
{
2020
/**
21-
* Attribute to select fields
22-
*/
23-
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
24-
/**
25-
* @var CrossSellDataProvider
21+
* @see module di.xml
22+
* @var RelatedDataProvider
2623
*/
2724
private $dataProvider;
2825

29-
public function __construct(CrossSellDataProvider $dataProvider)
30-
{
26+
/**
27+
* @param RelatedDataProvider $dataProvider
28+
*/
29+
public function __construct(
30+
RelatedDataProvider $dataProvider
31+
) {
3132
$this->dataProvider = $dataProvider;
3233
}
3334

3435
/**
35-
* Fetches the data from persistence models and format it according to the GraphQL schema.
36-
*
37-
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
38-
* @param ContextInterface $context
39-
* @param ResolveInfo $info
40-
* @param array|null $value
41-
* @param array|null $args
42-
* @throws \Exception
43-
* @return mixed|Value
36+
* @inheritdoc
4437
*/
4538
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
4639
{
47-
$product = $value['model'];
48-
$this->dataProvider->addFieldToSelect(self::FIELDS);
49-
$collection = $this->dataProvider->getData($product);
40+
$data = $this->dataProvider->getProducts($info, $value);
5041

51-
$count = 0;
52-
foreach ($collection as $item) {
53-
$data[$count] = $item->getData();
54-
$data[$count]['model'] = $item;
55-
$count++;
56-
}
5742
return $data;
5843
}
59-
60-
}
44+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Related/RelatedProducts.php

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,37 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related;
99

10-
1110
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\RelatedDataProvider;
1211
use Magento\Framework\GraphQl\Config\Element\Field;
13-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1512
use Magento\Framework\GraphQl\Query\ResolverInterface;
1613
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1714

18-
1915
/**
20-
* Class RelatedProducts
21-
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related
16+
* Related Products Resolver
2217
*/
2318
class RelatedProducts implements ResolverInterface
2419
{
25-
26-
/**
27-
* Attribute to select fields
28-
*/
29-
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
3020
/**
3121
* @var RelatedDataProvider
3222
*/
3323
private $dataProvider;
3424

3525
/**
36-
* RelatedProducts constructor.
3726
* @param RelatedDataProvider $dataProvider
3827
*/
39-
public function __construct(RelatedDataProvider $dataProvider)
40-
{
28+
public function __construct(
29+
RelatedDataProvider $dataProvider
30+
) {
4131
$this->dataProvider = $dataProvider;
4232
}
4333

4434
/**
45-
* Fetches the data from persistence models and format it according to the GraphQL schema.
46-
*
47-
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
48-
* @param ContextInterface $context
49-
* @param ResolveInfo $info
50-
* @param array|null $value
51-
* @param array|null $args
52-
* @throws \Exception
53-
* @return mixed|Value
35+
* @inheritdoc
5436
*/
5537
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5638
{
57-
$product = $value['model'];
58-
$this->dataProvider->addFieldToSelect(self::FIELDS);
59-
$collection = $this->dataProvider->getData($product);
39+
$data = $this->dataProvider->getProducts($info, $value);
6040

61-
$count = 0;
62-
$data = [];
63-
foreach ($collection as $item) {
64-
$data[$count] = $item->getData();
65-
$data[$count]['model'] = $item;
66-
$count++;
67-
}
6841
return $data;
6942
}
70-
71-
}
43+
}

0 commit comments

Comments
 (0)