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

Expose the internal ApolloRequest<D>.fetchFromCache extension property #6307

Closed
Emplexx opened this issue Dec 7, 2024 · 3 comments
Closed

Comments

@Emplexx
Copy link

Emplexx commented Dec 7, 2024

Use case

I am trying to implement a client-side rate limit in my application using an ApolloInterceptor, and since my client is configured with a normalized cache, the issue I'm running into is that I have no way of telling whether I should limit the request or not, since I don't know if it's going to the network (what I'm trying to limit, to spare the API I'm using), or to the cache. I tried to look through how normalized caching is implemented and found the fetchFromCache extension property.

Describe the solution you'd like

I would like that property to be public (potentially marked with @ApolloInternal?), or another way to tell if the ApolloRequest inside of an interceptor is going to the cache or not. Sorry if I got anything wrong

@martinbonnin
Copy link
Contributor

Hi 👋

In order to do that, I'd recommend registering your ApolloInterceptor after setting up the cache:

ApolloClient.Builder()
  .serverUrl("https://...")
  .normalizedCache(normalizedCacheFactory)
  .addInterceptor(myRateLimitingInterceptor)

This way, your interceptor will only be called if no request was found in the cache.

Alternatively, you could also use an HttpInterceptor.

@Emplexx
Copy link
Author

Emplexx commented Dec 8, 2024

Ah, I was under the impression that an interceptor would still be called in this scenario. This works for my use-case so I will close this issue. Thank you!

@Emplexx Emplexx closed this as completed Dec 8, 2024
Copy link
Contributor

github-actions bot commented Dec 8, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better.

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

No branches or pull requests

2 participants