-
Hi! I'm trying to understand how the cache works between Queries and Mutations. I was expecting that Mutation results would populate the cache in a way that queries would then be able to get that cached data, but after some testing, my observations lead me to believe that Mutations and Queries use separate caching entires that are "independent", wondering if that is accurate?? I've executed 2 different scenarios:
..in order to understand how the cache behaves, and precisely to understand the bug that I'm currently having in my app around scenario B. I don't understand why in the last query of scenario B, I'm still getting the NULL value cached from initial Query. I would have expected the Mutation in the middle to reset the original NULL value from the cache, since they have the same ID.. However, a problem that I see is, when a query returns 'empty/null' result, is that there's no "__typename" being returned for obvious reason, so perhaps that is my problem, and I'm wondering how am I supposed to deal with the cache for all those ~not found queries that have no __typename being returned. The library will cache the query with the query name (not the type) and the id, so the mutation probably is unable to override such null cached entry by the lack of typename, and most likely my problem here, unless they are just entirely independent (I see cache entries with "Query" as the main key, and other entires with "Mutation" as the main cache key, so they seem to be separated/independent, is that accurate? I'm looking for confirmation of this caching Query/Mutation independent behaviour, and suggestions on how to deal with scenario B around my NULL issue, but first, I'll share scenario A (the tested happy path), to show how the cache is being populated for happy path, which helps better understand my scenario B's NULL caching issue.. Here's my setup: GraphQL Client Definition:
Here's how I display the cache entries after each operations:
======================================
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Thanks for opening this discussion @Frank-D. This is a reoccurring question in different shapes and conditions (e.g. #1117). As I can see you have two questions in your example:
For the first question. The reason why it's fetching the query from the network is that you haven't cached the The answer to your second question is related. Performing a mutation will not store any information about the relationship between the How can you fix this? You can do three things on the top of my mind:
|
Beta Was this translation helpful? Give feedback.
Thanks for opening this discussion @Frank-D. This is a reoccurring question in different shapes and conditions (e.g. #1117).
As I can see you have two questions in your example:
For the first question. The reason why it's fetching the query from the network is that you haven't cached the
Query
with theuser
field. The client can't assume that fetchinguser(authID: 12345)
maps to the user with auth-id12345
. While it might intuitively make sense for us as developers, codifying such an assumption would be prone to bugs. E.g., consider the API where you're only allowed to …