Skip to content

Commit 74a1065

Browse files
committed
Merge remote-tracking branch 'upstream/2.4-develop' into B2B-1876
2 parents ffe036f + 9264a46 commit 74a1065

File tree

5 files changed

+122
-23
lines changed

5 files changed

+122
-23
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/ProductsCount.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
use Magento\Catalog\Model\Category;
1111
use Magento\Catalog\Model\Product\Visibility;
12-
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\StockProcessor;
12+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CompositeCollectionProcessor;
1313
use Magento\Framework\Api\SearchCriteriaInterface;
1414
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1616
use Magento\Framework\GraphQl\Config\Element\Field;
1717
use Magento\Framework\GraphQl\Query\ResolverInterface;
1818

19+
1920
/**
2021
* Retrieves products count for a category
2122
*/
@@ -27,9 +28,9 @@ class ProductsCount implements ResolverInterface
2728
private $catalogProductVisibility;
2829

2930
/**
30-
* @var StockProcessor
31+
* @var CompositeCollectionProcessor
3132
*/
32-
private $stockProcessor;
33+
private $collectionProcessor;
3334

3435
/**
3536
* @var SearchCriteriaInterface
@@ -39,16 +40,16 @@ class ProductsCount implements ResolverInterface
3940
/**
4041
* @param Visibility $catalogProductVisibility
4142
* @param SearchCriteriaInterface $searchCriteria
42-
* @param StockProcessor $stockProcessor
43+
* @param CompositeCollectionProcessor $collectionProcessor
4344
*/
4445
public function __construct(
4546
Visibility $catalogProductVisibility,
4647
SearchCriteriaInterface $searchCriteria,
47-
StockProcessor $stockProcessor
48+
CompositeCollectionProcessor $collectionProcessor
4849
) {
4950
$this->catalogProductVisibility = $catalogProductVisibility;
5051
$this->searchCriteria = $searchCriteria;
51-
$this->stockProcessor = $stockProcessor;
52+
$this->collectionProcessor = $collectionProcessor;
5253
}
5354

