Skip to content

Commit b6fa826

Browse files
committed
B2B-2659: Implement GraphQL Resolver Cache for cmsBlocks query
1 parent ffe14c3 commit b6fa826

File tree

2 files changed

+47
-6
lines changed
  • app/code/Magento/GraphQlCache/Model/Plugin/Query/Resolver/Result
  • dev/tests/api-functional/testsuite/Magento/CmsGraphQl/Model/Resolver

2 files changed

+47
-6
lines changed

app/code/Magento/GraphQlCache/Model/Plugin/Query/Resolver/Result/Cache.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,16 @@ public function aroundResolve(
128128

129129
$resolvedValue = $proceed($field, $context, $info, $value, $args);
130130

131-
$this->graphQlResolverCache->save(
132-
$this->serializer->serialize($resolvedValue),
133-
$cacheIdentityString,
134-
$identityProvider->getIdentities($resolvedValue),
135-
false, // use default lifetime directive
136-
);
131+
$identities = $identityProvider->getIdentities($resolvedValue);
132+
133+
if (count($identities)) {
134+
$this->graphQlResolverCache->save(
135+
$this->serializer->serialize($resolvedValue),
136+
$cacheIdentityString,
137+
$identities,
138+
false, // use default lifetime directive
139+
);
140+
}
137141

138142
return $resolvedValue;
139143
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
1515
use Magento\Store\Model\StoreManagerInterface;
1616
use Magento\TestFramework\ObjectManager;
17+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
1718
use Magento\TestFramework\TestCase\GraphQlAbstract;
1819
use Magento\Widget\Model\Template\FilterEmulate;
1920

@@ -48,6 +49,13 @@ protected function setUp(): void
4849
$this->storeManager = $objectManager->get(StoreManagerInterface::class);
4950
}
5051

52+
protected function tearDown(): void
53+
{
54+
$this->graphqlCache->clean();
55+
56+
parent::tearDown();
57+
}
58+
5159
/**
5260
* @magentoDataFixture Magento/Cms/_files/blocks.php
5361
*/
@@ -246,6 +254,35 @@ public function testCmsBlockResolverCacheIsInvalidatedAfterChangingItsStoreView(
246254
);
247255
}
248256

257+
/**
258+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
259+
* @return void
260+
*/
261+
public function testCmsBlockResolverCacheDoesNotSaveNonExistentCmsBlock()
262+
{
263+
$nonExistentBlock = ObjectManager::getInstance()->create(BlockInterface::class);
264+
$nonExistentBlock->setIdentifier('non-existent-block');
265+
266+
$query = $this->getQuery([$nonExistentBlock->getIdentifier()]);
267+
268+
try {
269+
$response = $this->graphQlQueryWithResponseHeaders($query);
270+
$this->fail('Expected exception was not thrown');
271+
} catch (ResponseContainsErrorsException $e) {
272+
// expected exception
273+
}
274+
275+
print_r($e->getResponseData());
276+
277+
$response['headers'] = $e->getResponseHeaders();
278+
279+
$cacheIdentityString = $this->getResolverCacheKeyFromResponseAndBlocks($response, [$nonExistentBlock]);
280+
281+
$this->assertFalse(
282+
$this->graphqlCache->load($cacheIdentityString)
283+
);
284+
}
285+
249286
private function getQuery(array $identifiers): string
250287
{
251288
$identifiersStr = $this->getQuotedBlockIdentifiersListAsString($identifiers);

0 commit comments

Comments
 (0)