question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apollo Client 2.0 getInitialState for SSR with Next.js

See original GitHub issue

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 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
jbaxleyiiicommented, Oct 11, 2017

@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 method

0reactions
jbaxleyiiicommented, Oct 10, 2017

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started With Apollo Client in Next.js
In this post, we'll briefly cover each way to fetch and render data with Next.js, along with the important considerations for each approach....
Read more >
Server-side rendering (SSR) using Apollo and Next.js - Medium
I'm initializing the apollo client at the very first line since on the server-side it is better to create a new client for...
Read more >
Using Apollo Client for Next.js SSR and CSR with TypeScript
We setup Apollo Client to make requests to our GraphQL API. I also show you how to make your requests faster by connecting...
Read more >
Migration Guide – Angular - GraphQL Code Generator
Updating your app to Apollo Client 2.0 and Angular Apollo 1.0. ... If you have previously used getInitialState for SSR, that API has...
Read more >
graphql-hooks - npm
js bundle is a whopping 93% smaller (7.9KB vs 116KB). ApolloClient ➡️ GraphQLClient. - import { ApolloClient } from 'apollo-client' - ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found