From 23f76fb88d87678f2c00b8d0e49c343d226a117f Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 28 Aug 2026 12:50:14 -0400 Subject: [PATCH] Restore GraphQL caching after query failures --- src/StaticCache.php | 5 +++++ tests/unit/StaticCacheTest.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/StaticCache.php b/src/StaticCache.php index e113847..fb648f0 100644 --- a/src/StaticCache.php +++ b/src/StaticCache.php @@ -162,6 +162,11 @@ private function handleInitWebApplication(Event $event): void private function handleAfterPrepareWebResponse(Event $event): void { + if ($this->graphqlCachingStack !== []) { + Craft::$app->getConfig()->getGeneral()->enableGraphqlCaching = $this->graphqlCachingStack[0]; + $this->graphqlCachingStack = []; + } + if (!$this->isCacheable()) { return; } diff --git a/tests/unit/StaticCacheTest.php b/tests/unit/StaticCacheTest.php index 3e38dac..319fa60 100644 --- a/tests/unit/StaticCacheTest.php +++ b/tests/unit/StaticCacheTest.php @@ -129,7 +129,9 @@ public function testGraphqlCachingIsOnlyDisabledWhileCollectingCacheInfo(): void { $staticCache = new StaticCache(); $generalConfig = Craft::$app->getConfig()->getGeneral(); + $response = Craft::$app->getResponse(); $enableGraphqlCaching = $generalConfig->enableGraphqlCaching; + $statusCode = $response->getStatusCode(); try { $generalConfig->enableGraphqlCaching = true; @@ -149,8 +151,20 @@ public function testGraphqlCachingIsOnlyDisabledWhileCollectingCacheInfo(): void $this->handleAfterExecuteGqlQuery($staticCache); $this->assertTrue($generalConfig->enableGraphqlCaching); + + $this->handleBeforeExecuteGqlQuery($staticCache); + $this->handleBeforeExecuteGqlQuery($staticCache); + $this->assertFalse($generalConfig->enableGraphqlCaching); + + $response->setStatusCode(500); + $this->handleAfterPrepareWebResponse($staticCache); + $this->assertTrue($generalConfig->enableGraphqlCaching); + + $graphqlCachingStack = new ReflectionProperty($staticCache, 'graphqlCachingStack'); + $this->assertSame([], $graphqlCachingStack->getValue($staticCache)); } finally { $generalConfig->enableGraphqlCaching = $enableGraphqlCaching; + $response->setStatusCode($statusCode); } } @@ -643,6 +657,12 @@ private function handleAfterExecuteGqlQuery(StaticCache $staticCache): void $method->invoke($staticCache, new \yii\base\Event()); } + private function handleAfterPrepareWebResponse(StaticCache $staticCache): void + { + $method = new ReflectionMethod($staticCache, 'handleAfterPrepareWebResponse'); + $method->invoke($staticCache, new \yii\base\Event()); + } + private function addCacheHeadersToWebResponse(StaticCache $staticCache): void { $method = new ReflectionMethod($staticCache, 'addCacheHeadersToWebResponse');