Skip to content

Commit

Permalink
Merge pull request #5261 from magento-honey-badgers/MC-23303
Browse files Browse the repository at this point in the history
[honey] MC-23303: [Forwardport] Support preview for staged data
  • Loading branch information
cpartica authored Feb 6, 2020
2 parents 7404d9d + f762d42 commit 847f941
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function addVisibilityFilter(SearchCriteriaInterface $searchCriteria, bo
? $this->visibility->getVisibleInSearchIds()
: $this->visibility->getVisibleInCatalogIds();

$this->addFilter($searchCriteria, 'visibility', $visibilityIds);
$this->addFilter($searchCriteria, 'visibility', $visibilityIds, 'in');
}

/**
Expand All @@ -155,13 +155,20 @@ private function preparePriceAggregation(SearchCriteriaInterface $searchCriteria
* @param SearchCriteriaInterface $searchCriteria
* @param string $field
* @param mixed $value
* @param string|null $condition
*/
private function addFilter(SearchCriteriaInterface $searchCriteria, string $field, $value): void
{
private function addFilter(
SearchCriteriaInterface $searchCriteria,
string $field,
$value,
?string $condition = null
): void {
$filter = $this->filterBuilder
->setField($field)
->setValue($value)
->setConditionType($condition)
->create();

$this->filterGroupBuilder->addFilter($filter);
$filterGroups = $searchCriteria->getFilterGroups();
$filterGroups[] = $this->filterGroupBuilder->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,20 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\GraphQl\Config\Element\Field;
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;
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\ProductQueryInterface;

/**
* 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
* @deprecated
*/
private $searchCriteriaBuilder;

/**
* @var Filter
* @deprecated
*/
private $filterQuery;

/**
* @var Search
* @var ProductQueryInterface
*/
private $searchQuery;

Expand All @@ -51,25 +30,15 @@ class Products implements ResolverInterface
private $searchApiCriteriaBuilder;

/**
* @param ProductRepositoryInterface $productRepository
* @param Builder $searchCriteriaBuilder
* @param Filter $filterQuery
* @param Search $searchQuery
* @param ProductQueryInterface $searchQuery
* @param SearchCriteriaBuilder $searchApiCriteriaBuilder
*/
public function __construct(
ProductRepositoryInterface $productRepository,
Builder $searchCriteriaBuilder,
Filter $filterQuery,
Search $searchQuery = null,
SearchCriteriaBuilder $searchApiCriteriaBuilder = null
ProductQueryInterface $searchQuery,
SearchCriteriaBuilder $searchApiCriteriaBuilder
) {
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterQuery = $filterQuery;
$this->searchQuery = $searchQuery ?? ObjectManager::getInstance()->get(Search::class);
$this->searchApiCriteriaBuilder = $searchApiCriteriaBuilder ??
ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
$this->searchQuery = $searchQuery;
$this->searchApiCriteriaBuilder = $searchApiCriteriaBuilder;
}

/**
Expand All @@ -94,18 +63,17 @@ public function resolve(
'eq' => $value['id']
]
];
$searchCriteria = $this->searchApiCriteriaBuilder->build($args, false);
$searchResult = $this->searchQuery->getResult($searchCriteria, $info);
$searchResult = $this->searchQuery->getResult($args, $info);

//possible division by 0
if ($searchCriteria->getPageSize()) {
$maxPages = ceil($searchResult->getTotalCount() / $searchCriteria->getPageSize());
if ($searchResult->getPageSize()) {
$maxPages = ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
} else {
$maxPages = 0;
}

$currentPage = $searchCriteria->getCurrentPage();
if ($searchCriteria->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
$currentPage = $searchResult->getCurrentPage();
if ($searchResult->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
$currentPage = new GraphQlInputException(
__(
'currentPage value %1 specified is greater than the number of pages available.',
Expand All @@ -118,7 +86,7 @@ public function resolve(
'total_count' => $searchResult->getTotalCount(),
'items' => $searchResult->getProductsSearchResult(),
'page_info' => [
'page_size' => $searchCriteria->getPageSize(),
'page_size' => $searchResult->getPageSize(),
'current_page' => $currentPage,
'total_pages' => $maxPages
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function getMinimumProductPrice(SaleableInterface $product, StoreInterfa
$priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId());
$regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue();
$finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue();
$minPriceArray = $this->formatPrice($regularPrice, $finalPrice, $store);
$minPriceArray = $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store);
$minPriceArray['model'] = $product;
return $minPriceArray;
}
Expand All @@ -103,7 +103,7 @@ private function getMaximumProductPrice(SaleableInterface $product, StoreInterfa
$priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId());
$regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue();
$finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue();
$maxPriceArray = $this->formatPrice($regularPrice, $finalPrice, $store);
$maxPriceArray = $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store);
$maxPriceArray['model'] = $product;
return $maxPriceArray;
}
Expand Down
40 changes: 5 additions & 35 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CatalogGraphQl\Model\Resolver;

use Magento\CatalogGraphQl\Model\Resolver\Products\Query\ProductQueryInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Filter;
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search;
Expand All @@ -24,51 +25,24 @@
class Products implements ResolverInterface
{
/**
* @var Builder
* @deprecated
*/
private $searchCriteriaBuilder;

/**
* @var Search
* @var ProductQueryInterface
*/
private $searchQuery;

/**
* @var Filter
* @deprecated
*/
private $filterQuery;

/**
* @var SearchFilter
* @deprecated
*/
private $searchFilter;

/**
* @var SearchCriteriaBuilder
*/
private $searchApiCriteriaBuilder;

/**
* @param Builder $searchCriteriaBuilder
* @param Search $searchQuery
* @param Filter $filterQuery
* @param SearchFilter $searchFilter
* @param ProductQueryInterface $searchQuery
* @param SearchCriteriaBuilder|null $searchApiCriteriaBuilder
*/
public function __construct(
Builder $searchCriteriaBuilder,
Search $searchQuery,
Filter $filterQuery,
SearchFilter $searchFilter,
ProductQueryInterface $searchQuery,
SearchCriteriaBuilder $searchApiCriteriaBuilder = null
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->searchQuery = $searchQuery;
$this->filterQuery = $filterQuery;
$this->searchFilter = $searchFilter;
$this->searchApiCriteriaBuilder = $searchApiCriteriaBuilder ??
\Magento\Framework\App\ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
}
Expand All @@ -95,11 +69,7 @@ public function resolve(
);
}

//get product children fields queried
$productFields = (array)$info->getFieldSelection(1);
$includeAggregations = isset($productFields['filters']) || isset($productFields['aggregations']);
$searchCriteria = $this->searchApiCriteriaBuilder->build($args, $includeAggregations);
$searchResult = $this->searchQuery->getResult($searchCriteria, $info, $args);
$searchResult = $this->searchQuery->getResult($args, $info);

if ($searchResult->getCurrentPage() > $searchResult->getTotalPages() && $searchResult->getTotalCount() > 0) {
throw new GraphQlInputException(
Expand Down
Loading

0 comments on commit 847f941

Please sign in to comment.