Open isopterix opened 6 years ago
Btw. the way I solved it for now is to manually set any query with a "@client" directive in it to use
fetchPolicy='cache-first'
I haven't checked this with any mixed client/network queries yet though...
Even I am facing similar issue where default data is being returned instead of actual updated data from cache. Any updates on this?
Hi @isopterix, can you please provide a stripped down reproduction in CodeSandbox so I can look into this? Thanks!
Hi @peggyrayzis, will try to put something together shortly.
I have the same problem even with setting fetchPolicy='cache-first'
I tried to put something together... but for whatever reason I am getting "Cannot read property 'Query' of undefined" in the console... https://codesandbox.io/s/82m9r8p379
Any updates on this? I had to remove defaults as work-around for this issue.
Also experiencing this issue. With a mixed client/network query, the issue seems to still occur even when using fetchPolicy='cache-first'
.
I'm seeing the same issue. Defaults overwrite the persisted cache when a global config of cache-and-network
is set.
Hi, Is there a temporary fix or work-around for this?
I’m experiencing a similar issue. I’m using next.js and cache is being filled on a server side, dumped to var, which is passed to the browser and used for rehydration during cache initialization on the browser side. Problem occurs when I’m trying to query cache after - it contains only defaults from apollo-link-state. It looks like setting defaults doesn’t care if there is something in the cache already.
Same for me :(
I've got same issue, and fixed it by removing the defaults from withClientState(), and writing them directly in the apollo cache.
Here is my code:
const DEFAULT_STATE = {
networkStatus: {
__typename: 'NetworkStatus',
isConnected: true,
isWebSocketSupported: true,
},
};
const stateLink = withClientState({
cache: apolloCache,
resolvers: {
Query: {},
Mutation: {},
},
// defaults: DEFAULT_STATE,
});
apolloCache.writeData({ data: DEFAULT_STATE });
Same problem here. I've broken it down to 2 separate issues:
cache-first
where the onCompleted
callback would never be called. cache-and-network
seemed to solve the problem, but really its a race condition and cache-and-network
just switches up the race a bit. The problem is that the query is trying to run before/during store rehydration. Like most people the first thing my app does is run a query using the Query
component which happens on first render. Any kind of delay will hide this problem. At first I tried putting in a timer, but then I realized it was enough to wait until the component mounted. Still, depending on the environment, its probably still vulnerable to breaking under the right conditions. Either apollo needs to be smart enough to queue queries until the store is rehydrated, or we need access to the rehydration state.Oh BTW this guy seems to have found a workaround for not overwriting with defaults: https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/195#issuecomment-428668407
apollo-link-state is in the process of being integrated into apollo-client, including multiple bug fixes and new features. For more info, see https://github.com/apollographql/apollo-client/pull/4338. You can try it out by installing apollo-client@alpha
.
I'm not personally involved in the development and haven't tested to see if it fixes this bug, but my understanding is that it should be fixed by the time apollo-client 2.5 is released. Note that the API is still subject to change.
Anyone who can explain why cache-and-network
is not working with query
?
Guys, I'm having very similar issues. Have a good look at your indexing logic. Any null keys are not cached. Worse: non-unique keys tend to be overwritten. Make sure this only happens when you want it to.
Apollo 2.5 has been released. I doubt there will be any further changes to this library now that local state has been integrated into the core.
@peggyrayzis could we move this to the apollo repository?
Hi,
I ran into an issue when setting the global fetchPolicy setting for query and watchQuery to cache-and-network while using apollo-link-state and apollo-cache-persist.
My defaultOptions in my Apollo configuration look like this:
For whatever reason I cannot get Apollo to grab the persistent cache from the browser and use the client state flag to assess whether a user is logged in or not.
When I set the queries' fetchPolicy manually to cache-and-network without defining it globally via the defaultOptions tag in the Apollo configuration everything works fine. However, if I use the same approach to fetch the local state via the "@client" directive, the local state read from the cache is somehow overwritte with the default when the network re-fetch is initiated shortly after the cache is initially read. Hence, the user always sees the login screen as the appUserIsLoggedin setting is always reset to false.
Below are my two config files. Note that in this version the defaultOptions are commented out and hence the Q_GET_APP_LOGIN_STATE query in the index file is fetched standard cache-first method. THIS WORKS AS EXPECTED!
However, if I activate the defaultOptions setting OR manually set fetchPolicy for the Q_GET_APP_LOGIN_STATE query to cache-and-network, the appUserIsLoggedin variable is initially true when loading the page and a cache with it set to true is present. However, shortly after, the local-state variable is automatically set to false again for whatever reason. I assume this is the result of the automated network re-fetch.
PLEASE NOTE: This ONLY affects data which is stored in the local-state. Data fetched from the network works as expected regardless of the used fetchPolicy setting.
Any ideas what may be the cause for this?
My React Index file:
My Apollo configuration:
My package versions: