Skip to content

Commit dfa7778

Browse files
committed
ACP2E-2151: GraphQL GetCategories query works slowly and generates 1K SQL queries
- fix
1 parent 9a58794 commit dfa7778

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ public function resolve(
125125
return [];
126126
}
127127

128-
if (!$product->getTierPrices()) {
129-
return [];
130-
}
131-
132128
$productId = (int)$product->getId();
133129
$this->tiers->addProductFilter($productId);
134130

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Price/Provider.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,68 @@
1717
*/
1818
class Provider implements ProviderInterface
1919
{
20+
/**
21+
* @var array
22+
*/
23+
private $minimalPrice = [
24+
FinalPrice::PRICE_CODE => [],
25+
RegularPrice::PRICE_CODE => []
26+
];
27+
28+
/**
29+
* @var array
30+
*/
31+
private $maximalPrice = [
32+
FinalPrice::PRICE_CODE => [],
33+
RegularPrice::PRICE_CODE => []
34+
];
35+
2036
/**
2137
* @inheritdoc
2238
*/
2339
public function getMinimalFinalPrice(SaleableInterface $product): AmountInterface
2440
{
25-
/** @var FinalPrice $finalPrice */
26-
$finalPrice = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE);
27-
return $finalPrice->getMinimalPrice();
41+
if (!isset($this->minimalPrice[FinalPrice::PRICE_CODE][$product->getId()])) {
42+
/** @var FinalPrice $finalPrice */
43+
$finalPrice = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE);
44+
$this->minimalPrice[FinalPrice::PRICE_CODE][$product->getId()] = $finalPrice->getMinimalPrice();
45+
}
46+
return $this->minimalPrice[FinalPrice::PRICE_CODE][$product->getId()];
2847
}
2948

3049
/**
3150
* @inheritdoc
3251
*/
3352
public function getMinimalRegularPrice(SaleableInterface $product): AmountInterface
3453
{
35-
return $this->getRegularPrice($product);
54+
if (!isset($this->minimalPrice[RegularPrice::PRICE_CODE][$product->getId()])) {
55+
$this->minimalPrice[RegularPrice::PRICE_CODE][$product->getId()] = $this->getRegularPrice($product);
56+
}
57+
return $this->minimalPrice[RegularPrice::PRICE_CODE][$product->getId()];
3658
}
3759

3860
/**
3961
* @inheritdoc
4062
*/
4163
public function getMaximalFinalPrice(SaleableInterface $product): AmountInterface
4264
{
43-
/** @var FinalPrice $finalPrice */
44-
$finalPrice = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE);
45-
return $finalPrice->getMaximalPrice();
65+
if (!isset($this->maximalPrice[FinalPrice::PRICE_CODE][$product->getId()])) {
66+
/** @var FinalPrice $finalPrice */
67+
$finalPrice = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE);
68+
$this->maximalPrice[FinalPrice::PRICE_CODE][$product->getId()] = $finalPrice->getMaximalPrice();
69+
}
70+
return $this->maximalPrice[FinalPrice::PRICE_CODE][$product->getId()];
4671
}
4772

4873
/**
4974
* @inheritdoc
5075
*/
5176
public function getMaximalRegularPrice(SaleableInterface $product): AmountInterface
5277
{
53-
return $this->getRegularPrice($product);
78+
if (!isset($this->maximalPrice[RegularPrice::PRICE_CODE][$product->getId()])) {
79+
$this->maximalPrice[RegularPrice::PRICE_CODE][$product->getId()] = $this->getRegularPrice($product);
80+
}
81+
return $this->maximalPrice[RegularPrice::PRICE_CODE][$product->getId()];
5482
}
5583

5684
/**

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/Product/Price/Provider.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Pricing\Price\FinalPrice;
1111
use Magento\Catalog\Pricing\Price\RegularPrice;
12+
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Provider as CatalogPriceProvider;
1213
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderInterface;
1314
use Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterfaceFactory;
1415
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
@@ -52,17 +53,25 @@ class Provider implements ProviderInterface, ResetAfterRequestInterface
5253
RegularPrice::PRICE_CODE => []
5354
];
5455

56+
/**
57+
* @var CatalogPriceProvider
58+
*/
59+
private $catalogPriceProvider;
60+
5561
/**
5662
* @param ConfigurableOptionsProviderInterfaceFactory $optionsProviderFactory
5763
* @param BaseFactory $amountFactory
64+
* @param CatalogPriceProvider $catalogPriceProvider
5865
*/
5966
public function __construct(
6067
ConfigurableOptionsProviderInterfaceFactory $optionsProviderFactory,
61-
BaseFactory $amountFactory
68+
BaseFactory $amountFactory,
69+
CatalogPriceProvider $catalogPriceProvider
6270
) {
6371
$this->optionsProvider = $optionsProviderFactory->create();
6472
$this->optionsProviderFactory = $optionsProviderFactory;
6573
$this->amountFactory = $amountFactory;
74+
$this->catalogPriceProvider = $catalogPriceProvider;
6675
}
6776

6877
/**
@@ -117,7 +126,13 @@ private function getMinimalPrice(SaleableInterface $product, string $code): Amou
117126
if (!isset($this->minimalPrice[$code][$product->getId()])) {
118127
$minimumAmount = null;
119128
foreach ($this->optionsProvider->getProducts($product) as $variant) {
120-
$variantAmount = $variant->getPriceInfo()->getPrice($code)->getAmount();
129+
$variantAmount = null;
130+
if ($code === FinalPrice::PRICE_CODE) {
131+
$variantAmount = $this->catalogPriceProvider->getMinimalFinalPrice($variant);
132+
} elseif ($code === RegularPrice::PRICE_CODE) {
133+
$variantAmount = $this->catalogPriceProvider->getMinimalRegularPrice($variant);
134+
}
135+
121136
if (!$minimumAmount || ($variantAmount->getValue() < $minimumAmount->getValue())) {
122137
$minimumAmount = $variantAmount;
123138
$this->minimalPrice[$code][$product->getId()] = $variantAmount;
@@ -140,7 +155,13 @@ private function getMaximalPrice(SaleableInterface $product, string $code): Amou
140155
if (!isset($this->maximalPrice[$code][$product->getId()])) {
141156
$maximumAmount = null;
142157
foreach ($this->optionsProvider->getProducts($product) as $variant) {
143-
$variantAmount = $variant->getPriceInfo()->getPrice($code)->getAmount();
158+
$variantAmount = null;
159+
if ($code === FinalPrice::PRICE_CODE) {
160+
$variantAmount = $this->catalogPriceProvider->getMaximalFinalPrice($variant);
161+
} elseif ($code === RegularPrice::PRICE_CODE) {
162+
$variantAmount = $this->catalogPriceProvider->getMaximalRegularPrice($variant);
163+
}
164+
144165
if (!$maximumAmount || ($variantAmount->getValue() > $maximumAmount->getValue())) {
145166
$maximumAmount = $variantAmount;
146167
$this->maximalPrice[$code][$product->getId()] = $variantAmount;

0 commit comments

Comments
 (0)