5455
/**
@@ -63,8 +64,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6364
$category = $value['model'];
6465
$productsCollection = $category->getProductCollection();
6566
$productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
66-
$productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []);
67+
$productsCollection = $this->collectionProcessor->process(
68+
$productsCollection,
69+
$this->searchCriteria,
70+
[],
71+
$context
72+
);
73+
$size = $productsCollection->getSize();
6774

68-
return $productsCollection->getSize();
75+
return $size;
6976
}
7077
}

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10+
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\GraphQl\Model\Query\ContextInterface;
1012
use Magento\CatalogGraphQl\Model\Category\CategoryFilter;
1113
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree;
1214
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
1315
use Magento\Framework\Exception\InputException;
16+
use Magento\Framework\Exception\LocalizedException;
1417
use Magento\Framework\GraphQl\Config\Element\Field;
1518
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1619
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
@@ -81,22 +84,39 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8184
} catch (InputException $e) {
8285
throw new GraphQlInputException(__($e->getMessage()));
8386
}
84-
return $this->fetchCategories($rootCategoryIds, $info, (int) $store->getId());
87+
return $this->fetchCategories($rootCategoryIds, $info, $processedArgs, $store, [], $context);
8588
}
8689

8790
/**
8891
* Fetch category tree data
8992
*
9093
* @param array $categoryIds
9194
* @param ResolveInfo $info
92-
* @param int $storeId
95+
* @param array $criteria
96+
* @param StoreInterface $store
97+
* @param array $attributeNames
98+
* @param ContextInterface $context
9399
* @return array
100+
* @throws LocalizedException
94101
*/
95-
private function fetchCategories(array $categoryIds, ResolveInfo $info, int $storeId)
96-
{
102+
private function fetchCategories(
103+
array $categoryIds,
104+
ResolveInfo $info,
105+
array $criteria,
106+
StoreInterface $store,
107+
array $attributeNames,
108+
$context
109+
) : array {
97110
$fetchedCategories = [];
98111
foreach ($categoryIds as $categoryId) {
99-
$categoryTree = $this->categoryTree->getTree($info, $categoryId, $storeId);
112+
$categoryTree = $this->categoryTree->getFilteredTree(
113+
$info,
114+
$categoryId,
115+
$criteria,
116+
$store,
117+
$attributeNames,
118+
$context
119+
);
100120
if (empty($categoryTree)) {
101121
continue;
102122
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99

1010
use GraphQL\Language\AST\FieldNode;
1111
use GraphQL\Language\AST\NodeKind;
12+
13+
use Magento\Store\Api\Data\StoreInterface;
1214
use Magento\Catalog\Api\Data\CategoryInterface;
1315
use Magento\Catalog\Model\Category;
1416
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1517
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
18+
use Magento\GraphQl\Model\Query\ContextInterface;
1619
use Magento\CatalogGraphQl\Model\AttributesJoiner;
1720
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
1821
use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
22+
use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface;
23+
use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria;
1924
use Magento\Framework\EntityManager\MetadataPool;
2025
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
26+
use Magento\Framework\Exception\LocalizedException;
27+
use \Exception;
28+
use \Iterator;
2129

2230
/**
2331
* Category tree data provider
@@ -54,25 +62,41 @@ class CategoryTree
5462
*/
5563
private $metadata;
5664

65+
/**
66+
* @var CollectionProcessorInterface
67+
*/
68+
private $collectionProcessor;
69+
70+
/**
71+
* @var SearchCriteria
72+
*/
73+
private $searchCriteria;
74+
5775
/**
5876
* @param CollectionFactory $collectionFactory
5977
* @param AttributesJoiner $attributesJoiner
6078
* @param DepthCalculator $depthCalculator
6179
* @param LevelCalculator $levelCalculator
6280
* @param MetadataPool $metadata
81+
* @param CollectionProcessorInterface $collectionProcessor
82+
* @param SearchCriteria $searchCriteria
6383
*/
6484
public function __construct(
6585
CollectionFactory $collectionFactory,
6686
AttributesJoiner $attributesJoiner,
6787
DepthCalculator $depthCalculator,
6888
LevelCalculator $levelCalculator,
69-
MetadataPool $metadata
89+
MetadataPool $metadata,
90+
CollectionProcessorInterface $collectionProcessor,
91+
SearchCriteria $searchCriteria
7092
) {
7193
$this->collectionFactory = $collectionFactory;
7294
$this->attributesJoiner = $attributesJoiner;
7395
$this->depthCalculator = $depthCalculator;
7496
$this->levelCalculator = $levelCalculator;
7597
$this->metadata = $metadata;
98+
$this->collectionProcessor = $collectionProcessor;
99+
$this->searchCriteria = $searchCriteria;
76100
}
77101

78102
/**
@@ -81,11 +105,27 @@ public function __construct(
81105
* @param ResolveInfo $resolveInfo
82106
* @param int $rootCategoryId
83107
* @param int $storeId
84-
* @return \Iterator
108+
* @return Iterator
109+
* @throws LocalizedException
110+
* @throws Exception
85111
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
86112
*/
87-
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $storeId): \Iterator
113+
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $storeId): Iterator
88114
{
115+
$collection = $this->getCollection($resolveInfo, $rootCategoryId);
116+
return $collection->getIterator();
117+
}
118+
119+
/**
120+
* Return prepared collection
121+
*
122+
* @param ResolveInfo $resolveInfo
123+
* @param int $rootCategoryId
124+
* @return Collection
125+
* @throws LocalizedException
126+
* @throws Exception
127+
*/
128+
private function getCollection(ResolveInfo $resolveInfo, int $rootCategoryId) : Collection {
89129
$categoryQuery = $resolveInfo->fieldNodes[0];
90130
$collection = $this->collectionFactory->create();
91131
$this->joinAttributesRecursively($collection, $categoryQuery, $resolveInfo);
@@ -119,7 +159,7 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $stor
119159
$rootCategoryId
120160
);
121161

122-
return $collection->getIterator();
162+
return $collection;
123163
}
124164

125165
/**
@@ -150,4 +190,31 @@ private function joinAttributesRecursively(
150190
$this->joinAttributesRecursively($collection, $node, $resolveInfo);
151191
}
152192
}
193+
194+
/**
195+
* Returns categories tree starting from parent $rootCategoryId with filtration
196+
*
197+
* @param ResolveInfo $resolveInfo
198+
* @param int $rootCategoryId
199+
* @param array $criteria
200+
* @param StoreInterface $store
201+
* @param array $attributeNames
202+
* @param ContextInterface $context
203+
* @return Iterator
204+
* @throws LocalizedException
205+
* @throws Exception
206+
*/
207+
public function getFilteredTree(
208+
ResolveInfo $resolveInfo,
209+
int $rootCategoryId,
210+
array $criteria,
211+
StoreInterface $store,
212+
array $attributeNames,
213+
ContextInterface $context
214+
): Iterator {
215+
$searchCriteria = $this->searchCriteria->buildCriteria($criteria, $store);
216+
$collection = $this->getCollection($resolveInfo, $rootCategoryId);
217+
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
218+
return $collection->getIterator();
219+
}
153220
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ private function sortTree(array $tree): array
157157
return $element1['position'] > $element2['position'];
158158
});
159159
$node['children'] = $this->sortTree($node['children']);
160+
if (isset($node['children_count'])) {
161+
$node['children_count'] = count($node['children']);
162+
}
163+
} elseif (isset($node['children_count'])) {
164+
$node['children_count'] = 0;
160165
}
161166
}
162167

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoriesQuery/CategoriesFilterTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public function filterSingleCategoryDataProvider(): array
579579
'name' => 'Category 1.1',
580580
'url_key' => 'category-1-1',
581581
'url_path' => 'category-1/category-1-1',
582-
'children_count' => '0',
582+
'children_count' => '1',
583583
'path' => '1/2/3/4',
584584
'position' => '1'
585585
]
@@ -594,7 +594,7 @@ public function filterSingleCategoryDataProvider(): array
594594
'name' => 'Category 1.1',
595595
'url_key' => 'category-1-1',
596596
'url_path' => 'category-1/category-1-1',
597-
'children_count' => '0',
597+
'children_count' => '1',
598598
'path' => '1/2/3/4',
599599
'position' => '1'
600600
]
@@ -678,7 +678,7 @@ public function filterMultipleCategoriesDataProvider(): array
678678
'name' => 'Category 1.1',
679679
'url_key' => 'category-1-1',
680680
'url_path' => 'category-1/category-1-1',
681-
'children_count' => '0',
681+
'children_count' => '1',
682682
'path' => '1/2/3/4',
683683
'position' => '1'
684684
],
@@ -714,7 +714,7 @@ public function filterMultipleCategoriesDataProvider(): array
714714
'name' => 'Category 1.1',
715715
'url_key' => 'category-1-1',
716716
'url_path' => 'category-1/category-1-1',
717-
'children_count' => '0',
717+
'children_count' => '1',
718718
'path' => '1/2/3/4',
719719
'position' => '1'
720720
],
@@ -751,7 +751,7 @@ public function filterMultipleCategoriesDataProvider(): array
751751
'name' => 'Category 1.1',
752752
'url_key' => 'category-1-1',
753753
'url_path' => 'category-1/category-1-1',
754-
'children_count' => '0',
754+
'children_count' => '1',
755755
'path' => '1/2/3/4',
756756
'position' => '1'
757757
],
@@ -786,7 +786,7 @@ public function filterMultipleCategoriesDataProvider(): array
786786
'name' => 'Category 1.1',
787787
'url_key' => 'category-1-1',
788788
'url_path' => 'category-1/category-1-1',
789-
'children_count' => '0',
789+
'children_count' => '1',
790790
'path' => '1/2/3/4',
791791
'position' => '1'
792792
],

0 commit comments

Comments
 (0)