Reading cached data failed for some reason(solved)

I encountered a weird bug that’s impossible for me to debug, seriously need some help!!!
I am reading some similar propperties from a cached Post object with these two fragments:

The type of the two fragments:

The cachedVotes returned the cached value I wanted, but the other fragment returned an error:

From above I printed out the cache object itself, for more info:

These two are the same thing IMO, but why is this happenning…
Can someone help me point out something I did wrong and help me understand this, I am cluless, spending two hours debugging…
Thank you!

This situation is the best I can do in order to ask the question. When i was doing a simple cache.readQuery, it just return null, I have no idea …

What does PostsSnippetsFragmentDoc look like? Have you tried specifying the fragmentName option?

These are the two fragments, a nested fragment structure, could this be the reason? And I don’t understand the error message, since the type def are from code gen, but normal querying and mutating are all just working fine

Yep, I think that’s probably the issue. Both are fragments on Post, and the one being nested doesn’t give a good way to differentiate the two on the same fragment. I’d set the fragmentName to “PostsSnippet” on the PostsSnippetFragment read

cache.readFragment<PostsSnippetFragment>({
    fragment: PostsSnippetFragmentDoc,
    fragmentName: 'PostsSnippet',
    id: `Post:${id}`
});
1 Like

Holy shit…
I tested out, it is because of the nested thing!
Thank you for the help!
I think this is the same reason why my Post read returns null, it is deeply nested, but readQuery seems doesn’t have a queryName option for it, how do I get around it?

Here’s the issue: I can’t read the Post in cache.read or in cache.updateQuery:

Both just return a null when querying the same PostQuery

Ok, I didn’t provide the variables for the PostsQuery…

Oh, cool…all working now then?

Yeah, I am just so glad, this caching thing is so much clear now! Thank you man, you made my day!

Awesome, glad it’s working!

1 Like