-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there any documentation on how CacheKeyResolver works for normalized cache #1370
Comments
Ugh, this issue really needs to be resolved. Took me quite a while to figure out this out. Until this gets better documentation, hopefully my comment can help others out (and maybe someone can also correct me if I got this wrong).
For example, going off of the example given in the normalized cache documentation:
Calling the BookWithAuthorName query will result in:
Calling AuthorById afterwards will result in:
If configured properly, the fromFieldArguments will generate the same cache key as the previous |
For anyone who comes here, there's now a dedicated page for the normalized cache. You can also follow #2331 for updates about normalized cache improvements. |
Hello, I just started messing with the normalized cache so sorry if my question to follow is already answered or the answer is obvious but I fail to see it. With the current way CacheKeyResolver works it is sound and clear to me how the cache, in the above example, is able find the author (if any) of the AuthorById query when a BookWithAuthorName query has previously succeeded, at least once. However, it is not clear to me how I can get the book when I have the author (going up the leaf?!). Any clearance/hints on that would be highly appreciated. thx |
From the given example, it'd be impossible, as the AuthorById query doesn't return any book information, so there'd be no book information to retrieve from the cache, even if the keys were somehow resolved correctly. Therefore, the question is moot. |
Yeah I was referring more on the how the CacheKeyResolver could be employed to provide such capability in general, not this particular example. The only possible way, I see this happening, is actually providing some fields inside the Author that can be used to calculate the book id?! but this seems like a really hacky way to go. |
A more reasonable example would be if the Author schema included a books field, which had a list of books the author had written. Then, just like how you can cache an author from a top level book query (since a book contains information on its author), you could cache each book in an author.
Since ids are globally unique in the example, all you'd need to do is fetch the id for each book and associate it with its entry. The page on cache normalization already shows an example of how to do it with books -> author, and it would also work in the reverse case.
I'm not sure if this is really hacky at all. It feels like GraphQL encourages this, as you can easily refer to another object type from within an object, and it'd be very reasonable to be able to look up all the Book objects written by an Author. I'd take a look at this fake schema, which does something similar, where a User type has the capability to look up that User's posts. |
Hi everyone 👋 Apollo Android 3 introduces declarative cache directives that make it easier to define your cache keys without implementing a If you still required the flexibility of programmatic cache ids, there's also a separate page for that. Hope this helps! I'm going to close this issue as it's more than one year old. If anything's unclear, please feel free to open a new issue and we'll dive into it |
Wanted to know how CacheKeyResolver works for normalized cache.
what is the difference between fromFieldRecordSet and fromFieldArguments, and when are they called?
The text was updated successfully, but these errors were encountered: