|
11 | 11 | use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
|
12 | 12 | use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
|
13 | 13 | use Magento\Framework\EntityManager\MetadataPool;
|
| 14 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
14 | 15 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
15 | 16 | use Magento\Catalog\Api\Data\CategoryInterface;
|
16 | 17 | use Magento\Catalog\Model\ResourceModel\Category\Collection;
|
@@ -84,6 +85,10 @@ public function __construct(
|
84 | 85 | public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterator
|
85 | 86 | {
|
86 | 87 | $categoryQuery = $resolveInfo->fieldNodes[0];
|
| 88 | + if ($this->isCategoryActive($rootCategoryId) === false) { |
| 89 | + throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist')); |
| 90 | + } |
| 91 | + |
87 | 92 | $collection = $this->collectionFactory->create();
|
88 | 93 | $this->joinAttributesRecursively($collection, $categoryQuery);
|
89 | 94 | $depth = $this->depthCalculator->calculate($categoryQuery);
|
@@ -144,4 +149,30 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi
|
144 | 149 | $this->joinAttributesRecursively($collection, $node);
|
145 | 150 | }
|
146 | 151 | }
|
| 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 | + } |
147 | 178 | }
|
0 commit comments