Skip to content

Commit

Permalink
Removed CacheableQuery from constructor, added condition which checks…
Browse files Browse the repository at this point in the history
… if GraphQlCache is enabled and can instance CacheableQuery
  • Loading branch information
ivanviduka committed Jul 18, 2023
1 parent de6c76b commit fb69e71
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions Plugin/GraphQl/AfterRenderResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}

0 comments on commit fb69e71

Please sign in to comment.