Skip to content

Commit 4958abf

Browse files
committed
MC-42795: GraphQl products query layered navigation filters return incorrect child categories list
1 parent 875dc8f commit 4958abf

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations;
8+
namespace Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category;
99

1010
use Magento\Catalog\Api\CategoryListInterface;
1111
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -16,9 +16,9 @@
1616
use Magento\Framework\Api\Search\AggregationInterface;
1717

1818
/**
19-
* Include only subcategories of category in aggregation
19+
* Class to include only direct subcategories of category in aggregation
2020
*/
21-
class IncludeSubcategoriesOnly
21+
class IncludeDirectChildrenOnly
2222
{
2323
/**
2424
* @var string
@@ -82,7 +82,7 @@ public function __construct(
8282
}
8383

8484
/**
85-
* Filter category aggregation to include only subcategories of requested category
85+
* Filter category aggregation to include only direct subcategories of requested category
8686
*
8787
* @param AggregationInterface $aggregation
8888
* @param int|null $storeId
@@ -126,7 +126,7 @@ public function setFilter(array $filter): void
126126
}
127127

128128
/**
129-
* Filter bucket values to include only subcategories of requested category
129+
* Filter bucket values to include only direct subcategories of requested category
130130
*
131131
* @param array $categoryBucketValues
132132
* @param array $categoryIdsRequested

app/code/Magento/CatalogGraphQl/DataProvider/Product/LayeredNavigation/Builder/Category.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\Framework\Api\Search\BucketInterface;
1717
use Magento\Framework\App\ResourceConnection;
1818
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Formatter\LayerFormatter;
19-
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\IncludeSubcategoriesOnly;
19+
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category\IncludeDirectChildrenOnly;
2020

2121
/**
2222
* @inheritdoc
@@ -64,32 +64,32 @@ class Category implements LayerBuilderInterface
6464
private $layerFormatter;
6565

6666
/**
67-
* @var IncludeSubcategoriesOnly
67+
* @var IncludeDirectChildrenOnly
6868
*/
69-
private $includeSubcategoriesOnly;
69+
private $includeDirectChildrenOnly;
7070

7171
/**
7272
* @param CategoryAttributeQuery $categoryAttributeQuery
7373
* @param CategoryAttributesMapper $attributesMapper
7474
* @param RootCategoryProvider $rootCategoryProvider
7575
* @param ResourceConnection $resourceConnection
7676
* @param LayerFormatter $layerFormatter
77-
* @param IncludeSubcategoriesOnly $includeSubcategoriesOnly
77+
* @param IncludeDirectChildrenOnly $includeDirectChildrenOnly
7878
*/
7979
public function __construct(
8080
CategoryAttributeQuery $categoryAttributeQuery,
8181
CategoryAttributesMapper $attributesMapper,
8282
RootCategoryProvider $rootCategoryProvider,
8383
ResourceConnection $resourceConnection,
8484
LayerFormatter $layerFormatter,
85-
IncludeSubcategoriesOnly $includeSubcategoriesOnly
85+
IncludeDirectChildrenOnly $includeDirectChildrenOnly
8686
) {
8787
$this->categoryAttributeQuery = $categoryAttributeQuery;
8888
$this->attributesMapper = $attributesMapper;
8989
$this->resourceConnection = $resourceConnection;
9090
$this->rootCategoryProvider = $rootCategoryProvider;
9191
$this->layerFormatter = $layerFormatter;
92-
$this->includeSubcategoriesOnly = $includeSubcategoriesOnly;
92+
$this->includeDirectChildrenOnly = $includeDirectChildrenOnly;
9393
}
9494

9595
/**
@@ -99,7 +99,7 @@ public function __construct(
9999
*/
100100
public function build(AggregationInterface $aggregation, ?int $storeId): array
101101
{
102-
$aggregation = $this->includeSubcategoriesOnly->filter($aggregation, $storeId);
102+
$aggregation = $this->includeDirectChildrenOnly->filter($aggregation, $storeId);
103103
$bucket = $aggregation->getBucket(self::CATEGORY_BUCKET);
104104
if ($this->isBucketEmpty($bucket)) {
105105
return [];

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

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

1010
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\LayerBuilder;
11-
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\IncludeSubcategoriesOnly;
11+
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category\IncludeDirectChildrenOnly;
1212
use Magento\Directory\Model\PriceCurrency;
1313
use Magento\Framework\App\ObjectManager;
1414
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -37,27 +37,27 @@ class Aggregations implements ResolverInterface
3737
private $priceCurrency;
3838

3939
/**
40-
* @var IncludeSubcategoriesOnly
40+
* @var IncludeDirectChildrenOnly
4141
*/
42-
private $includeSubcategoriesOnly;
42+
private $includeDirectChildrenOnly;
4343

4444
/**
4545
* @param \Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider
4646
* @param LayerBuilder $layerBuilder
4747
* @param PriceCurrency $priceCurrency
48-
* @param IncludeSubcategoriesOnly $includeSubcategoriesOnly
48+
* @param IncludeDirectChildrenOnly $includeDirectChildrenOnly
4949
*/
5050
public function __construct(
5151
\Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider,
5252
LayerBuilder $layerBuilder,
5353
PriceCurrency $priceCurrency = null,
54-
IncludeSubcategoriesOnly $includeSubcategoriesOnly = null
54+
IncludeDirectChildrenOnly $includeDirectChildrenOnly = null
5555
) {
5656
$this->filtersDataProvider = $filtersDataProvider;
5757
$this->layerBuilder = $layerBuilder;
5858
$this->priceCurrency = $priceCurrency ?: ObjectManager::getInstance()->get(PriceCurrency::class);
59-
$this->includeSubcategoriesOnly = $includeSubcategoriesOnly
60-
?: ObjectManager::getInstance()->get(IncludeSubcategoriesOnly::class);
59+
$this->includeDirectChildrenOnly = $includeDirectChildrenOnly
60+
?: ObjectManager::getInstance()->get(IncludeDirectChildrenOnly::class);
6161
}
6262

6363
/**
@@ -78,9 +78,9 @@ public function resolve(
7878

7979
if ($aggregations) {
8080
$categoryFilter = $value['categories'] ?? [];
81-
$includeSubcategoriesOnly = $args['filter']['includeSubcategoriesOnly'] ?? false;
82-
if ($includeSubcategoriesOnly && !empty($categoryFilter)) {
83-
$this->includeSubcategoriesOnly->setFilter(['category' => $categoryFilter]);
81+
$includeDirectChildrenOnly = $args['filter']['category']['includeDirectChildrenOnly'] ?? false;
82+
if ($includeDirectChildrenOnly && !empty($categoryFilter)) {
83+
$this->includeDirectChildrenOnly->setFilter(['category' => $categoryFilter]);
8484
}
8585
/** @var StoreInterface $store */
8686
$store = $context->getExtensionAttributes()->getStore();

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,12 @@ type Products @doc(description: "The Products object is the top-level object ret
324324
sort_fields: SortFields @doc(description: "An object that includes the default sort field and all available sort fields.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\SortFields")
325325
}
326326

327-
input AggregationsFilterInput {
328-
includeSubcategoriesOnly: Boolean = false @doc(description: "Flag to include only subcategories of requested category.")
327+
input AggregationsFilterInput @doc(description: "An input object that specifies the filters used in product aggregations.") {
328+
category: AggregationsCategoryFilterInput @doc(description: "Filter category aggregations in layered navigation.")
329+
}
330+
331+
input AggregationsCategoryFilterInput @doc(description: "Filter category aggregations in layered navigation."){
332+
includeDirectChildrenOnly: Boolean = false @doc(description: "Indicates whether to include only direct subcategories or all children categories at all levels.")
329333
}
330334

331335
type CategoryProducts @doc(description: "The category products object returned in the Category query.") {

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function testAggregationEqCategory()
2424
{
2525
$filterValue = '{category_id: {eq: "2"}}';
2626
$categoryAggregation = $this->aggregationCategoryTesting($filterValue, "true");
27-
$expectedOptions = $this->getCategoryTwoOptions();
28-
$this->assertEquals($expectedOptions, $categoryAggregation);
27+
$expectedSubcategorie = $this->getSubcategoriesOfCategoryTwo();
28+
$this->assertEquals($expectedSubcategorie, $categoryAggregation);
2929
}
3030

3131
/**
@@ -37,18 +37,18 @@ public function testAggregationInCategory()
3737
{
3838
$filterValue = '{category_id: {in: ["3","2"]}}';
3939
$categoryAggregation = $this->aggregationCategoryTesting($filterValue, "true");
40-
$expectedOptions = $this->getCategoryThreeOptions() + $this->getCategoryTwoOptions();
41-
$this->assertEquals($expectedOptions, $categoryAggregation);
40+
$expectedSubcategorie = $this->getSubcategoriesOfCategoryThree() + $this->getSubcategoriesOfCategoryTwo();
41+
$this->assertEquals($expectedSubcategorie, $categoryAggregation);
4242
}
4343

4444
/**
4545
* @param string $filterValue
4646
*
4747
* @return array
4848
*/
49-
private function aggregationCategoryTesting(string $filterValue, string $includeSubcategoriesOnly): array
49+
private function aggregationCategoryTesting(string $filterValue, string $includeDirectChildrenOnly): array
5050
{
51-
$query = $this->getGraphQlQuery($filterValue, $includeSubcategoriesOnly);
51+
$query = $this->getGraphQlQuery($filterValue, $includeDirectChildrenOnly);
5252
$result = $this->graphQlQuery($query);
5353
$this->assertArrayNotHasKey('errors', $result);
5454
$this->assertArrayHasKey('aggregations', $result['products']);
@@ -75,7 +75,7 @@ function ($a) {
7575
*
7676
* @return array<string,string>
7777
*/
78-
private function getCategoryTwoOptions(): array
78+
private function getSubcategoriesOfCategoryTwo(): array
7979
{
8080
return [
8181
3 => 'Category 1',
@@ -90,7 +90,7 @@ private function getCategoryTwoOptions(): array
9090
*
9191
* @return array<string,string>
9292
*/
93-
private function getCategoryThreeOptions(): array
93+
private function getSubcategoriesOfCategoryThree(): array
9494
{
9595
return [
9696
4 => 'Category 1.1',
@@ -102,17 +102,17 @@ private function getCategoryThreeOptions(): array
102102
* Get graphQl query.
103103
*
104104
* @param string $categoryList
105-
* @param string $includeSubcategoriesOnly
105+
* @param string $includeDirectChildrenOnly
106106
* @return string
107107
*/
108-
private function getGraphQlQuery(string $categoryList, string $includeSubcategoriesOnly): string
108+
private function getGraphQlQuery(string $categoryList, string $includeDirectChildrenOnly): string
109109
{
110110
return <<<QUERY
111111
{
112112
products(filter: {$categoryList}) {
113113
total_count
114114
items { sku }
115-
aggregations (filter: {includeSubcategoriesOnly: {$includeSubcategoriesOnly}}) {
115+
aggregations (filter: { category: {includeDirectChildrenOnly: {$includeDirectChildrenOnly}}}) {
116116
attribute_code
117117
count
118118
label

0 commit comments

Comments
 (0)