Skip to content

Commit 525a1e9

Browse files
committed
magento/graphql-ce#463: Query category for disabled catalog with products returns the products count
1 parent f7fbc9a commit 525a1e9

File tree

3 files changed

+83
-33
lines changed

3 files changed

+83
-33
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogGraphQl\Model\Resolver\Category;
7+
8+
use Magento\Catalog\Api\Data\CategoryInterface;
9+
use Magento\Catalog\Model\Category;
10+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
11+
use Magento\Framework\EntityManager\MetadataPool;
12+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
13+
14+
/**
15+
* Check if category is active.
16+
*/
17+
class CheckCategoryIsActive
18+
{
19+
/**
20+
* @var CollectionFactory
21+
*/
22+
private $collectionFactory;
23+
24+
/**
25+
* @var MetadataPool
26+
*/
27+
private $metadata;
28+
29+
/**
30+
* @param CollectionFactory $collectionFactory
31+
* @param MetadataPool $metadata
32+
*/
33+
public function __construct(
34+
CollectionFactory $collectionFactory,
35+
MetadataPool $metadata
36+
)
37+
{
38+
$this->collectionFactory = $collectionFactory;
39+
$this->metadata = $metadata;
40+
}
41+
42+
/**
43+
* Check if category is active.
44+
*
45+
* @param int $rootCategoryId
46+
* @throws GraphQlNoSuchEntityException
47+
*/
48+
public function execute(int $rootCategoryId): void
49+
{
50+
$collection = $this->collectionFactory->create();
51+
$collection->addAttributeToFilter(Category::KEY_IS_ACTIVE, ['eq' => 1])
52+
->getSelect()
53+
->where(
54+
$collection->getSelect()
55+
->getConnection()
56+
->quoteIdentifier(
57+
'e.' .
58+
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
59+
) . ' = ?',
60+
$rootCategoryId
61+
);
62+
63+
if ($collection->count() === 0) {
64+
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
65+
}
66+
}
67+
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Model\Category;
11+
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1012
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
1113
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -34,16 +36,25 @@ class CategoryTree implements ResolverInterface
3436
*/
3537
private $extractDataFromCategoryTree;
3638

39+
/**
40+
* @var CheckCategoryIsActive
41+
*/
42+
private $checkCategoryIsActive;
43+
3744
/**
3845
* @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
3946
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
47+
* @param CheckCategoryIsActive $checkCategoryIsActive
4048
*/
4149
public function __construct(
4250
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree,
43-
ExtractDataFromCategoryTree $extractDataFromCategoryTree
44-
) {
51+
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
52+
CheckCategoryIsActive $checkCategoryIsActive
53+
)
54+
{
4555
$this->categoryTree = $categoryTree;
4656
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
57+
$this->checkCategoryIsActive = $checkCategoryIsActive;
4758
}
4859

4960
/**
@@ -72,6 +83,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7283
}
7384

7485
$rootCategoryId = $this->getCategoryId($args);
86+
if ($rootCategoryId !== Category::TREE_ROOT_ID) {
87+
$this->checkCategoryIsActive->execute($rootCategoryId);
88+
}
7589
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
7690

7791
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
1212
use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
1313
use Magento\Framework\EntityManager\MetadataPool;
14-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1615
use Magento\Catalog\Api\Data\CategoryInterface;
1716
use Magento\Catalog\Model\ResourceModel\Category\Collection;
@@ -85,10 +84,6 @@ public function __construct(
8584
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterator
8685
{
8786
$categoryQuery = $resolveInfo->fieldNodes[0];
88-
if ($this->isCategoryActive($rootCategoryId) === false) {
89-
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
90-
}
91-
9287
$collection = $this->collectionFactory->create();
9388
$this->joinAttributesRecursively($collection, $categoryQuery);
9489
$depth = $this->depthCalculator->calculate($categoryQuery);
@@ -149,30 +144,4 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi
149144
$this->joinAttributesRecursively($collection, $node);
150145
}
151146
}
152-
153-
/**
154-
* Check if provided category active
155-
*
156-
* @param int $rootCategoryId
157-
* @return bool
158-
* @throws \Magento\Framework\Exception\LocalizedException
159-
*/
160-
private function isCategoryActive(int $rootCategoryId) : bool
161-
{
162-
if ($rootCategoryId == Category::TREE_ROOT_ID) {
163-
return true;
164-
}
165-
$collection = $this->collectionFactory->create();
166-
$collection->addAttributeToFilter(Category::KEY_IS_ACTIVE, ['eq' => 1])
167-
->getSelect()
168-
->where(
169-
$collection->getSelect()
170-
->getConnection()
171-
->quoteIdentifier(
172-
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
173-
) . ' = ?',
174-
$rootCategoryId
175-
);
176-
return (bool)$collection->count();
177-
}
178147
}

0 commit comments

Comments
 (0)