From fb69e71c804d4c0a131ee4d82783b09613ed7bbe Mon Sep 17 00:00:00 2001 From: ivanviduka Date: Tue, 18 Jul 2023 12:36:19 +0200 Subject: [PATCH] Removed CacheableQuery from constructor, added condition which checks if GraphQlCache is enabled and can instance CacheableQuery --- Plugin/GraphQl/AfterRenderResult.php | 43 +++++++++++++++++----------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/Plugin/GraphQl/AfterRenderResult.php b/Plugin/GraphQl/AfterRenderResult.php index c700b2ce..e80f8bbd 100644 --- a/Plugin/GraphQl/AfterRenderResult.php +++ b/Plugin/GraphQl/AfterRenderResult.php @@ -4,9 +4,10 @@ namespace Fastly\Cdn\Plugin\GraphQl; use Fastly\Cdn\Model\Config; +use Magento\Framework\App\ObjectManager; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultInterface; -use Magento\GraphQlCache\Model\CacheableQuery; +use Magento\Framework\Module\Manager; class AfterRenderResult { @@ -16,22 +17,22 @@ class AfterRenderResult private $config; /** - * @var CacheableQuery + * @var Manager */ - private $cacheableQuery; + private $moduleManager; /** * AfterRenderResult constructor. * * @param Config $config - * @param CacheableQuery $cacheableQuery + * @param Manager $moduleManager */ public function __construct( Config $config, - CacheableQuery $cacheableQuery + Manager $moduleManager ) { $this->config = $config; - $this->cacheableQuery = $cacheableQuery; + $this->moduleManager = $moduleManager; } /** @@ -47,21 +48,29 @@ public function afterRenderResult( ResultInterface $result, ResponseInterface $response ): ResultInterface { - if ($this->config->isEnabled() - && $this->config->getType() === Config::FASTLY - && $this->cacheableQuery->isCacheable()) { - $header = $response->getHeader('cache-control'); - if ($header) { - if ($ttl = $this->config->getStaleTtl()) { - $header->addDirective('stale-while-revalidate', $ttl); - } + if (!$this->config->isEnabled() || !($this->config->getType() === Config::FASTLY)) { + return $result; + } + + if (!$this->moduleManager->isEnabled('Magento_GraphQlCache') || + !ObjectManager::getInstance()->get(\Magento\GraphQlCache\Model\CacheableQuery::class)->isCacheable() + ) { + return $result; + } + + $header = $response->getHeader('cache-control'); - if ($ttl = $this->config->getStaleErrorTtl()) { - $header->addDirective('stale-if-error', $ttl); - } + if ($header) { + if ($ttl = $this->config->getStaleTtl()) { + $header->addDirective('stale-while-revalidate', $ttl); + } + + if ($ttl = $this->config->getStaleErrorTtl()) { + $header->addDirective('stale-if-error', $ttl); } } + return $result; } }