Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

28563: GraphQL product search does not consider Category Permissions configuration - Pass the context as argument #28757

Merged
merged 14 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$category = $value['model'];
$productsCollection = $category->getProductCollection();
$productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
$productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []);
$productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, [], $context);

return $productsCollection->getSize();
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$fields = $this->productFieldsSelector->getProductFieldsFromInfo($info);
$this->productDataProvider->addEavAttributes($fields);

$result = function () use ($value) {
$data = $value['product'] ?? $this->productDataProvider->getProductBySku($value['sku']);
$result = function () use ($value, $context) {
$data = $value['product'] ?? $this->productDataProvider->getProductBySku($value['sku'], $context);
if (empty($data)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function resolve(
);
}

$searchResult = $this->searchQuery->getResult($args, $info);
$searchResult = $this->searchQuery->getResult($args, $info, $context);

if ($searchResult->getCurrentPage() > $searchResult->getTotalPages() && $searchResult->getTotalCount() > 0) {
throw new GraphQlInputException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product as ProductDataProvider;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Deferred resolver for product data.
Expand Down Expand Up @@ -102,11 +103,12 @@ public function addEavAttributes(array $attributeCodes) : void
* Get product from result set.
*
* @param string $sku
* @param ContextInterface $context
* @return array
*/
public function getProductBySku(string $sku) : array
public function getProductBySku(string $sku, ContextInterface $context) : array
{
$products = $this->fetch();
$products = $this->fetch($context);

if (!isset($products[$sku])) {
return [];
Expand All @@ -118,9 +120,10 @@ public function getProductBySku(string $sku) : array
/**
* Fetch product data and return in array format. Keys for products will be their skus.
*
* @param ContextInterface $context
* @return array
*/
private function fetch() : array
private function fetch(ContextInterface $context) : array
{
if (empty($this->productSkus) || !empty($this->productList)) {
return $this->productList;
Expand All @@ -131,7 +134,8 @@ private function fetch() : array
$this->searchCriteriaBuilder->create(),
$this->attributeCodes,
false,
true
true,
$context
);

/** @var \Magento\Catalog\Model\Product $product */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Product field data provider, used for GraphQL resolver processing.
Expand Down Expand Up @@ -73,18 +74,20 @@ public function __construct(
* @param string[] $attributes
* @param bool $isSearch
* @param bool $isChildSearch
* @param ContextInterface $context
* @return SearchResultsInterface
*/
public function getList(
SearchCriteriaInterface $searchCriteria,
array $attributes = [],
bool $isSearch = false,
bool $isChildSearch = false
bool $isChildSearch = false,
ContextInterface $context
): SearchResultsInterface {
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->collectionFactory->create();

$this->collectionPreProcessor->process($collection, $searchCriteria, $attributes);
$this->collectionPreProcessor->process($collection, $searchCriteria, $attributes, $context);

if (!$isChildSearch) {
$visibilityIds = $isSearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Adds passed in attributes to product collection results
Expand Down Expand Up @@ -39,7 +40,8 @@ public function __construct($fieldToAttributeMap = [])
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
cpartica marked this conversation as resolved.
Show resolved Hide resolved
foreach ($attributeNames as $name) {
$this->addAttribute($collection, $name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Add necessary joins for extensible entities.
Expand All @@ -33,16 +34,13 @@ public function __construct(JoinProcessorInterface $joinProcessor)
}

/**
* @param Collection $collection
* @param SearchCriteriaInterface $searchCriteria
* @param array $attributeNames
* @return Collection
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @inheritdoc
*/
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to do here attributes and context optional, because not all implementations use it
also code becomes very complicated to pass all this around then not use it
Plus adding suppress unused params later..it all points optional is better

): Collection {
$this->joinProcessor->process($collection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Catalog\Model\Product\Media\Config as MediaConfig;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Add attributes required for every GraphQL product resolution process.
Expand Down Expand Up @@ -40,7 +41,8 @@ public function __construct(MediaConfig $mediaConfig)
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
if (in_array('media_gallery_entries', $attributeNames)) {
$mediaAttributes = $this->mediaConfig->getMediaAttributeCodes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Add attributes required for every GraphQL product resolution process.
Expand All @@ -24,7 +25,8 @@ class RequiredColumnsProcessor implements CollectionProcessorInterface
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
$collection->addAttributeToSelect('special_price');
$collection->addAttributeToSelect('special_price_from');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface as SearchCriteriaApplier;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Apply search criteria data to passed in collection.
Expand Down Expand Up @@ -38,7 +39,8 @@ public function __construct(SearchCriteriaApplier $searchCriteriaApplier)
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
$this->searchCriteriaApplier->process($searchCriteria, $collection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status as StockStatusResource;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Add stock filtering if configuration requires it.
Expand Down Expand Up @@ -46,7 +47,8 @@ public function __construct(StockConfigurationInterface $stockConfig, StockStatu
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
if (!$this->stockConfig->isShowOutOfStock()) {
$this->stockStatusResource->addIsInStockFilterToCollection($collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Join visibility and status tables to product collection
Expand All @@ -24,7 +25,8 @@ class VisibilityStatusProcessor implements CollectionProcessorInterface
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Add additional joins, attributes, and clauses to a product collection.
Expand All @@ -21,11 +22,13 @@ interface CollectionProcessorInterface
* @param Collection $collection
* @param SearchCriteriaInterface $searchCriteria
* @param array $attributeNames
* @param ContextInterface $context
* @return Collection
*/
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* {@inheritdoc}
Expand All @@ -34,10 +35,11 @@ public function __construct(array $collectionProcessors = [])
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
array $attributeNames,
ContextInterface $context
): Collection {
foreach ($this->collectionProcessors as $collectionProcessor) {
$collection = $collectionProcessor->process($collection, $searchCriteria, $attributeNames);
$collection = $collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
}

return $collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Product field data provider for product search, used for GraphQL resolver processing.
Expand Down Expand Up @@ -84,12 +85,14 @@ public function __construct(
* @param SearchCriteriaInterface $searchCriteria
* @param SearchResultInterface $searchResult
* @param array $attributes
* @param ContextInterface $context
* @return SearchResultsInterface
*/
public function getList(
SearchCriteriaInterface $searchCriteria,
SearchResultInterface $searchResult,
array $attributes = []
array $attributes = [],
ContextInterface $context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to make this optional, or put it before attributes, because attributes is optional.
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface::process has attributes mandatory and we also have to make it optional

): SearchResultsInterface {
/** @var Collection $collection */
$collection = $this->collectionFactory->create();
Expand All @@ -103,7 +106,7 @@ public function getList(
$this->getSortOrderArray($searchCriteriaForCollection)
)->apply();

$this->collectionPreProcessor->process($collection, $searchCriteriaForCollection, $attributes);
$this->collectionPreProcessor->process($collection, $searchCriteriaForCollection, $attributes, $context);
$collection->load();
$this->collectionPostProcessor->process($collection, $attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product as ProductProvider;
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult;
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResultFactory;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Search\Model\Query;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
Expand Down Expand Up @@ -84,16 +85,18 @@ public function __construct(
*
* @param array $args
* @param ResolveInfo $info
* @param ContextInterface $context
* @return SearchResult
*/
public function getResult(
array $args,
ResolveInfo $info
ResolveInfo $info,
ContextInterface $context
): SearchResult {
$fields = $this->fieldSelection->getProductsFieldSelection($info);
try {
$searchCriteria = $this->buildSearchCriteria($args, $info);
$searchResults = $this->productDataProvider->getList($searchCriteria, $fields);
$searchResults = $this->productDataProvider->getList($searchCriteria, $fields, false, false, $context);
} catch (InputException $e) {
return $this->createEmptyResult($args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Search for products by criteria
Expand All @@ -19,7 +20,8 @@ interface ProductQueryInterface
*
* @param array $args
* @param ResolveInfo $info
* @param ContextInterface $context
* @return SearchResult
*/
public function getResult(array $args, ResolveInfo $info): SearchResult;
public function getResult(array $args, ResolveInfo $info, ContextInterface $context): SearchResult;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResultFactory;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Search\Api\SearchInterface;
use Magento\Search\Model\Search\PageSizeProvider;

Expand Down Expand Up @@ -80,12 +81,13 @@ public function __construct(
*
* @param array $args
* @param ResolveInfo $info
* @param ContextInterface $context
* @return SearchResult
* @throws \Exception
*/
public function getResult(
array $args,
ResolveInfo $info
ResolveInfo $info,
ContextInterface $context
): SearchResult {
$queryFields = $this->fieldSelection->getProductsFieldSelection($info);
$searchCriteria = $this->buildSearchCriteria($args, $info);
Expand All @@ -101,7 +103,7 @@ public function getResult(
//Address limitations of sort and pagination on search API apply original pagination from GQL query
$searchCriteria->setPageSize($realPageSize);
$searchCriteria->setCurrentPage($realCurrentPage);
$searchResults = $this->productsProvider->getList($searchCriteria, $itemsResults, $queryFields);
$searchResults = $this->productsProvider->getList($searchCriteria, $itemsResults, $queryFields, $context);

$totalPages = $realPageSize ? ((int)ceil($searchResults->getTotalCount() / $realPageSize)) : 0;

Expand Down
Loading