Skip to content

Prevent stale GraphQL responses in the static cache - #200

Merged
timkelty merged 7 commits into
3.xfrom
timkelty/investigate-gql-static-caching
Aug 28, 2026
Merged

Prevent stale GraphQL responses in the static cache#200
timkelty merged 7 commits into
3.xfrom
timkelty/investigate-gql-static-caching

Conversation

@timkelty

@timkelty timkelty commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

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.

Copilot AI lite review requested due to automatic review settings August 28, 2026 16:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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\Gql before/after execute query event handlers in StaticCache to toggle enableGraphqlCaching off only while collecting cache info.
  • Track and restore the prior enableGraphqlCaching value via a new $graphqlCaching instance 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.

Comment thread tests/unit/StaticCacheTest.php
Comment thread tests/unit/StaticCacheTest.php
Copilot AI review requested due to automatic review settings August 28, 2026 16:37
@timkelty
timkelty marked this pull request as ready for review August 28, 2026 16:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@timkelty
timkelty requested a lite review from Copilot August 28, 2026 16:40
@timkelty
timkelty merged commit edd0e38 into 3.x Aug 28, 2026
11 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/StaticCache.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants