Skip to content

Commit 319054b

Browse files
committed
B2B-2658: Implement GraphQL Resolver Cache for Customer query
- CR fixes (type hint and inline comments)
1 parent 26b4054 commit 319054b

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

app/code/Magento/CmsGraphQl/Model/Resolver/Block/ResolverCacheIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ResolverCacheIdentity implements IdentityInterface
2121
/**
2222
* @inheritdoc
2323
*/
24-
public function getIdentities($resolvedData, $parentResolvedData): array
24+
public function getIdentities($resolvedData, ?array $parentResolvedData = null): array
2525
{
2626
$ids = [];
2727
$items = $resolvedData['items'] ?? [];

app/code/Magento/CmsGraphQl/Model/Resolver/Page/ResolverCacheIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ResolverCacheIdentity implements IdentityInterface
2424
/**
2525
* @inheritdoc
2626
*/
27-
public function getIdentities($resolvedData, $parentResolvedData): array
27+
public function getIdentities($resolvedData, ?array $parentResolvedData = null): array
2828
{
2929
return empty($resolvedData[PageInterface::PAGE_ID]) ?
3030
[] : [sprintf('%s_%s', $this->cacheTag, $resolvedData[PageInterface::PAGE_ID])];

app/code/Magento/CustomerGraphQl/Model/Resolver/Cache/Customer/ResolverCacheIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ResolverCacheIdentity implements IdentityInterface
2323
/**
2424
* @inheritdoc
2525
*/
26-
public function getIdentities($resolvedData, $parentResolvedData): array
26+
public function getIdentities($resolvedData, ?array $parentResolvedData = null): array
2727
{
2828
return empty($resolvedData['model']->getId()) ?
2929
[] : [sprintf('%s_%s', $this->cacheTag, $resolvedData['model']->getId())];

app/code/Magento/CustomerGraphQl/Model/Resolver/Cache/Subscriber/ResolverCacheIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ResolverCacheIdentity implements IdentityInterface
2222
/**
2323
* @inheritdoc
2424
*/
25-
public function getIdentities($resolvedData, $parentResolvedData): array
25+
public function getIdentities($resolvedData, ?array $parentResolvedData = null): array
2626
{
2727
return empty($parentResolvedData['model']->getId()) ?
2828
[] : [sprintf('%s_%s', $this->cacheTag, $parentResolvedData['model']->getId())];

dev/tests/api-functional/testsuite/Magento/GraphQl/CustomerGraphQl/Model/Resolver/CustomerTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Customer\Model\Customer;
1313
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
1414
use Magento\CustomerGraphQl\Model\Resolver\Customer as CustomerResolver;
15+
use Magento\CustomerGraphQl\Model\Resolver\IsSubscribed;
1516
use Magento\Framework\ObjectManagerInterface;
1617
use Magento\Framework\Registry;
1718
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
@@ -131,6 +132,91 @@ public function testCustomerResolverCacheAndInvalidation(callable $invalidationM
131132
$this->assertCurrentCustomerCacheRecordDoesNotExist();
132133
}
133134

135+
/**
136+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
137+
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
138+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
139+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
140+
* @dataProvider invalidationMechanismProvider
141+
*/
142+
public function testCustomerIsSubscribedResolverCacheAndInvalidation()
143+
{
144+
$customer = $this->customerRepository->get('[email protected]');
145+
146+
$query = $this->getCustomerQuery();
147+
148+
$token = $this->generateCustomerToken($customer->getEmail(), 'password');
149+
150+
$this->mockCustomerUserInfoContext($customer);
151+
$response = $this->graphQlQueryWithResponseHeaders(
152+
$query,
153+
[],
154+
'',
155+
['Authorization' => 'Bearer ' . $token]
156+
);
157+
158+
$this->assertCurrentCustomerCacheRecordExists($customer);
159+
$this->assertIsSubscribedRecordExists($customer);
160+
161+
// call query again to ensure no errors are thrown
162+
$this->graphQlQueryWithResponseHeaders(
163+
$query,
164+
[],
165+
'',
166+
['Authorization' => 'Bearer ' . $token]
167+
);
168+
169+
// change customer subscription
170+
171+
// assert that cache entry is invalidated
172+
// $this->assertCurrentCustomerCacheRecordDoesNotExist();
173+
}
174+
175+
private function getCacheKeyForIsSubscribedResolver(CustomerInterface $customer): string
176+
{
177+
$resolverMock = $this->getMockBuilder(IsSubscribed::class)
178+
->disableOriginalConstructor()
179+
->getMock();
180+
181+
/** @var ProviderInterface $cacheKeyCalculatorProvider */
182+
$cacheKeyCalculatorProvider = Bootstrap::getObjectManager()->get(ProviderInterface::class);
183+
184+
$cacheKeyFactor = $cacheKeyCalculatorProvider
185+
->getKeyCalculatorForResolver($resolverMock)
186+
->calculateCacheKey(
187+
['model' => $customer]
188+
);
189+
190+
$cacheKeyQueryPayloadMetadata = IsSubscribed::class . '\Interceptor[]';
191+
192+
$cacheKeyParts = [
193+
GraphQlResolverCache::CACHE_TAG,
194+
$cacheKeyFactor,
195+
sha1($cacheKeyQueryPayloadMetadata)
196+
];
197+
198+
// strtoupper is called in \Magento\Framework\Cache\Frontend\Adapter\Zend::_unifyId
199+
return strtoupper(implode('_', $cacheKeyParts));
200+
}
201+
202+
/**
203+
* Assert that cache record exists for the given customer.
204+
*
205+
* @param CustomerInterface $customer
206+
* @return void
207+
*/
208+
private function assertIsSubscribedRecordExists(CustomerInterface $customer)
209+
{
210+
$cacheKey = $this->getCacheKeyForIsSubscribedResolver($customer);
211+
$cacheEntry = Bootstrap::getObjectManager()->get(GraphQlResolverCache::class)->load($cacheKey);
212+
$cacheEntryDecoded = json_decode($cacheEntry, true);
213+
214+
$this->assertEquals(
215+
$customer->getEmail(),
216+
$cacheEntryDecoded['email']
217+
);
218+
}
219+
134220
/**
135221
* @magentoApiDataFixture Magento/Customer/_files/customer.php
136222
* @magentoApiDataFixture Magento/Store/_files/second_store.php

0 commit comments

Comments
 (0)