From 0a5ab867d99060089d47555d30d0d0961babce88 Mon Sep 17 00:00:00 2001 From: Mate Sitchinava Date: Sat, 2 Mar 2024 00:02:17 +0400 Subject: [PATCH] TEAMS-920 - Added missing process of category_uid filter input --- .../Query/CategoryUidArgsProcessor.php | 66 +++++++++++++++++++ src/Model/Resolver/Products/Query/Search.php | 22 +++++-- src/etc/di.xml | 8 +++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/Model/Resolver/Products/Query/CategoryUidArgsProcessor.php diff --git a/src/Model/Resolver/Products/Query/CategoryUidArgsProcessor.php b/src/Model/Resolver/Products/Query/CategoryUidArgsProcessor.php new file mode 100644 index 0000000..bc27e65 --- /dev/null +++ b/src/Model/Resolver/Products/Query/CategoryUidArgsProcessor.php @@ -0,0 +1,66 @@ + + * @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]); + } +} diff --git a/src/Model/Resolver/Products/Query/Search.php b/src/Model/Resolver/Products/Query/Search.php index 5a6bf5a..fd97a30 100644 --- a/src/Model/Resolver/Products/Query/Search.php +++ b/src/Model/Resolver/Products/Query/Search.php @@ -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; @@ -93,6 +94,11 @@ class Search extends CoreSearch */ protected CategoryCollectionFactory $categoryCollectionFactory; + /** + * @var ArgumentsProcessorInterface + */ + protected ArgumentsProcessorInterface $argsSelection; + /** * @param SearchInterface $search * @param SearchResultFactory $searchResultFactory @@ -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, @@ -119,7 +126,8 @@ public function __construct( DataPostProcessor $productPostProcessor, QueryFactory $queryFactory, StoreManagerInterface $storeManager, - CategoryCollectionFactory $categoryCollectionFactory + CategoryCollectionFactory $categoryCollectionFactory, + ArgumentsProcessorInterface $argsSelection ) { parent::__construct( $search, @@ -127,7 +135,8 @@ public function __construct( $pageSize, $fieldSelection, $productsProvider, - $searchCriteriaBuilder + $searchCriteriaBuilder, + $argsSelection ); $this->search = $search; @@ -142,6 +151,8 @@ public function __construct( $this->productSearchResultsInterfaceFactory = $productSearchResultsInterfaceFactory; $this->emulateSearchResult = $emulateSearchResult; $this->categoryCollectionFactory = $categoryCollectionFactory; + $this->argsSelection = $argsSelection ?: ObjectManager::getInstance() + ->get(ArgumentsProcessorInterface::class); } /** @@ -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; } @@ -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); diff --git a/src/etc/di.xml b/src/etc/di.xml index 7cf33bd..3afbe9c 100755 --- a/src/etc/di.xml +++ b/src/etc/di.xml @@ -53,6 +53,14 @@ + + + + ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query\CategoryUidArgsProcessor + + + +