Skip to content

Commit 6e3a29f

Browse files
committed
B2B-2659: Implement GraphQL Resolver Cache for cmsBlocks query
1 parent 00f9557 commit 6e3a29f

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\CmsGraphQl\Model\Resolver\Block;
9+
10+
use Magento\Cms\Api\Data\BlockInterface;
11+
use Magento\Cms\Model\Block;
12+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
13+
14+
class ResolverCacheIdentity implements IdentityInterface
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $cacheTag = Block::CACHE_TAG;
20+
21+
/**
22+
* Get block identities from resolved data
23+
*
24+
* @param array $resolvedData
25+
* @return string[]
26+
*/
27+
public function getIdentities(array $resolvedData): array
28+
{
29+
$ids = [];
30+
$items = $resolvedData['items'] ?? [];
31+
foreach ($items as $item) {
32+
if (is_array($item) && !empty($item[BlockInterface::BLOCK_ID])) {
33+
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[BlockInterface::BLOCK_ID]);
34+
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[BlockInterface::IDENTIFIER]);
35+
}
36+
}
37+
38+
return $ids;
39+
}
40+
}

app/code/Magento/CmsGraphQl/etc/graphql/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Magento\CmsGraphQl\Model\Resolver\Page\ResolverCacheIdentity
2626
</item>
2727
<item name="Magento\CmsGraphQl\Model\Resolver\Blocks" xsi:type="string">
28-
Magento\CmsGraphQl\Model\Resolver\Block\Identity
28+
Magento\CmsGraphQl\Model\Resolver\Block\ResolverCacheIdentity
2929
</item>
3030
</argument>
3131
</arguments>

dev/tests/api-functional/testsuite/Magento/GraphQl/CmsGraphQl/Model/Resolver/BlockTest.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ public function testCmsBlockResolverCacheDoesNotSaveNonExistentCmsBlock()
272272
// expected exception
273273
}
274274

275-
print_r($e->getResponseData());
276-
277275
$response['headers'] = $e->getResponseHeaders();
278276

279277
$cacheIdentityString = $this->getResolverCacheKeyFromResponseAndBlocks($response, [$nonExistentBlock]);
@@ -283,6 +281,62 @@ public function testCmsBlockResolverCacheDoesNotSaveNonExistentCmsBlock()
283281
);
284282
}
285283

284+
/**
285+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
286+
* @magentoDataFixture Magento/Cms/_files/block.php
287+
* @magentoDataFixture Magento/Cms/_files/blocks.php
288+
*/
289+
public function testCmsBlockResolverCacheRetainsEntriesThatHaveNotBeenUpdated()
290+
{
291+
// query block1
292+
$block1 = $this->blockRepository->getById('fixture_block');
293+
294+
$queryBlock1 = $this->getQuery([
295+
$block1->getIdentifier(),
296+
]);
297+
298+
$responseBlock1 = $this->graphQlQueryWithResponseHeaders($queryBlock1);
299+
300+
$cacheIdentityStringBlock1 = $this->getResolverCacheKeyFromResponseAndBlocks($responseBlock1, [$block1]);
301+
302+
// query block2
303+
$block2 = $this->blockRepository->getById('enabled_block');
304+
305+
$queryBlock2 = $this->getQuery([
306+
$block2->getIdentifier(),
307+
]);
308+
309+
$responseBlock2 = $this->graphQlQueryWithResponseHeaders($queryBlock2);
310+
311+
$cacheIdentityStringBlock2 = $this->getResolverCacheKeyFromResponseAndBlocks($responseBlock2, [$block2]);
312+
313+
// assert both cache entries are present
314+
$this->assertIsNumeric(
315+
$this->graphQlResolverCache->test($cacheIdentityStringBlock1),
316+
'Cache entry for block1 should be present'
317+
);
318+
319+
$this->assertIsNumeric(
320+
$this->graphQlResolverCache->test($cacheIdentityStringBlock2),
321+
'Cache entry for block2 should be present'
322+
);
323+
324+
// assert that cache is invalidated after block1 update
325+
$block1->setContent('Updated content');
326+
$this->blockRepository->save($block1);
327+
328+
$this->assertFalse(
329+
$this->graphQlResolverCache->test($cacheIdentityStringBlock1),
330+
'Cache entry for block1 should be invalidated after block1 update'
331+
);
332+
333+
// assert that cache is not invalidated after block1 update
334+
$this->assertIsNumeric(
335+
$this->graphQlResolverCache->test($cacheIdentityStringBlock2),
336+
'Cache entry for block2 should be present after block1 update'
337+
);
338+
}
339+
286340
private function getQuery(array $identifiers): string
287341
{
288342
$identifiersStr = $this->getQuotedBlockIdentifiersListAsString($identifiers);
@@ -347,7 +401,6 @@ private function assertTagsByCacheIdentityAndBlocks(string $cacheIdentityString,
347401
$tags = $metadatas['tags'];
348402

349403
$expectedTags = [
350-
$cacheIdPrefix . strtoupper(Block::CACHE_TAG),
351404
$cacheIdPrefix . strtoupper(GraphQlResolverCache::CACHE_TAG),
352405
$cacheIdPrefix . 'MAGE',
353406
];

0 commit comments

Comments
 (0)