Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for problem with removing GraphQl module #650

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}