Prevent stale GraphQL responses in the static cache - #200
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Addresses stale static-cache entries caused by GraphQL responses being cached without element dependencies by temporarily disabling Craft’s GraphQL caching while Elements::startCollectingCacheInfo() is active, ensuring static-cache invalidation remains correct until the upstream Craft fix is available.
Changes:
- Register
craft\services\Gqlbefore/after execute query event handlers inStaticCacheto toggleenableGraphqlCachingoff only while collecting cache info. - Track and restore the prior
enableGraphqlCachingvalue via a new$graphqlCachinginstance property. - Add a unit test to verify GraphQL caching is only disabled during cache-info collection.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/StaticCache.php |
Hooks into Craft GQL query lifecycle to disable GraphQL caching only while collecting element cache dependencies, then restores prior setting. |
tests/unit/StaticCacheTest.php |
Adds coverage for the new GraphQL caching toggle behavior during cache-info collection. |
Suppressed comments (1)
tests/unit/StaticCacheTest.php:638
- This helper reflects a private method but does not call ReflectionMethod::setAccessible(true). Invoking a private method without setting accessibility will fail.
private function handleAfterExecuteGqlQuery(StaticCache $staticCache): void
{
$method = new ReflectionMethod($staticCache, 'handleAfterExecuteGqlQuery');
$method->invoke($staticCache, new \yii\base\Event());
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GraphQL responses could be stored in the static cache without the element dependencies needed to invalidate them. Changes to queried elements would not purge those responses, leaving stale content cached until it expired.
Ensure statically cached GraphQL responses are invalidated when their queried elements change. This workaround can be removed after craftcms/cms#19508 reaches Cloud's minimum supported Craft version.