Skip to content

Commit de0da1b

Browse files
committed
B2B-2658: Implement GraphQL Resolver Cache for Customer query
- added hydration skip procedure
1 parent eb04e47 commit de0da1b

File tree

13 files changed

+87
-45
lines changed

13 files changed

+87
-45
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/CacheKey/FactorProvider/CurrentCustomerId.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver\CacheKey\FactorProvider;
99

1010
use Magento\GraphQl\Model\Query\ContextInterface;
11-
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\FactorProviderInterface;
11+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\GenericFactorInterface;
1212

1313
/**
1414
* Provides logged-in customer id as a factor to use in the cache key for resolver cache.
1515
*/
16-
class CurrentCustomerId implements FactorProviderInterface
16+
class CurrentCustomerId implements GenericFactorInterface
1717
{
1818
/**
1919
* Factor name.

app/code/Magento/CustomerGraphQl/Model/Resolver/CacheKey/FactorProvider/CustomerGroup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
use Magento\Customer\Api\Data\GroupInterface;
1111
use Magento\GraphQl\Model\Query\ContextInterface;
12-
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\FactorProviderInterface;
12+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\GenericFactorInterface;
1313

1414
/**
1515
* Provides customer group as a factor to use in the cache key for resolver cache.
1616
*/
17-
class CustomerGroup implements FactorProviderInterface
17+
class CustomerGroup implements GenericFactorInterface
1818
{
1919
private const NAME = "CUSTOMER_GROUP";
2020

app/code/Magento/CustomerGraphQl/Model/Resolver/CacheKey/FactorProvider/CustomerTaxRate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use Magento\Customer\Api\Data\GroupInterface;
1111
use Magento\Customer\Model\ResourceModel\GroupRepository as CustomerGroupRepository;
1212
use Magento\GraphQl\Model\Query\ContextInterface;
13-
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\FactorProviderInterface;
13+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\GenericFactorInterface;
1414
use Magento\Tax\Model\Calculation as CalculationModel;
1515
use Magento\Tax\Model\ResourceModel\Calculation as CalculationResource;
1616

1717
/**
1818
* Provides tax rate as a factor to use in the cache key for resolver cache.
1919
*/
20-
class CustomerTaxRate implements FactorProviderInterface
20+
class CustomerTaxRate implements GenericFactorInterface
2121
{
2222
private const NAME = 'CUSTOMER_TAX_RATE';
2323

app/code/Magento/CustomerGraphQl/Model/Resolver/CacheKey/FactorProvider/IsLoggedIn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver\CacheKey\FactorProvider;
99

1010
use Magento\GraphQl\Model\Query\ContextInterface;
11-
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\FactorProviderInterface;
11+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\GenericFactorInterface;
1212

1313
/**
1414
* Provides logged-in status as a factor to use in the cache key for resolver cache.
1515
*/
16-
class IsLoggedIn implements FactorProviderInterface
16+
class IsLoggedIn implements GenericFactorInterface
1717
{
1818
private const NAME = "IS_LOGGED_IN";
1919

app/code/Magento/CustomerGraphQl/Model/Resolver/CacheKey/FactorProvider/ParentCustomerEntityId.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver\CacheKey\FactorProvider;
99

1010
use Magento\GraphQl\Model\Query\ContextInterface;
11-
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValueFactorProviderInterface;
11+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue\PlainValueFactorInterface;
1212

1313
/**
1414
* Provides customer id from the parent resolved value as a factor to use in the cache key for resolver cache.
1515
*/
16-
class ParentCustomerEntityId implements ParentValueFactorProviderInterface
16+
class ParentCustomerEntityId implements PlainValueFactorInterface
1717
{
1818
/**
1919
* Factor name.
@@ -31,8 +31,8 @@ public function getFactorName(): string
3131
/**
3232
* @inheritDoc
3333
*/
34-
public function getFactorValue(ContextInterface $context, ?array $parentResolverData = null): string
34+
public function getFactorValue(ContextInterface $context, ?array $parentValue = null): string
3535
{
36-
return (string)$parentResolverData['model']->getId();
36+
return (string)$parentValue['model_id'];
3737
}
3838
}

app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/CacheKey/Calculator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Exception;
1111
use Magento\Framework\ObjectManagerInterface;
1212
use Magento\GraphQl\Model\Query\ContextFactoryInterface;
13+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue\FactorInterface as ParentValueFactorInterface;
14+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue\ProcessedValueFactorInterface;
15+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue\PlainValueFactorInterface;
1316
use Magento\GraphQlResolverCache\Model\Resolver\Result\ValueProcessorInterface;
1417
use Psr\Log\LoggerInterface;
1518

@@ -29,7 +32,7 @@ class Calculator
2932
private $factorProviders;
3033

3134
/**
32-
* @var FactorProviderInterface[]
35+
* @var GenericFactorInterface[]
3336
*/
3437
private $factorProviderInstances;
3538

@@ -86,10 +89,9 @@ public function calculateCacheKey(?array $parentResolverData = null): ?string
8689
$this->initializeFactorProviderInstances();
8790
$keys = [];
8891
foreach ($this->factorProviderInstances as $provider) {
89-
if ($provider instanceof ParentValueFactorProviderInterface) {
90-
// trigger data hydration for key calculation
91-
// only when the parent-dependent key factor provider is called and the value is an array
92-
if (is_array($parentResolverData)) {
92+
if ($provider instanceof ParentValueFactorInterface) {
93+
// trigger data hydration for key calculation if factor needs the hydrated values
94+
if (is_array($parentResolverData) && $provider instanceof ProcessedValueFactorInterface) {
9395
$this->valueProcessor->preProcessParentValue($parentResolverData);
9496
}
9597
$keys[$provider->getFactorName()] = $provider->getFactorValue(

app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/CacheKey/FactorProviderInterface.php renamed to app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/CacheKey/GenericFactorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Interface for key factors that are used to calculate the resolver cache key.
1414
*/
15-
interface FactorProviderInterface
15+
interface GenericFactorInterface
1616
{
1717
/**
1818
* Name of the cache key factor.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey;
8+
namespace Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue;
99

1010
use Magento\GraphQl\Model\Query\ContextInterface;
11+
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\GenericFactorInterface;
1112

1213
/**
1314
* Interface for key factors that are used to calculate the resolver cache key.
1415
*/
15-
interface ParentValueFactorProviderInterface extends FactorProviderInterface
16+
interface FactorInterface extends GenericFactorInterface
1617
{
1718
/**
1819
* Returns the runtime value that should be used as factor.
1920
*
2021
* @param ContextInterface $context
21-
* @param array|null $parentResolverData
22+
* @param array|null $parentValue
2223
* @return string
2324
*/
24-
public function getFactorValue(ContextInterface $context, ?array $parentResolverData = null): string;
25+
public function getFactorValue(ContextInterface $context, ?array $parentValue = null): string;
2526
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue;
9+
10+
/**
11+
* Interface for key factors that are used to calculate the resolver cache key.
12+
*/
13+
interface PlainValueFactorInterface extends FactorInterface
14+
{
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue;
9+
10+
/**
11+
* Interface for key factors that are used to calculate the resolver cache key.
12+
*/
13+
interface ProcessedValueFactorInterface extends FactorInterface
14+
{
15+
16+
}

0 commit comments

Comments
 (0)