feat(cache): add L2CacheKeyInterceptor for custom cache key transformation#1423
Conversation
…ation Add L2CacheKeyInterceptor function type and L2CacheKeyInterceptorInfo struct to CachingOptions, allowing library users (like Cosmo Router) to customize L2 cache keys per-request. This enables tenant isolation, custom prefixes/suffixes from request headers, and flexible cache key transformations without modifying graphql-go-tools internals. The interceptor: - Transforms L2 cache keys after rendering (includes existing subgraph header prefix) - Receives SubgraphName and CacheName for conditional customization - Does NOT affect L1 cache keys (per-request, internal optimization) - Is applied in prepareCacheKeys() and buildMutationEntityCacheKey() for consistency Includes comprehensive tests covering transformation, L1 isolation, info passing, and nil (default) behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe changes introduce a new public API for L2 cache key interception in the resolve layer, allowing optional transformation of L2 cache keys before lookups, writes, and deletions. The interceptor is applied in two code paths (prepareCacheKeys and buildMutationEntityCacheKey) while L1 caching remains unaffected. Comprehensive tests verify behavior across multiple scenarios including transformation, L1 isolation, and nil-case handling. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
v2/pkg/engine/resolve/loader_cache.go (1)
1094-1101: Please add a mutation-path interceptor test for key symmetry.This path now transforms mutation entity keys too. A dedicated test through
detectMutationEntityImpactwould protect delete/get symmetry from regressions.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@v2/pkg/engine/resolve/loader_cache.go` around lines 1094 - 1101, Add a dedicated unit test that uses detectMutationEntityImpact to verify key symmetry for mutation-path transformations: simulate an ExecutionOptions.Caching.L2CacheKeyInterceptor (the same interceptor type used in loader_cache.go) that mutates keys via L2CacheKeyInterceptorInfo (SubgraphName/CacheName) and assert that mutation-path operations (delete) and retrieval (get) produce symmetric cache keys; ensure the test registers the interceptor on l.ctx.ExecutionOptions.Caching.L2CacheKeyInterceptor and fails if detectMutationEntityImpact reports any asymmetry so future changes to L2CacheKeyInterceptor application to mutation entity keys are caught.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@v2/pkg/engine/resolve/loader_cache.go`:
- Around line 1094-1101: Add a dedicated unit test that uses
detectMutationEntityImpact to verify key symmetry for mutation-path
transformations: simulate an ExecutionOptions.Caching.L2CacheKeyInterceptor (the
same interceptor type used in loader_cache.go) that mutates keys via
L2CacheKeyInterceptorInfo (SubgraphName/CacheName) and assert that mutation-path
operations (delete) and retrieval (get) produce symmetric cache keys; ensure the
test registers the interceptor on
l.ctx.ExecutionOptions.Caching.L2CacheKeyInterceptor and fails if
detectMutationEntityImpact reports any asymmetry so future changes to
L2CacheKeyInterceptor application to mutation entity keys are caught.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: c359189a-0bfc-413d-b6a6-c104f1c4d1ca
📒 Files selected for processing (3)
v2/pkg/engine/resolve/context.gov2/pkg/engine/resolve/l2_cache_key_interceptor_test.gov2/pkg/engine/resolve/loader_cache.go
Summary
Add
L2CacheKeyInterceptorfunction type toCachingOptions, enabling library users (like Cosmo Router) to customize L2 cache keys per-request without modifying graphql-go-tools internals. The interceptor receivesSubgraphNameandCacheNamemetadata for conditional customization, and is applied after the existing subgraph header prefix.Key Changes
L2CacheKeyInterceptorfunction type andL2CacheKeyInterceptorInfostruct tov2/pkg/engine/resolve/context.goprepareCacheKeys()andbuildMutationEntityCacheKey()inv2/pkg/engine/resolve/loader_cache.goTest Plan
🤖 Generated with Claude Code
Summary by CodeRabbit