-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[Proposal] - Disabling Cache #1419
Comments
@chriscartlidge are you using Apollo Client in nodejs for server side rendering? If so, cache context should be garbage collected so long as you aren't keeping references to it after the render request. If you are simply trying to make server-to-server GraphQL requests, I would advise you to use something other than Apollo Client for that use case... probably just raw |
Thanks for filing an issue, @chriscartlidge! This is definitely an interesting thought we haven't heard much about yet! I'd love to find out more about the use case here, and the reasons you have selected Apollo for this case. At first glance, it doesn't sound like being able to control the caching on a per-query level is necessary for what you are doing, and disabling caching entirely is sufficient. Some specific questions:
|
Hi guys, so what's the best way to disable cache on ApolloClient? |
@phuochau I don't think disabling the cache is currently supported. I believe the intent behind apollo is to provide an opinionated graphql + caching solution for building client-side apps. |
@chriscartlidge This is something we've been thinking about for a while. I'm curious about your specific use-case, because it might make more sense to simply fetch the GraphQL query "by hand" if you don't ever want to cache things and don't need results to be reactive (i.e. updating when newer results from overlapping queries come in). Apollo Client currently uses the normalized cache to watch for updates to queries, which means that turning off the cache would make Apollo effectively just a fancy fetch function. However, we have thought about this use-case, and our currently favored idea is the following:
Note that the If you're interested in this, we could get you started on implementing this in a PR! Does that sound like something you'd have time & motivation for? |
@chriscartlidge @phuochau Disable cache? If you spend more time reading sources then writing issue, you will discover that you can use |
@langpavel I don't think most people read through source code in their spare time. Some of us contribute more than others. That is to be expected, and I think everyone deserves respect :) |
@thebigredgeek Reading sources is for me the easier way in case of Apollo :-) So sorry, I didn't want to be impolite :-) |
Project I'm working on has a specific data structure which makes me unable to use |
@thebigredgeek @helfer @stubailo Thanks for the constructive replies and apologies for replying back to this late 👍 @stubailo Let me give you a bit of overview about our setup at OpenTable. OverviewWe currently have a front end structure where our front end is divided into self-contained As you can expect there is a use case of sharing frontend For this, we are using something that we developed and open-sourced called OpenComponents which allows to build and share these components across microsites. These components are hosted on an OC-Registry and microsites can make a Where the apollo-client comes inWe can have many components on a single page e.g. the header, restaurant-list and location picker. These components are responsible for fetching of their own data via calling our GraphQL API. Our idea of using the This worked quite well for us at first as each instance of the registries had a singleton instance of the client which the components used to fetch their data and we had a lot of ROI on the batching/de-duplication cross components. However, due to the amount of request that we have (double digit millions per day) for components the caching got out of hand as it kept growing and growing and thus the proposal I sent above. We currently don't use pagination, mutation handling or polling but it is something we are very interested in doing in the coming months. Note: We also use Apollo within our microsites for server-side render when components are not used. ConclusionI hope that's a decent overview of our current situation and why we have chosen the apollo-client for use with OpenComponents. I like the suggestion that you have made @helfer and I'd like to explore helping out more on getting these supported within the client. |
I am wondering if we could solve this, along with other requests for a more flexible caching structure, by implementing a sort of "pluggable" storage interface, as we do with |
This would be nice. I'm working on a project using graphql and we will be doing queries with sensitive data that can't stay around in memory. Being able to disable cache for these queries would be great. I'd rather be able to use apollo for these instead of doing async calls myself. |
@thebigredgeek I was doing exactly what you said not to here: #1419 (comment) I'm using Presumably I don't need to use ApolloServer either, but just an executable schema? |
I acomplished that, using directly @bind
loadMoreItems(offset, limit) {
const { client, loadMoreItemsGQLQuery } = this.props;
return client.networkInterface.query({
query: loadMoreItemsGQLQuery,
variables: {
offset,
limit
}
}).then((res) => {
const { data } = res;
_(data)
.values()
.first()
.map((item, i) => {
this.items.set(offset + i, item);
})
;
});
} |
@maximblack I also managed to achieve this using a network-only fetch policy like so: const { client } = this.props
client.query({
query: myGQLQuery,
fetchPolicy: 'network-only', // skip the cache
}).then(res => {
// done
}) Use the |
@maximblack @credli you might be interested in what we're working on over here: https://github.com/apollographql/apollo-fetcher/blob/44fe1084954882039d76edbbf921882577d8fb13/SPEC.md |
This issue has been automatically marked as stale becuase it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client! |
If we can set const client = new ApolloClient({
defaultFetchPolicy: 'cache-and-network'
}) |
@vinhlh Yeah! That would be great! |
Could you provide any help on how to use |
I am also using |
Apollo Client doesn't cache on disk - it's just a JS variable, so when you close the page it's all gone. |
Yes, the issue is specifically for when server rendering. Just adding a
warning here that your solution won't work for everyone. :)
…On Oct 27, 2017 7:50 AM, "Dave I." ***@***.***> wrote:
@mhuggins <https://github.com/mhuggins> I've tested it and is working for
me...i'm not using server rending , it's react app front end app
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1419 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAKUhQ0CzKBrCxhNM4kqZmsmyRZ_MRYUks5swd-_gaJpZM4MfNbs>
.
|
is it still possible to use apollo.networkinterface directly with apollo 2.0? |
@michaelknoch |
this works like charm, thanks @rrhvella example: const query = operation => makePromise((execute(apolloClient.link, operation)));
query({ query, variables })
.then(result => {
console.log(result)
})
.catch(err => this.setErrorState(err)); I wondered why its not possible to create a separate apolloClient without cache. const clientWithoutCache = new ApolloClient({
link: new HttpLink({ uri: 'http://api.githunt.com/graphql' }),
cache: null
}); |
How about a NullCache implementation? It would work akin to a mock cache, with support for fragmentMatcher and whatever else is needed, but would not actually store anything, and read operations would return NOT FOUND. |
I just overwrote the query method: const apolloQuery = client.query.bind(client)
client.query = async (...args) => {
await client.cache.reset()
return apolloQuery(...args)
} EDIT: |
@OskarKaminski Dosn't seem to work. (apollo-client: 2.0.4) I find creating an ObservableQuery using watchQuery and then refetch much more idiomatic. |
@OskarKaminski fetchPolicy globally does not work, I must to do it one by one |
I am also hitting a situation where the Apollo Cache seems to be far to aggressive and we are not done completing all of our mutations. Due to this we keep getting some stale data that is a property of an object causing 'bugs' currently during our refactor to utilize graphql more predominantly. |
@webmobiles Your solution worked perfectly for me. I'm running a React frontend with React-Tables that's using Server-side pagination. When I would go from the Table > Profile page > delete object > Redirect back to Table a new query was not being fired.
In the application I'm building there is the same need for cache on the frontend. It would be great to have something with the network layer to disable cache.
That would be fantastic. I understand the importance of cache and I'm probably an edge case but would be super helpful to have something like that. |
i have just read the apollo-client 2.2.2 changelog: has anyone tried it already? |
Im using |
As mentioned in #1419 (comment), modern versions of |
@hwillson this only disables the cache per query, but we need the option to disable the cache globally |
@bogdansoare The |
@hwillson understood. Will something like this work ?
|
@bogdansoare That should work - let us know if it doesn't! |
const defaultOptions = {
watchQuery: {
fetchPolicy: 'no-cache',
errorPolicy: 'ignore'
},
query: {
fetchPolicy: 'no-cache',
errorPolicy: 'all'
},
} Unfortunately that did't work for me. My queries return I'm using the |
@paschaldev Any chance you could put together a small runnable reproduction that shows this happening? That would greatly help with troubleshooting. Thanks! |
@hwillson I'll try to. For now I'll just go back to using the cache |
To Disable Query Component Caching with react-apollo, you can use that;
|
Thanks its great we closed our issue |
If you are using Nuxt, one strategy can be to write a plugin which wraps the query function on the default client, to turn off cache. This code will yield an injected function $apolloQuery() which can be used in the app. Create a plugin as
(implement: then in
this allows for you to call queries in your store such as:
|
Before getting into the issue I just wanted to say what a great job you are doing with the apollo-client here at OpenTable we really are enjoying working with it!
Situation
We are currently using the apollo-client in a number of different use cases at it's working well 👍.
However, we have one application which is consuming the apollo-client via node-js and it is very memory sensitive (for various reasons) and because of this having the cache/store caching all queries and data is not great for us.
Proposals
Because of this, we have a couple of solutions that we'd like to propose:
1 (Disabling the cache globally)
One possible way to disable caching is to specify a cachePolicy hash when creating the client within the networkInterface, with the default policy to always cache e.g.
Advantages:
Disadvantages:
noFetch=true
with caching set to false would produce no results, which could be strange if the developer didn't have the context of what the policy was set to globally.2 (Disabling per query)
The next natural alternative to controlling the cache policy is to do this at a query level, with the default policy to always cache e.g.
Advantages:
Disadvantages:
3 (Cache size Limit)
Instead of disabling the caching we could set a byte limit for the cache size within the networkInterface e.g.
Advantages:
From a personal point of view, I'd like to be able to control the cache on a per-query level whilst also being able to set the size of the cache.
What are your thoughts on this?
The text was updated successfully, but these errors were encountered: