Open leebenson opened 6 years ago
An alternative work-around for this that also allows you to have use a defaultState
when you reset the store can be utilized following this pattern:
const defaultLinkState = {/*default values here*/};
export default function createClient(initialState) {
// 1. Create the cache and the apollo link state
const cache = new InMemoryCache();
const stateLink = createLinkState(cache, defaultLinkState);
// 2. AFTER the cache & link state is created, restore the initial state
if (initialState) {
cache.restore(initialState);
}
return new ApolloClient({
link: ApolloLink.from([
stateLink,
httpLink
]),
cache
});
}
There are 2 different states to be aware of:
defaultState
: is applied if client.resetStore()
is invokedinitialState
: the state the client receives from the server renderThis should be added to the documentation.
Specifying
defaults
in a call towithClientState()
always overwrites the local cache, even ifcache.restore(window.__APOLLO_STATE__)
is passed as thecache
property.My assumption would be that restoring a cached state would take precedence over defaults, and only an explicit call to
client.resetStore()
would reset to defaults.Currently, I am working around it as follows:
However, the above isn't ideal, since
defaults
is only available via SSR, and resetting withclient.resetStore()
means the client has no default state to return to.Looks like the behavior is defined by these lines