Skip to content
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

Support for Cache Redirects #842

Closed
Hbbb opened this issue Oct 17, 2019 · 11 comments
Closed

Support for Cache Redirects #842

Hbbb opened this issue Oct 17, 2019 · 11 comments
Labels

Comments

@Hbbb
Copy link

Hbbb commented Oct 17, 2019

Hi there! I'm currently in the process of putting together a POC for GraphQL for my company and I'm scratching my head on one thing in particular: Cache Redirects.

I have a textbook example of a list view with it's own query, which clicks through to a detail view with a different query, fetching the same data that's already returned from the initial list query. I'm noticing that although the data exists locally, Apollo caches based on the query, not the ID of objects (or their path in the graph, I'm still pretty new to all of this), so the detail view's query is a cache miss and hits the network.

I found a page in the Apollo React documentation that describes the exact functionality that I need: https://www.apollographql.com/docs/react/v2.5/advanced/caching/#cache-redirects-with-cacheredirects

Does this exist in the Swift client? Are there plans to introduce it? Does it already exist and I just missed it?

Thanks for your time!

@designatednerd
Copy link
Contributor

I would take a look at the updated caching documentation in #851 - You should be able to use the cacheKeyForObject method outlined in the Controlling Normalization section to set up your caches to use the ID as the cache key.

@designatednerd
Copy link
Contributor

@Hbbb Does the caching documentation answer your question?

@Hbbb
Copy link
Author

Hbbb commented Oct 29, 2019

I think I may be misunderstanding this. I've implemented cacheKeyForObject but I'm still seeing subsequent network calls to fetch objects that should be read from cache.

The app in question is something I put together for a proof of concept for my company. Let me push what I have up to a public repo and I'll link you to it. It's a tiny amount of code, so it should illustrate the problem quite nicely.

Also, full disclosure I'm very new to GraphQL and to iOS development, so this could very well be user error.

@designatednerd
Copy link
Contributor

Great, would love to see it - one thing to be aware of is that anything that doesn't have all the pieces of data requested for a particular cache hit is considered a cache miss, and will send requests to the network.

@designatednerd
Copy link
Contributor

@Hbbb Did you ever get that sample code together? Would love to get a look.

@Hbbb
Copy link
Author

Hbbb commented Nov 7, 2019

Hey @designatednerd! Sorry for the delay on this.

Here's the sample code: https://github.com/Hbbb/pokedex-graphql

@designatednerd
Copy link
Contributor

OK! So I ran your app while running Charles proxy, and I only saw one call actually go out to the network even when going to several pages. Here's a gif (because for %reasons% GitHub won't let me just upload a damned video) where you can see that a single network call is made when you click the pokeball, and then going into each of the detail screens, nothing hits the network:

no_calls 2019-11-07 14_06_45

@Hbbb
Copy link
Author

Hbbb commented Nov 11, 2019

Okay great! I didn't think to inspect the network traffic 🤦‍♂.

I just built and ran the app again and the last thing I'm confused about is why, when I initially tap a cell for a Pokemon that I haven't tapped before, there is a brief delay before the content appears. On subsequent taps, it's as though the screen is pre-rendered and the content appears immediately. Is this delay due to Apollo, or something completely unrelated?

@designatednerd
Copy link
Contributor

It may be due to getting the query watcher set up rather than just trying to perform a query, or potentially something where your detail is otherwise constructing itself, but I'm not positive. My best advice is to run the Time Profiler instrument and see where things are causing the main thread to spin.

@designatednerd
Copy link
Contributor

@Hbbb since we've found the answer for your question, can we close this issue out?

@Hbbb Hbbb closed this as completed Nov 13, 2019
@Hbbb
Copy link
Author

Hbbb commented Nov 13, 2019

Yep. Thanks so much for your time @designatednerd 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants