Skip to content

Commit f644625

Browse files
committed
MC-15986: Category Filtering
- added resolver changes on filter
1 parent 5e12400 commit f644625

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

app/code/Magento/CatalogGraphQl/Model/Category/CategoryFilter.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ public function __construct(
3232
* Filter for filtering the requested categories id's based on url_key, ids, name in the result.
3333
*
3434
* @param array $args
35-
* @return array
35+
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
36+
* @return bool
3637
*/
37-
public function applyFilters(array $args): array
38-
{
39-
$categoryCollection = $this->collectionFactory->create();
38+
public function applyFilters(
39+
array $args,
40+
\Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
41+
): bool {
4042
foreach ($args['filters'] as $field => $cond) {
4143
foreach ($cond as $condType => $value) {
4244
if ($field === 'ids') {
4345
$categoryCollection->addIdFilter($value);
46+
} elseif ($condType === 'match') {
47+
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$value}%"]);
4448
} else {
4549
$categoryCollection->addAttributeToFilter($field, [$condType => $value]);
4650
}
4751
}
4852
}
49-
$categoryIds = [];
50-
foreach ($categoryCollection as $category) {
51-
$categoryIds[] = (int)$category->getId();
52-
}
53-
return $categoryIds;
53+
return true;
5454
}
5555
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree;
1818
use Magento\CatalogGraphQl\Model\Category\CategoryFilter;
19+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1920

2021
/**
2122
* Category List resolver, used for GraphQL category data request processing.
@@ -27,6 +28,11 @@ class CategoryList implements ResolverInterface
2728
*/
2829
private $categoryTree;
2930

31+
/**
32+
* @var CollectionFactory
33+
*/
34+
private $collectionFactory;
35+
3036
/**
3137
* @var CategoryFilter
3238
*/
@@ -47,17 +53,20 @@ class CategoryList implements ResolverInterface
4753
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
4854
* @param CheckCategoryIsActive $checkCategoryIsActive
4955
* @param CategoryFilter $categoryFilter
56+
* @param CollectionFactory $collectionFactory
5057
*/
5158
public function __construct(
5259
CategoryTree $categoryTree,
5360
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
5461
CheckCategoryIsActive $checkCategoryIsActive,
55-
CategoryFilter $categoryFilter
62+
CategoryFilter $categoryFilter,
63+
CollectionFactory $collectionFactory
5664
) {
5765
$this->categoryTree = $categoryTree;
5866
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
5967
$this->checkCategoryIsActive = $checkCategoryIsActive;
6068
$this->categoryFilter = $categoryFilter;
69+
$this->collectionFactory = $collectionFactory;
6170
}
6271

6372
/**
@@ -69,10 +78,18 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6978
return $value[$field->getName()];
7079
}
7180

81+
$categoryCollection = $this->collectionFactory->create();
82+
$categoryCollection->addAttributeToFilter('is_active', 1);
83+
$categoryCollection->addAttributeToSelect(['name','url_key', 'ids']);
84+
7285
if (!isset($args['filters'])) {
7386
$rootCategoryIds = [(int)$context->getExtensionAttributes()->getStore()->getRootCategoryId()];
7487
} else {
75-
$rootCategoryIds = $this->categoryFilter->applyFilters($args);
88+
$this->categoryFilter->applyFilters($args, $categoryCollection);
89+
$rootCategoryIds = [];
90+
foreach ($categoryCollection as $category) {
91+
$rootCategoryIds[] = (int)$category->getId();
92+
}
7693
}
7794

7895
$result = [];
@@ -81,7 +98,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8198
$this->checkCategoryIsActive->execute($rootCategoryId);
8299
}
83100
$categoryTree = $this->categoryTree->getTree($info, $rootCategoryId);
84-
if (empty($categoryTree)) {
101+
if (empty($categoryTree) || ($categoryTree->count() == 0)) {
85102
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
86103
}
87104
$result[] = current($this->extractDataFromCategoryTree->execute($categoryTree));

0 commit comments

Comments
 (0)