Skip to content

Commit 39ca723

Browse files
committed
magento/graphql-ce#816: Root category ID
1 parent c9737d0 commit 39ca723

File tree

5 files changed

+32
-138
lines changed

5 files changed

+32
-138
lines changed

app/code/Magento/CatalogGraphQl/Model/Category/GetRootCategoryId.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

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

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10-
use Magento\Catalog\Model\Category;
1110
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1211
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
13-
use Magento\Framework\Exception\LocalizedException;
1412
use Magento\Framework\GraphQl\Config\Element\Field;
1513
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1614
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -40,45 +38,20 @@ class CategoryTree implements ResolverInterface
4038
* @var CheckCategoryIsActive
4139
*/
4240
private $checkCategoryIsActive;
43-
/**
44-
* @var \Magento\CatalogGraphQl\Model\Category\GetRootCategoryId
45-
*/
46-
private $getRootCategoryId;
4741

4842
/**
4943
* @param Products\DataProvider\CategoryTree $categoryTree
5044
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
5145
* @param CheckCategoryIsActive $checkCategoryIsActive
52-
* @param \Magento\CatalogGraphQl\Model\Category\GetRootCategoryId $getRootCategoryId
5346
*/
5447
public function __construct(
5548
Products\DataProvider\CategoryTree $categoryTree,
5649
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
57-
CheckCategoryIsActive $checkCategoryIsActive,
58-
\Magento\CatalogGraphQl\Model\Category\GetRootCategoryId $getRootCategoryId
50+
CheckCategoryIsActive $checkCategoryIsActive
5951
) {
6052
$this->categoryTree = $categoryTree;
6153
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
6254
$this->checkCategoryIsActive = $checkCategoryIsActive;
63-
$this->getRootCategoryId = $getRootCategoryId;
64-
}
65-
66-
/**
67-
* Get category id
68-
*
69-
* @param array $args
70-
* @return int
71-
* @throws GraphQlNoSuchEntityException
72-
*/
73-
private function getCategoryId(array $args) : int
74-
{
75-
$rootCategoryId = 0;
76-
try {
77-
$rootCategoryId = isset($args['id']) ? (int)$args['id'] : $this->getRootCategoryId->execute();
78-
} catch (LocalizedException $exception) {
79-
throw new GraphQlNoSuchEntityException(__('Store doesn\'t exist'));
80-
}
81-
return $rootCategoryId;
8255
}
8356

8457
/**
@@ -90,10 +63,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9063
return $value[$field->getName()];
9164
}
9265

93-
$rootCategoryId = $this->getCategoryId($args);
94-
if ($rootCategoryId !== $this->getRootCategoryId->execute() && $rootCategoryId > 0) {
95-
$this->checkCategoryIsActive->execute($rootCategoryId);
96-
}
66+
$rootCategoryId = isset($args['id']) ? (int)$args['id'] :
67+
$context->getExtensionAttributes()->getStore()->getRootCategoryId();
68+
$this->checkCategoryIsActive->execute($rootCategoryId);
69+
9770
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
9871

9972
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
14+
/**
15+
* Root category tree field resolver, used for GraphQL request processing.
16+
*/
17+
class RootCategoryId implements ResolverInterface
18+
{
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
23+
{
24+
return $context->getExtensionAttributes()->getStore()->getRootCategoryId();
25+
}
26+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ type StoreConfig @doc(description: "The type contains information about a store
416416
grid_per_page : Int @doc(description: "Products per Page on Grid Default Value.")
417417
list_per_page : Int @doc(description: "Products per Page on List Default Value.")
418418
catalog_default_sort_by : String @doc(description: "Default Sort By.")
419-
root_category_id: Int @doc(description: "The ID ot root category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryRoot")
419+
root_category_id: Int @doc(description: "The ID of the root category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\RootCategoryId")
420420
}
421421

422422
type ProductVideo @doc(description: "Contains information about a product video.") implements MediaGalleryInterface {

0 commit comments

Comments
 (0)