Skip to content

Commit a4798a0

Browse files
committed
MC-39463: GraphQL caches urlResolver response and can return the old value after the url rewrite update
1 parent f10b36d commit a4798a0

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

app/code/Magento/CatalogUrlRewrite/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
</argument>
5757
</arguments>
5858
</type>
59+
<type name="Magento\UrlRewrite\Model\UrlRewrite">
60+
<arguments>
61+
<argument name="entityToCacheTagMap" xsi:type="array">
62+
<item name="product" xsi:type="const">Magento\Catalog\Model\Product::CACHE_TAG</item>
63+
<item name="category" xsi:type="const">Magento\Catalog\Model\Category::CACHE_TAG</item>
64+
</argument>
65+
</arguments>
66+
</type>
5967
<type name="Magento\Eav\Model\Config">
6068
<arguments>
6169
<argument name="attributesForPreload" xsi:type="array">

app/code/Magento/CmsUrlRewrite/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@
99
<type name="Magento\Cms\Model\ResourceModel\Page">
1010
<plugin name="cms_url_rewrite_plugin" type="Magento\CmsUrlRewrite\Plugin\Cms\Model\ResourceModel\Page"/>
1111
</type>
12+
<type name="Magento\UrlRewrite\Model\UrlRewrite">
13+
<arguments>
14+
<argument name="entityToCacheTagMap" xsi:type="array">
15+
<item name="cms-page" xsi:type="const">Magento\Cms\Model\Page::CACHE_TAG</item>
16+
</argument>
17+
</arguments>
18+
</type>
1219
</config>

app/code/Magento/UrlRewrite/Model/UrlRewrite.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class UrlRewrite extends AbstractModel
5656
*/
5757
private $eventManager;
5858

59+
/**
60+
* @var array
61+
*/
62+
private $entityToCacheTagMap;
63+
5964
/**
6065
* UrlRewrite constructor.
6166
*
@@ -67,6 +72,7 @@ class UrlRewrite extends AbstractModel
6772
* @param Json|null $serializer
6873
* @param CacheContext|null $cacheContext
6974
* @param EventManager|null $eventManager
75+
* @param array $entityToCacheTagMap
7076
*/
7177
public function __construct(
7278
Context $context,
@@ -76,12 +82,14 @@ public function __construct(
7682
array $data = [],
7783
Json $serializer = null,
7884
CacheContext $cacheContext = null,
79-
EventManager $eventManager = null
85+
EventManager $eventManager = null,
86+
array $entityToCacheTagMap = []
8087
)
8188
{
8289
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
8390
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
8491
$this->eventManager = $eventManager ?: ObjectManager::getInstance()->get(EventManager::class);
92+
$this->entityToCacheTagMap = $entityToCacheTagMap;
8593
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
8694
}
8795

@@ -130,14 +138,8 @@ public function setMetadata($metadata)
130138
*/
131139
private function cleanCacheForEntity($entityType, $entityId)
132140
{
133-
if ($entityType !== Rewrite::ENTITY_TYPE_CUSTOM) {
134-
$map = [
135-
Rewrite::ENTITY_TYPE_PRODUCT => Product::CACHE_TAG,
136-
Rewrite::ENTITY_TYPE_CATEGORY => Category::CACHE_TAG,
137-
Rewrite::ENTITY_TYPE_CMS_PAGE => Page::CACHE_TAG
138-
];
139-
140-
$cacheKey = $map[$entityType];
141+
if ($entityType !== Rewrite::ENTITY_TYPE_CUSTOM && array_key_exists($entityType, $this->entityToCacheTagMap)) {
142+
$cacheKey = $this->entityToCacheTagMap[$entityType];
141143

142144
$this->cacheContext->registerEntities($cacheKey, [$entityId]);
143145
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
@@ -147,7 +149,7 @@ private function cleanCacheForEntity($entityType, $entityId)
147149
public function afterDelete()
148150
{
149151
$this->cleanCacheForEntity($this->getEntityType(), $this->getEntityId());
150-
return parent::afterDelete(); // TODO: Change the autogenerated stub
152+
return parent::afterDelete();
151153
}
152154

153155
public function afterSave()

0 commit comments

Comments
 (0)