title: How to rehydrate my apollo state from server side?
body: <p>I am totally new to react-apollo I am pretty confused that how to rehydrate state from the server side to client And my app is working, But the problem is it is not using preloaded state from Apollo After component rendered it is calling the API again.</p>
  
  <p>Seriously Redux Integration Makes Complicated only Apollo state is rendering not the custom redux state that's the problem here.But I don;t know how to integrate.</p>
  
  <p>Server.js</p>
  
  <pre><code>const HTML = ({ html,state}) =&gt; (
  
      &lt;html lang="en" prefix="og: http://ogp.me/ns#"&gt;
      &lt;head&gt;
          &lt;meta charSet="utf-8" /&gt;
          &lt;meta httpEquiv="X-UA-Compatible" content="IE=edge" /&gt;
          &lt;meta httpEquiv="Content-Language" content="en" /&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1" /&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
      &lt;div
          id="app"
          dangerouslySetInnerHTML={{ __html: html }} /&gt;
      &lt;script dangerouslySetInnerHTML={{
          __html: `window.__STATE__=${JSON.stringify(state)};`,
      }} /&gt;
  
      &lt;script src="/static/app.js" /&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  );
  
  app.get('/*',(req,res) =&gt; {
      const routeContext = {};
      const client = serverClient();
  
      const components = (
          &lt;StaticRouter location={req.url} context={routeContext}&gt;
              &lt;ApolloProvider store={store} client={client}&gt;
                  &lt;WApp /&gt;
              &lt;/ApolloProvider&gt;
          &lt;/StaticRouter&gt;
      );
  
      getDataFromTree(components).then(() =&gt; {
          const html = ReactDOMServer.renderToString(components);
          const initialState = {apollo: client.getInitialState()}
  
  
          res.send(`&lt;!DOCTYPE html&gt;\n${ReactDOMServer.renderToStaticMarkup(
              &lt;HTML
                  html={html}
                  state={initialState}
                   /&gt;,
          )}`)
  
  
      })
  
  
  })
  </code></pre>
  
  <p>apolloClient.js</p>
  
  <pre><code>import ApolloClient, {
      createNetworkInterface,
      addTypeName,
  } from 'apollo-client';
  const isProduction = process.env.NODE_ENV !== 'development';
  const testUrl = 'http://localhost:3000/api';
  
  // const url = isProduction ? productionUrl : testUrl;
  const url =  testUrl;
  
  
  
  
  
  const client = new ApolloClient({
  
      networkInterface: createNetworkInterface({uri:testUrl}),
      dataIdFromObject:({id}) =&gt; id,
      reduxRootKey:state =&gt; state.apollo,
      initialState: (typeof window !=='undefined')? window.__STATE__:{}
  
  
  
  
  
  
  });
  
  
  
  
  
  
  
  export default client;
  </code></pre>
  
  <p>store.js</p>
  
  <pre><code>import { createStore, compose, applyMiddleware } from 'redux';
  import { syncHistoryWithStore } from 'react-router-redux';
  import thunk from 'redux-thunk';
  import {createLogger} from 'redux-logger';
  
  
  import client from '../apolloClient';
  import rootReducer from '../Reducers'
  
  //All Reducer
  import {initialState as allPosts} from '../Reducers/AllPosts_Reucer';
  const isProduction = process.env.NODE_ENV !== 'development';
  const isClient = typeof document !== 'undefined';
  const initialState = {
      allPosts
  };
  
  const middlewares = [thunk, client.middleware()];
  const enhancers = [];
  
  if (!isProduction &amp;&amp; isClient) {
      const loggerMiddleware = createLogger();
      middlewares.push(loggerMiddleware);
  
      if (typeof devToolsExtension === 'function') {
          const devToolsExtension = window.devToolsExtension;
          enhancers.push(devToolsExtension());
      }
  }
  
  
  
  const composedEnhancers = compose(
      applyMiddleware(...middlewares),
      ...enhancers
  );
  const store = createStore(
      rootReducer,
      initialState,
  
      composedEnhancers,
  );
  
  export default store;
  </code></pre>
  
  <p>Sample Component</p>
  
  <pre><code>import React,{Component} from 'react';
  import { connect } from 'react-redux';
  import { bindActionCreators } from 'redux';
  import { graphql } from 'react-apollo';
  
  import gql from 'graphql-tag';
  
  import * as postActions from '../../Redux/Actions/postActions';
  
  
  class Home extends Component{
      componentWillMount(){
          // console.log('From Will Mount',this.props.posts)
      }
      renderAllPost(){
          const {loading,posts} = this.props;
  
          if(!loading){
              return posts.map(data =&gt; {
                  return &lt;li key={data.id}&gt;{data.title}&lt;/li&gt;
              })
          }else{
              return &lt;div&gt;loading&lt;/div&gt;
          }
      }
      render(){
      console.log(this.props);
          return(
              &lt;div&gt;
  
                  {this.renderAllPost()}
  
              &lt;/div&gt;
          )
      }
  }
  
  
  //start from here
  const GetallPosts = gql`
  query getAllPosts{
    posts{
      id
      title
      body
    }
  }
  `;
  
  // const mapStateToPros = (state) =&gt; ({
  //     allPosts:state.allPosts
  // });
  
  const mapDispatchToProps = (dispatch) =&gt; ({
      actions:bindActionCreators(
          postActions,
          dispatch
      )
  });
  
  
  const ContainerWithData = graphql(GetallPosts,{
      props:({ data:{loading,posts} }) =&gt; ({
          posts,
          loading,
      })
  })(Home)
  
  
  export default connect(
      // mapStateToPros,
      // mapDispatchToProps
  )(ContainerWithData)
  </code></pre>