Skip to content

Commit e35f496

Browse files
committed
MC-32388: Add url_path as a possible filter in categoryList query
1 parent 239b0dd commit e35f496

File tree

4 files changed

+22
-34
lines changed

4 files changed

+22
-34
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\Config\ScopeConfigInterface;
1313
use Magento\Framework\Exception\InputException;
1414
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter;
15+
use Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match;
1516
use Magento\Search\Model\Query;
1617
use Magento\Store\Api\Data\StoreInterface;
1718
use Magento\Store\Model\ScopeInterface;
@@ -58,18 +59,14 @@ public function __construct(
5859
* @param array $criteria
5960
* @param StoreInterface $store
6061
* @return int[]
62+
* @throws InputException
6163
*/
62-
public function getCategoryIds(array $criteria, StoreInterface $store): array
64+
public function getResult(array $criteria, StoreInterface $store): array
6365
{
6466
$categoryIds = [];
65-
try {
66-
$criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store);
67-
} catch (InputException $e) {
68-
//Return empty set when match filter is too short. (matches search api behavior)
69-
return $categoryIds;
70-
}
71-
$criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1];
7267

68+
$criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store);
69+
$criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1];
7370
$searchCriteria = $this->searchCriteriaBuilder->build('categoryList', $criteria);
7471
$categories = $this->categoryList->getList($searchCriteria);
7572
foreach ($categories->getItems() as $category) {
@@ -95,16 +92,15 @@ private function formatMatchFilters(array $filters, StoreInterface $store): arra
9592
);
9693

9794
foreach ($filters as $filter => $condition) {
98-
$conditionType = array_keys($condition)[0];
95+
$conditionType = current(array_keys($condition));
9996
if ($conditionType === 'match') {
100-
$searchValue = $condition[$conditionType];
101-
$matchLength = strlen(trim($searchValue));
97+
$searchValue = trim(str_replace(Match::SPECIAL_CHARACTERS, '', $condition[$conditionType]));
98+
$matchLength = strlen($searchValue);
10299
if ($matchLength < $minQueryLength) {
103-
throw new InputException(__('Invalid match filter'));
100+
throw new InputException(__('Invalid match filter. Minimum length is %1.', $minQueryLength));
104101
}
105102
unset($filters[$filter]['match']);
106103
$filters[$filter]['like'] = '%' . $searchValue . '%';
107-
108104
}
109105
}
110106
return $filters;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\GraphQl\Query\Resolver\Argument\FieldEntityAttributesInterface;
1212

1313
/**
14-
* Retrieves attributes for a field for the ast converter
14+
* Retrieve filterable attributes for Category queries
1515
*/
1616
class CategoryFilterAttributesForAst implements FieldEntityAttributesInterface
1717
{

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

1010
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
11+
use Magento\Framework\Exception\InputException;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
12-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1314
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1516
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree;
1617
use Magento\CatalogGraphQl\Model\Category\CategoryFilter;
17-
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1818

1919
/**
2020
* Category List resolver, used for GraphQL category data request processing.
@@ -26,11 +26,6 @@ class CategoryList implements ResolverInterface
2626
*/
2727
private $categoryTree;
2828

29-
/**
30-
* @var CollectionFactory
31-
*/
32-
private $collectionFactory;
33-
3429
/**
3530
* @var CategoryFilter
3631
*/
@@ -45,18 +40,15 @@ class CategoryList implements ResolverInterface
4540
* @param CategoryTree $categoryTree
4641
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
4742
* @param CategoryFilter $categoryFilter
48-
* @param CollectionFactory $collectionFactory
4943
*/
5044
public function __construct(
5145
CategoryTree $categoryTree,
5246
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
53-
CategoryFilter $categoryFilter,
54-
CollectionFactory $collectionFactory
47+
CategoryFilter $categoryFilter
5548
) {
5649
$this->categoryTree = $categoryTree;
5750
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
5851
$this->categoryFilter = $categoryFilter;
59-
$this->collectionFactory = $collectionFactory;
6052
}
6153

6254
/**
@@ -69,11 +61,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6961
}
7062
$store = $context->getExtensionAttributes()->getStore();
7163

72-
$rootCategoryIds = [];
7364
if (!isset($args['filters'])) {
74-
$rootCategoryIds[] = (int)$store->getRootCategoryId();
75-
} else {
76-
$rootCategoryIds = $this->categoryFilter->getCategoryIds($args, $store);
65+
$args['filters']['ids'] = ['eq' => $store->getRootCategoryId()];
66+
}
67+
try {
68+
$rootCategoryIds = $this->categoryFilter->getResult($args, $store);
69+
} catch (InputException $e) {
70+
throw new GraphQlInputException(__($e->getMessage()));
7771
}
7872

7973
return $this->fetchCategories($rootCategoryIds, $info);
@@ -85,7 +79,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8579
* @param array $categoryIds
8680
* @param ResolveInfo $info
8781
* @return array
88-
* @throws GraphQlNoSuchEntityException
8982
*/
9083
private function fetchCategories(array $categoryIds, ResolveInfo $info)
9184
{

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ public function testEmptyFiltersReturnRootCategory()
360360
* Filtering with match value less than minimum query should return empty result
361361
*
362362
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
363+
* @expectedException \Exception
364+
* @expectedExceptionMessage Invalid match filter. Minimum length is 3.
363365
*/
364366
public function testMinimumMatchQueryLength()
365367
{
@@ -376,10 +378,7 @@ public function testMinimumMatchQueryLength()
376378
}
377379
}
378380
QUERY;
379-
$result = $this->graphQlQuery($query);
380-
$this->assertArrayNotHasKey('errors', $result);
381-
$this->assertArrayHasKey('categoryList', $result);
382-
$this->assertEquals([], $result['categoryList']);
381+
$this->graphQlQuery($query);
383382
}
384383

385384
/**

0 commit comments

Comments
 (0)