Skip to content

Commit

Permalink
Merge pull request #150 from matesich/master
Browse files Browse the repository at this point in the history
TEAMS-920 - Added missing process of category_uid filter input
  • Loading branch information
AleksandrsKondratjevs authored Mar 4, 2024
2 parents 24076ed + 0a5ab86 commit 8be23c7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
66 changes: 66 additions & 0 deletions src/Model/Resolver/Products/Query/CategoryUidArgsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* ScandiPWA_CatalogGraphQl
*
* @category ScandiPWA
* @package ScandiPWA_CatalogGraphQl
* @author <[email protected]>
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com)
*/
declare(strict_types=1);

namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query;

use Magento\Framework\GraphQl\Query\Uid;
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;

/**
* Category UID processor class for category uid and category id arguments
*/
class CategoryUidArgsProcessor implements ArgumentsProcessorInterface
{
protected const ID = 'category_id';
protected const UID = 'category_uid';

protected Uid $uidEncoder;

/**
* @param Uid $uidEncoder
*/
public function __construct(Uid $uidEncoder)
{
$this->uidEncoder = $uidEncoder;
}

/**
* Override to enable both category_id and category_uid to be used at the same time
*
* @param string $fieldName
* @param array $args
* @return array
* @throws GraphQlInputException
*/
public function process(
string $fieldName,
array $args
): array {
$idFilter = $args['filter'][self::ID] ?? [];
$uidFilter = $args['filter'][self::UID] ?? [];

if (empty($uidFilter)) {
return $args;
}

if (isset($uidFilter['eq'])) {
$args['filter'][self::ID]['eq'] = $this->uidEncoder->decode((string) $uidFilter['eq']);
} elseif (!empty($uidFilter['in'])) {
foreach ($uidFilter['in'] as $uid) {
$args['filter'][self::ID]['in'][] = $this->uidEncoder->decode((string) $uid);
}

unset($args['filter'][self::ID]['eq']);
}

unset($args['filter'][self::UID]);
}
}
22 changes: 18 additions & 4 deletions src/Model/Resolver/Products/Query/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult;
Expand Down Expand Up @@ -93,6 +94,11 @@ class Search extends CoreSearch
*/
protected CategoryCollectionFactory $categoryCollectionFactory;

/**
* @var ArgumentsProcessorInterface
*/
protected ArgumentsProcessorInterface $argsSelection;

/**
* @param SearchInterface $search
* @param SearchResultFactory $searchResultFactory
Expand All @@ -106,6 +112,7 @@ class Search extends CoreSearch
* @param QueryFactory $queryFactory
* @param StoreManagerInterface $storeManager
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param ArgumentsProcessorInterface $argsSelection
*/
public function __construct(
SearchInterface $search,
Expand All @@ -119,15 +126,17 @@ public function __construct(
DataPostProcessor $productPostProcessor,
QueryFactory $queryFactory,
StoreManagerInterface $storeManager,
CategoryCollectionFactory $categoryCollectionFactory
CategoryCollectionFactory $categoryCollectionFactory,
ArgumentsProcessorInterface $argsSelection
) {
parent::__construct(
$search,
$searchResultFactory,
$pageSize,
$fieldSelection,
$productsProvider,
$searchCriteriaBuilder
$searchCriteriaBuilder,
$argsSelection
);

$this->search = $search;
Expand All @@ -142,6 +151,8 @@ public function __construct(
$this->productSearchResultsInterfaceFactory = $productSearchResultsInterfaceFactory;
$this->emulateSearchResult = $emulateSearchResult;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->argsSelection = $argsSelection ?: ObjectManager::getInstance()
->get(ArgumentsProcessorInterface::class);
}

/**
Expand Down Expand Up @@ -261,7 +272,10 @@ private function getSearchResults(SearchCriteriaInterface $searchCriteria, Resol
*/
private function buildSearchCriteria(array $args, ResolveInfo $info): SearchCriteriaInterface
{
$searchCriteria = $this->searchCriteriaBuilder->build($args, $this->includeAggregations($info));
$productFields = (array)$info->getFieldSelection(1);
$fieldName = $info->fieldName ?? "";
$processedArgs = $this->argsSelection->process((string) $fieldName, $args);
$searchCriteria = $this->searchCriteriaBuilder->build($processedArgs, $this->getIsIncludeAggregations($info));

return $searchCriteria;
}
Expand All @@ -270,7 +284,7 @@ private function buildSearchCriteria(array $args, ResolveInfo $info): SearchCrit
* @param ResolveInfo $info
* @return bool
*/
private function includeAggregations(ResolveInfo $info): bool
private function getIsIncludeAggregations(ResolveInfo $info): bool
{
$productFields = (array)$info->getFieldSelection(1);

Expand Down
8 changes: 8 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
</arguments>
</type>

<type name="Magento\Framework\GraphQl\Query\Resolver\ArgumentsCompositeProcessor">
<arguments>
<argument name="processors" xsi:type="array">
<item name="category_uid" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUidArgsProcessor</item>
</argument>
</arguments>
</type>

<type name="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CompositeCollectionProcessor">
<arguments>
<argument name="collectionProcessors" xsi:type="array">
Expand Down

0 comments on commit 8be23c7

Please sign in to comment.