Skip to content

Navigation Menu

Sign in
Sign up

Cache handling for queries #2708

Unanswered
NatanielMarmucki asked this question in Q&A
Discussion options

I’m working on implementing cache handling for queries. The cache itself works fine and gets created properly, but I’m having issues with cache invalidation.
I have these queries:

"Returns the address."
address(
 "The address ID."
 id: ID! @eq
): Address 
@guard 
@whereAuth(relation: "user")
@find 
@cache(maxAge: 300)
"Returns the list of addresses."
addresses(
 "The number of items in the page."
 first: Int!
 "The offset from which items are returned."
 page: Int
): [Address!]! 
@guard 
@whereAuth(relation: "user")
@paginate(type: PAGINATOR)
@cache(maxAge: 300)

When deleting a record, I want to clear the cache for both addresses and address:

"Deletes the address."
deleteAddress(
 "The address ID."
 id: ID! @whereKey
): Address 
@guard 
@whereAuth(relation: "user")
@delete
@clearCache(type: "Query", field: "addresses")
@clearCache(type: "Query", field: "address", idSource: { argument: "id" })

The cache gets cleared for addresses, but not for address. What could be causing this?

I can see that it generates a cache tag like lighthouse:Query::address:id:01k1dmfx78w412pwdh4hcj3pzk, but on the Lighthouse side, it generates lighthouse:Query:01k1dmfx78w412pwdh4hcj3pzk:address for the @clearCache(type: "Query", field: "address", idSource: { argument: "id" }) directive.

You must be logged in to vote

Replies: 1 comment 7 replies

Comment options

Looking at query caching, do I need to clear all records from the "address" cache instead of selecting a specific record?

@clearCache(type: "Query", field: "address")

I’m wondering if there’s a way to clear just individual cache entries rather than wiping the entire cache for the address field.

You must be logged in to vote
7 replies
Comment options

Pretty sure that is the output for two instances of @clearCache. Please also log $type and $field to make it more clear what is going on.

Comment options

Yes, there were two instances of @clearCache.

Now only for @clearCache(type: "Query", field: "address", idSource: { argument: "id" })

[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective type ["Query"] 
[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective field ["address"] 
[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective args {"id":"01k1e6wfhrshmcbjvkk25br5tm"} 
[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective result [{"App\\Models\\Address":{"id":"01k1e6wfhrshmcbjvkk25br5tm","user_id":"01jmskz21ctcqgam7d4ejf6277","parking_id":"01hqdxghk6p140pdk7gke428gw","type":"basic","google_place_id":"ChIJN1t_tDeuEmsRUsoyG83frY4","country":"Poland","region":"Mazowieckie","city":"Warsaw","street":"Marszałkowska","number":"4","postcode":"00-001","coordinates":{"latitude":52.2297,"longitude":21.0122},"created_at":"2025-07-30T17:43:05.000000Z","updated_at":"2025-07-30T17:43:27.000000Z","deleted_at":"2025-07-30T17:43:27.000000Z"}}] 
[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective idSource [{"argument":"id"}] 
[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective idOrIds ["01k1e6wfhrshmcbjvkk25br5tm"] 
[2025年07月30日 17:43:27] local.INFO: ClearCacheDirective tag ["lighthouse:Query:01k1e6wfhrshmcbjvkk25br5tm:address"] 
Comment options

Why can't I use @clearcache(type: "Address")?

Lighthouse does not generate cache tags based on object type, but rather on root type (Query).
In practice, Lighthouse always generates cache for fields in the root type (Query, Mutation), because these fields are called in queries.

In my opinion, there is something wrong with the cache handling, or I am completely missing something.

Comment options

Lighthouse generuje takie tagi dla cache:

1)"app_local_database_parkapp_local_cache_db3fa599e676c7707751239a43c94dabe1e8176e:lighthouse:Query::addresses:first:10"
2) "app_local_database_parkapp_local_cache_tag:lighthouse:Query::addresses:entries"
3) "app_local_database_parkapp_local_cache_tag:lighthouse:Query::entries"
4)"app_local_database_parkapp_local_cache_lighthouse:query:aa971d88b102de745347e3cc3dacd6225d35f97500ab66982cde8f3b4c5fba47"
5)"app_local_database_parkapp_local_cache_lighthouse:query:9867c8c064f40f59eee0cff11782e7058e5facb0e753a678e536ed067473de29"

If we use @clearcache(type: "Address"), it will attempt to clear the cache but from the lighthouse:Address tag:

[2025年08月01日 10:10:25] local.DEBUG: [ClearCache] idSource default {"idOrIds":[null]} 
[2025年08月01日 10:10:25] local.INFO: [ClearCache] Flushing cache {"type":"Address","field":null,"id":null,"tag":"lighthouse:Address:"} 
[2025年08月01日 10:10:25] local.INFO: [ClearCache] Flushed cache {"tag":"lighthouse:Address:"}

@clearcache(type: "Address") does not work because cache tags are not associated with the object type, but with the root type and field.

Comment options

To be honest, @cache and @clearCache are features I never really use myself. There might be a lot of potential for improvement, I can't really speak to the best practices for using it in its current form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /