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

Apollo Client 2.0 getInitialState for SSR with Next.js #2259

Closed
jthegedus opened this issue Oct 5, 2017 · 5 comments
Closed

Apollo Client 2.0 getInitialState for SSR with Next.js #2259

jthegedus opened this issue Oct 5, 2017 · 5 comments
Assignees
Milestone

Comments

@jthegedus
Copy link

Intended 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 vercel/next.js#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
@jthegedus jthegedus changed the title Apollo 2.0 Apollo Client 2.0 and Next.js Oct 5, 2017
@jthegedus jthegedus changed the title Apollo Client 2.0 and Next.js Apollo Client 2.0 getInitialState for SSR with Next.js Oct 5, 2017
@jbaxleyiii
Copy link
Contributor

@jthegedus hmm this is an interesting point. I didn't expect keeping the cache reference around to be a problem but I do see that adding a quick method would be nice.

How about I implement something like this:

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
client.cache.extract()

That way cache is publicly attached to the client instance to be used as needed

@jbaxleyiii jbaxleyiii self-assigned this Oct 10, 2017
@jbaxleyiii jbaxleyiii added this to the 2.0 milestone Oct 10, 2017
@jbaxleyiii
Copy link
Contributor

cc @peggyrayzis for the upgrade guide / githunt work

@jthegedus
Copy link
Author

jthegedus commented Oct 10, 2017

Keeping the cache around isn't really a problem, but if it's not necessary because of apollo.queryManager.dataStore.getCache() then why split the functionality? If this type of usage is expected to remain available and stable, I don't see why I would want to track the cache separately (given what I currently understand, I could certainly be wrong).

Having the cache attached to the client in that manner would be more convenient than the queryManager.dataStore.getCache() method. Unless there are plans to support multiple caches on a single client object I don't see the harm.

@jbaxleyiii
Copy link
Contributor

The 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!

@jbaxleyiii
Copy link
Contributor

@jthegedus updated with #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 method

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants