Skip to content

Commit cfcb946

Browse files
authored
ENGCOM-5744: Removed static root tree, Getting it from store manager #851
2 parents 3b977c8 + 131f65e commit cfcb946

File tree

5 files changed

+135
-28
lines changed

5 files changed

+135
-28
lines changed

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
use Magento\Catalog\Model\Category;
1111
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1212
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
13-
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1413
use Magento\Framework\GraphQl\Config\Element\Field;
15-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1614
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1715
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree as CategoryTreeDataProvider;
1818

1919
/**
2020
* Category tree field resolver, used for GraphQL request processing.
@@ -27,7 +27,7 @@ class CategoryTree implements ResolverInterface
2727
const CATEGORY_INTERFACE = 'CategoryInterface';
2828

2929
/**
30-
* @var \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree
30+
* @var CategoryTreeDataProvider
3131
*/
3232
private $categoryTree;
3333

@@ -42,12 +42,12 @@ class CategoryTree implements ResolverInterface
4242
private $checkCategoryIsActive;
4343

4444
/**
45-
* @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
45+
* @param CategoryTreeDataProvider $categoryTree
4646
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
4747
* @param CheckCategoryIsActive $checkCategoryIsActive
4848
*/
4949
public function __construct(
50-
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree,
50+
CategoryTreeDataProvider $categoryTree,
5151
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
5252
CheckCategoryIsActive $checkCategoryIsActive
5353
) {
@@ -56,22 +56,6 @@ public function __construct(
5656
$this->checkCategoryIsActive = $checkCategoryIsActive;
5757
}
5858

59-
/**
60-
* Get category id
61-
*
62-
* @param array $args
63-
* @return int
64-
* @throws GraphQlInputException
65-
*/
66-
private function getCategoryId(array $args) : int
67-
{
68-
if (!isset($args['id'])) {
69-
throw new GraphQlInputException(__('"id for category should be specified'));
70-
}
71-
72-
return (int)$args['id'];
73-
}
74-
7559
/**
7660
* @inheritdoc
7761
*/
@@ -81,7 +65,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8165
return $value[$field->getName()];
8266
}
8367

84-
$rootCategoryId = $this->getCategoryId($args);
68+
$rootCategoryId = isset($args['id']) ? (int)$args['id'] :
69+
(int)$context->getExtensionAttributes()->getStore()->getRootCategoryId();
70+
8571
if ($rootCategoryId !== Category::TREE_ROOT_ID) {
8672
$this->checkCategoryIsActive->execute($rootCategoryId);
8773
}
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 (int)$context->getExtensionAttributes()->getStore()->getRootCategoryId();
25+
}
26+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +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 of the root category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\RootCategoryId")
419420
}
420421

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

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

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
namespace Magento\GraphQl\Catalog;
99

1010
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
1113
use Magento\Catalog\Model\CategoryRepository;
1214
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
1315
use Magento\Framework\DataObject;
16+
use Magento\TestFramework\ObjectManager;
1417
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
1518
use Magento\TestFramework\TestCase\GraphQlAbstract;
16-
use Magento\Catalog\Api\Data\ProductInterface;
17-
use Magento\Catalog\Api\ProductRepositoryInterface;
18-
use Magento\TestFramework\ObjectManager;
1919

2020
/**
2121
* Test loading of category tree
@@ -113,6 +113,80 @@ public function testCategoriesTree()
113113
);
114114
}
115115

116+
/**
117+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
118+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
119+
*/
120+
public function testRootCategoryTree()
121+
{
122+
$query = <<<QUERY
123+
{
124+
category {
125+
id
126+
level
127+
description
128+
path
129+
path_in_store
130+
product_count
131+
url_key
132+
url_path
133+
children {
134+
id
135+
description
136+
available_sort_by
137+
default_sort_by
138+
image
139+
level
140+
children {
141+
id
142+
filter_price_range
143+
description
144+
image
145+
meta_keywords
146+
level
147+
is_anchor
148+
children {
149+
level
150+
id
151+
}
152+
}
153+
}
154+
}
155+
}
156+
QUERY;
157+
$response = $this->graphQlQuery($query);
158+
$responseDataObject = new DataObject($response);
159+
//Some sort of smoke testing
160+
self::assertEquals(
161+
'Its a description of Test Category 1.2',
162+
$responseDataObject->getData('category/children/0/children/1/description')
163+
);
164+
self::assertEquals(
165+
'default-category',
166+
$responseDataObject->getData('category/url_key')
167+
);
168+
self::assertEquals(
169+
[],
170+
$responseDataObject->getData('category/children/0/available_sort_by')
171+
);
172+
self::assertEquals(
173+
'name',
174+
$responseDataObject->getData('category/children/0/default_sort_by')
175+
);
176+
self::assertCount(
177+
7,
178+
$responseDataObject->getData('category/children')
179+
);
180+
self::assertCount(
181+
2,
182+
$responseDataObject->getData('category/children/0/children')
183+
);
184+
self::assertEquals(
185+
13,
186+
$responseDataObject->getData('category/children/0/children/1/id')
187+
);
188+
}
189+
116190
/**
117191
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
118192
*/
@@ -187,6 +261,25 @@ public function testGetDisabledCategory()
187261
$this->graphQlQuery($query);
188262
}
189263

264+
/**
265+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
266+
* @expectedException \Exception
267+
* @expectedExceptionMessage Category doesn't exist
268+
*/
269+
public function testGetCategoryIdZero()
270+
{
271+
$categoryId = 0;
272+
$query = <<<QUERY
273+
{
274+
category(id: {$categoryId}) {
275+
id
276+
name
277+
}
278+
}
279+
QUERY;
280+
$this->graphQlQuery($query);
281+
}
282+
190283
public function testNonExistentCategoryWithProductCount()
191284
{
192285
$query = <<<QUERY
@@ -419,8 +512,7 @@ private function assertBaseFields($product, $actualResponse)
419512
['response_field' => 'attribute_set_id', 'expected_value' => $product->getAttributeSetId()],
420513
['response_field' => 'created_at', 'expected_value' => $product->getCreatedAt()],
421514
['response_field' => 'name', 'expected_value' => $product->getName()],
422-
['response_field' => 'price', 'expected_value' =>
423-
[
515+
['response_field' => 'price', 'expected_value' => [
424516
'minimalPrice' => [
425517
'amount' => [
426518
'value' => $product->getPrice(),

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function testGetStoreConfig()
4040
list_per_page_values,
4141
grid_per_page,
4242
list_per_page,
43-
catalog_default_sort_by
43+
catalog_default_sort_by,
44+
root_category_id
4445
}
4546
}
4647
QUERY;
@@ -56,5 +57,6 @@ public function testGetStoreConfig()
5657
$this->assertEquals('8', $response['storeConfig']['list_per_page_values']);
5758
$this->assertEquals(8, $response['storeConfig']['list_per_page']);
5859
$this->assertEquals('asc', $response['storeConfig']['catalog_default_sort_by']);
60+
$this->assertEquals(2, $response['storeConfig']['root_category_id']);
5961
}
6062
}

0 commit comments

Comments
 (0)