Skip to content

Commit 8ba64a2

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

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77

88
namespace Magento\UrlRewrite\Model;
99

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Category;
13+
use Magento\Catalog\Model\Product;
14+
use Magento\Cms\Api\PageRepositoryInterface;
15+
use Magento\Cms\Model\Page;
1016
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\EntityManager\EventManager;
18+
use Magento\Framework\Indexer\CacheContext;
1119
use Magento\Framework\Serialize\Serializer\Json;
20+
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
1221

1322
/**
1423
* UrlRewrite model class
@@ -92,4 +101,58 @@ public function setMetadata($metadata)
92101
}
93102
return $this->setData(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::METADATA, $metadata);
94103
}
104+
105+
private function opt1() {
106+
$map = [
107+
Rewrite::ENTITY_TYPE_PRODUCT => Product::CACHE_TAG,
108+
Rewrite::ENTITY_TYPE_CATEGORY => Category::CACHE_TAG,
109+
Rewrite::ENTITY_TYPE_CMS_PAGE => Page::CACHE_TAG
110+
];
111+
112+
if ($this->getEntityType() !== Rewrite::ENTITY_TYPE_CUSTOM) {
113+
$cacheKey = $map[$this->getEntityType()];
114+
115+
$cacheContext = ObjectManager::getInstance()->get(CacheContext::class);
116+
$eventManager = ObjectManager::getInstance()->get(EventManager::class);
117+
118+
$cacheContext->registerEntities($cacheKey, [$this->getEntityId()]);
119+
$eventManager->dispatch('clean_cache_by_tags', ['object' => $cacheContext]);
120+
}
121+
}
122+
123+
private function opt2() {
124+
$map = [
125+
Rewrite::ENTITY_TYPE_PRODUCT => function ($prodId) {
126+
/** @var ProductRepositoryInterface $productRepository */
127+
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
128+
return $productRepository->getById($prodId);
129+
},
130+
Rewrite::ENTITY_TYPE_CATEGORY => function ($catId) {
131+
/** @var CategoryRepositoryInterface $productRepository */
132+
$categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class);
133+
return $categoryRepository->get($catId);
134+
},
135+
Rewrite::ENTITY_TYPE_CMS_PAGE => function ($cmsId) {
136+
/** @var PageRepositoryInterface $productRepository */
137+
$pageRepository = ObjectManager::getInstance()->get(PageRepositoryInterface::class);
138+
return $pageRepository->getById($cmsId);
139+
},
140+
Rewrite::ENTITY_TYPE_CUSTOM => false
141+
];
142+
143+
$getter = $map[$this->getEntityType()];
144+
145+
if ($getter) {
146+
$entity = $getter($this->getEntityId());
147+
148+
$entityManager = ObjectManager::getInstance()->get(EventManager::class);
149+
$entityManager->dispatch('clean_cache_by_tags', ['object' => $entity]);
150+
}
151+
}
152+
153+
public function afterSave()
154+
{
155+
$this->opt1();
156+
return parent::afterSave(); // TODO: Change the autogenerated stub
157+
}
95158
}

0 commit comments

Comments
 (0)