This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from magento/issue_20
Include 'products' in category query
- Loading branch information
Showing
3 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/Products.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Category; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Filter; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
|
||
/** | ||
* Category products resolver, used by GraphQL endpoints to retrieve products assigned to a category | ||
*/ | ||
class Products implements ResolverInterface | ||
{ | ||
/** @var \Magento\Catalog\Api\ProductRepositoryInterface */ | ||
private $productRepository; | ||
|
||
/** @var Builder */ | ||
private $searchCriteriaBuilder; | ||
|
||
/** @var Filter */ | ||
private $filterQuery; | ||
|
||
/** @var ValueFactory */ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param Builder $searchCriteriaBuilder | ||
* @param Filter $filterQuery | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
ProductRepositoryInterface $productRepository, | ||
Builder $searchCriteriaBuilder, | ||
Filter $filterQuery, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->productRepository = $productRepository; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
$this->filterQuery = $filterQuery; | ||
$this->valueFactory = $valueFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
): Value { | ||
$args['filter'] = [ | ||
'category_ids' => [ | ||
'eq' => $value['id'] | ||
] | ||
]; | ||
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args); | ||
$searchCriteria->setCurrentPage($args['currentPage']); | ||
$searchCriteria->setPageSize($args['pageSize']); | ||
$searchResult = $this->filterQuery->getResult($searchCriteria, $info); | ||
|
||
//possible division by 0 | ||
if ($searchCriteria->getPageSize()) { | ||
$maxPages = ceil($searchResult->getTotalCount() / $searchCriteria->getPageSize()); | ||
} else { | ||
$maxPages = 0; | ||
} | ||
|
||
$currentPage = $searchCriteria->getCurrentPage(); | ||
if ($searchCriteria->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) { | ||
$currentPage = new GraphQlInputException( | ||
__( | ||
'currentPage value %1 specified is greater than the number of pages available.', | ||
[$maxPages] | ||
) | ||
); | ||
} | ||
|
||
$data = [ | ||
'total_count' => $searchResult->getTotalCount(), | ||
'items' => $searchResult->getProductsSearchResult(), | ||
'page_info' => [ | ||
'page_size' => $searchCriteria->getPageSize(), | ||
'current_page' => $currentPage | ||
] | ||
]; | ||
|
||
$result = function () use ($data) { | ||
return $data; | ||
}; | ||
|
||
return $this->valueFactory->create($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.