Skip to content

Commit 8be23c7

Browse files
Merge pull request #150 from matesich/master
TEAMS-920 - Added missing process of category_uid filter input
2 parents 24076ed + 0a5ab86 commit 8be23c7

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* ScandiPWA_CatalogGraphQl
4+
*
5+
* @category ScandiPWA
6+
* @package ScandiPWA_CatalogGraphQl
7+
* @author <[email protected]>
8+
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com)
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query;
13+
14+
use Magento\Framework\GraphQl\Query\Uid;
15+
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
16+
17+
/**
18+
* Category UID processor class for category uid and category id arguments
19+
*/
20+
class CategoryUidArgsProcessor implements ArgumentsProcessorInterface
21+
{
22+
protected const ID = 'category_id';
23+
protected const UID = 'category_uid';
24+
25+
protected Uid $uidEncoder;
26+
27+
/**
28+
* @param Uid $uidEncoder
29+
*/
30+
public function __construct(Uid $uidEncoder)
31+
{
32+
$this->uidEncoder = $uidEncoder;
33+
}
34+
35+
/**
36+
* Override to enable both category_id and category_uid to be used at the same time
37+
*
38+
* @param string $fieldName
39+
* @param array $args
40+
* @return array
41+
* @throws GraphQlInputException
42+
*/
43+
public function process(
44+
string $fieldName,
45+
array $args
46+
): array {
47+
$idFilter = $args['filter'][self::ID] ?? [];
48+
$uidFilter = $args['filter'][self::UID] ?? [];
49+
50+
if (empty($uidFilter)) {
51+
return $args;
52+
}
53+
54+
if (isset($uidFilter['eq'])) {
55+
$args['filter'][self::ID]['eq'] = $this->uidEncoder->decode((string) $uidFilter['eq']);
56+
} elseif (!empty($uidFilter['in'])) {
57+
foreach ($uidFilter['in'] as $uid) {
58+
$args['filter'][self::ID]['in'][] = $this->uidEncoder->decode((string) $uid);
59+
}
60+
61+
unset($args['filter'][self::ID]['eq']);
62+
}
63+
64+
unset($args['filter'][self::UID]);
65+
}
66+
}

src/Model/Resolver/Products/Query/Search.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
1414
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\App\ObjectManager;
1617
use Magento\Framework\Api\Search\SearchCriteriaInterface;
1718
use Magento\Framework\Api\Search\SearchResultInterface;
1819
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult;
@@ -93,6 +94,11 @@ class Search extends CoreSearch
9394
*/
9495
protected CategoryCollectionFactory $categoryCollectionFactory;
9596

97+
/**
98+
* @var ArgumentsProcessorInterface
99+
*/
100+
protected ArgumentsProcessorInterface $argsSelection;
101+
96102
/**
97103
* @param SearchInterface $search
98104
* @param SearchResultFactory $searchResultFactory
@@ -106,6 +112,7 @@ class Search extends CoreSearch
106112
* @param QueryFactory $queryFactory
107113
* @param StoreManagerInterface $storeManager
108114
* @param CategoryCollectionFactory $categoryCollectionFactory
115+
* @param ArgumentsProcessorInterface $argsSelection
109116
*/
110117
public function __construct(
111118
SearchInterface $search,
@@ -119,15 +126,17 @@ public function __construct(
119126
DataPostProcessor $productPostProcessor,
120127
QueryFactory $queryFactory,
121128
StoreManagerInterface $storeManager,
122-
CategoryCollectionFactory $categoryCollectionFactory
129+
CategoryCollectionFactory $categoryCollectionFactory,
130+
ArgumentsProcessorInterface $argsSelection
123131
) {
124132
parent::__construct(
125133
$search,
126134
$searchResultFactory,
127135
$pageSize,
128136
$fieldSelection,
129137
$productsProvider,
130-
$searchCriteriaBuilder
138+
$searchCriteriaBuilder,
139+
$argsSelection
131140
);
132141

133142
$this->search = $search;
@@ -142,6 +151,8 @@ public function __construct(
142151
$this->productSearchResultsInterfaceFactory = $productSearchResultsInterfaceFactory;
143152
$this->emulateSearchResult = $emulateSearchResult;
144153
$this->categoryCollectionFactory = $categoryCollectionFactory;
154+
$this->argsSelection = $argsSelection ?: ObjectManager::getInstance()
155+
->get(ArgumentsProcessorInterface::class);
145156
}
146157

147158
/**
@@ -261,7 +272,10 @@ private function getSearchResults(SearchCriteriaInterface $searchCriteria, Resol
261272
*/
262273
private function buildSearchCriteria(array $args, ResolveInfo $info): SearchCriteriaInterface
263274
{
264-
$searchCriteria = $this->searchCriteriaBuilder->build($args, $this->includeAggregations($info));
275+
$productFields = (array)$info->getFieldSelection(1);
276+
$fieldName = $info->fieldName ?? "";
277+
$processedArgs = $this->argsSelection->process((string) $fieldName, $args);
278+
$searchCriteria = $this->searchCriteriaBuilder->build($processedArgs, $this->getIsIncludeAggregations($info));
265279

266280
return $searchCriteria;
267281
}
@@ -270,7 +284,7 @@ private function buildSearchCriteria(array $args, ResolveInfo $info): SearchCrit
270284
* @param ResolveInfo $info
271285
* @return bool
272286
*/
273-
private function includeAggregations(ResolveInfo $info): bool
287+
private function getIsIncludeAggregations(ResolveInfo $info): bool
274288
{
275289
$productFields = (array)$info->getFieldSelection(1);
276290

src/etc/di.xml

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
</arguments>
5454
</type>
5555

56+
<type name="Magento\Framework\GraphQl\Query\Resolver\ArgumentsCompositeProcessor">
57+
<arguments>
58+
<argument name="processors" xsi:type="array">
59+
<item name="category_uid" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUidArgsProcessor</item>
60+
</argument>
61+
</arguments>
62+
</type>
63+
5664
<type name="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CompositeCollectionProcessor">
5765
<arguments>
5866
<argument name="collectionProcessors" xsi:type="array">

0 commit comments

Comments
 (0)