title: How to hydrate Apollo client from NextJS server
body: <p>I am using a custom NextJS server with Apollo Client. I want to fetch the GraphQL data server-side and then send it to the client. I was kind of able to do that, but the client-side fetches it again. I understand that the Apollo cache is available only on the server, then needs to be sent to the client and restored from there.</p>
  
  <p>The <a href="https://www.apollographql.com/docs/react/features/server-side-rendering" rel="noreferrer">Apollo docs</a> mention SSR but I don't want to fully render my app using the Apollo client, I want to use NextJS, I want to get just the data from the Apollo client and manually inject it into the HTML to restore it on the client. I looked at some examples for NextJS using Apollo, but none of them showed how to do exactly that.</p>
  
  <p>This is my custom handler for requests: </p>
  
  <pre><code>const app = next({ dev: process.env.NODE_ENV !== 'production' });
  
  const customHandler = async (req, res) =&gt; {
    const rendered = await app.renderToHTML(req, res, req.path, req.query);
    // somehow get the data from the apollo cache and inject it in the rendered html
    res.send(rendered);
  }
  </code></pre>