Skip to content

Commit fc1ce04

Browse files
ENGCOM-4008: Fixed non existant category with products issue #311
- Merge Pull Request magento/graphql-ce#311 from ldusan84/graphql-ce:feature/category-with-products-fix - Merged commits: 1. d41c407 2. 5bf7364 3. 8d828da 4. 996ba90
2 parents e457037 + 996ba90 commit fc1ce04

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516

1617
/**
@@ -72,11 +73,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7273

7374
$rootCategoryId = $this->getCategoryId($args);
7475
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
75-
if (!empty($categoriesTree)) {
76-
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
77-
return current($result);
78-
} else {
79-
return null;
76+
77+
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
78+
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
8079
}
80+
81+
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
82+
return current($result);
8183
}
8284
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use Magento\Catalog\Api\Data\CategoryInterface;
1111
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
1212
use Magento\Framework\DataObject;
13+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
1314
use Magento\TestFramework\TestCase\GraphQlAbstract;
1415
use Magento\Catalog\Api\Data\ProductInterface;
1516
use Magento\Catalog\Api\ProductRepositoryInterface;
1617
use Magento\TestFramework\ObjectManager;
1718

19+
1820
class CategoryTest extends GraphQlAbstract
1921
{
2022
/**
@@ -146,6 +148,21 @@ public function testGetCategoryById()
146148
);
147149
}
148150

151+
public function testNonExistentCategoryWithProductCount()
152+
{
153+
$query = <<<QUERY
154+
{
155+
category(id: 99) {
156+
product_count
157+
}
158+
}
159+
QUERY;
160+
161+
$this->expectException(ResponseContainsErrorsException::class);
162+
$this->expectExceptionMessage('GraphQL response contains errors: Category doesn\'t exist');
163+
$this->graphQlQuery($query);
164+
}
165+
149166
/**
150167
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
151168
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)

0 commit comments

Comments
 (0)