Skip to content

Commit

Permalink
Throw an exception where category doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ldusan84 committed Feb 14, 2019
1 parent d41c407 commit 5bf7364
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;

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

$rootCategoryId = $this->getCategoryId($args);
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
if (!empty($categoriesTree) && ($categoriesTree->count() > 0)) {
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
return current($result);
} else {
return null;

if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
}

$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
return current($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testNonExistentCategoryWithProductCount()
QUERY;

$response = $this->graphQlQuery($query);
$expectedResponse = ['category' => null];
$expectedResponse = ['errors' => ['message' => 'Category doesn\'t exist']];
$this->assertEquals($expectedResponse, $response);
}

Expand Down

0 comments on commit 5bf7364

Please sign in to comment.