Skip to content

Commit

Permalink
Category disabling support (#39)
Browse files Browse the repository at this point in the history
* add is_active to schema

* add is_active to resolver

* fix errors

* Add errors when category does not exist

* improved logic of getting additional parameter
  • Loading branch information
dmitrijs-voronovs authored and alfredsgenkins committed Jan 16, 2020
1 parent eeda28b commit 9a3bc10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Model/Resolver/CategoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree as DataCategoryTree;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand Down Expand Up @@ -66,7 +67,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
if (!empty($categoriesTree)) {
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
return current($result);
$category = current($result);

if(!$category) throw new GraphQlNoSuchEntityException(__('Category with specified id does not exist.'));
return $category;
}

return null;
Expand All @@ -76,6 +80,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
* @param array $args
* @return int
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
private function getCategoryId(array $args): int
{
Expand All @@ -87,10 +92,11 @@ private function getCategoryId(array $args): int
$categoryFactory = $this->categoryFactory->create();
$category = $categoryFactory->loadByAttribute('url_path', $args['url_path']);

if(!$category) throw new GraphQlNoSuchEntityException(__('Category with specified url path does not exist.'));
return (int)$category->getId();
}

throw new GraphQlInputException(__('"id or url for category must be specified'));
throw new GraphQlInputException(__('id or url for category must be specified'));
}
}

4 changes: 4 additions & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type Query {
@resolver(class: "ScandiPWA\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
}

type CategoryTree {
is_active: Boolean @doc(described: "Category is enabled")
}

interface ProductInterface {
attributes: [AttributeWithValue] @resolver(class:"ScandiPWA\\CatalogGraphQl\\Model\\Resolver\\AttributesWithValue")
stock_item: ProductStockItem @resolver(class: "ScandiPWA\\CatalogGraphQl\\Model\\Resolver\\Inventory\\StockCount")
Expand Down

0 comments on commit 9a3bc10

Please sign in to comment.