Apollo Client 2.0 getInitialState for SSR with Next.js
See original GitHub issueIntended outcome:
Upgrade the Next.js with-apollo example to use Apollo Client v2.0.0.
Actual outcome: Everything works, I just would like some feedback on the method used.
Originally I followed the Apollo Client v2 upgrade guide and implemented the recommended
import ApolloClient from "apollo-client";
import InMemoryCache from "apollo-cache-inmemory";
const cache = new InMemoryCache();
const client = new ApolloClient({ cache });
// do some data loading things using getDataFromTree
const state = cache.extract();
however found a discussion from the Apollo apollo-client-core Slack channel that said you could extract the state from the cache through the client:
const state = apollo.queryManager.dataStore.getCache().extract()
Since this is simpler I’ve implemented the latter. The pending PR is here https://github.com/zeit/next.js/pull/3029. Any comments before it gets merged on whether or not this is a valid method for grabbing the state from the cache would be good. It’s much nicer to only have to export the ApolloClient object from the https://github.com/zeit/next.js/pull/3029/files#diff-5cdabd33c0ae9862f9c98f19dff96b85 file, instead of the storing and exporting the cache itself too.
Thanks for your time!
Version
- apollo-client@^2.0.0-beta.3
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top GitHub Comments
@jthegedus updated with https://github.com/apollographql/apollo-client/pull/2281
Both previous ways will work, but once that beta is out, you can do
client.cache.extract()
and I’ve updated the upgrade guide to use it as the primary methodThe idea was that you typically define the cache where you need to extract the data, but that may not always be the case. I think attaching it to the client and recommending that as the official way is the best / easiest bet!
Thanks for the idea!