I'm trying to setup a razzle-react application, but when I want to add an initial state in my server.js, the function client.extract() is returning an empty object, thus, throwing an error when I try to modify said object.
This is a snippet of my server.js file
const Root = () => (
<ApolloProvider client={client}>
<ThemeProvider theme={theme['LIGHT']}>
<StaticRouter location={req.url} context={context}>
<React.Fragment>
<App />
<Global />
</React.Fragment>
</StaticRouter>
</ThemeProvider>
</ApolloProvider>
);
try {
await getDataFromTree(<Root />);
} catch (e) {
console.log(e);
}
let initialApolloState = client.extract();
console.log('initial apollo state', initialApolloState);
// chaning intial state. This is throwing an error.
initialApolloState.ROOT_QUERY.test = 'Hopefully this will work';
This is the Apollo Client:
export default new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser,
link: ApolloLink.from([
stateLink,
new HttpLink({
uri: 'https://api.graphcms.com/simple/v1/cjhdcwrb98if90109o4pzawaq'
})
]),
cache:
process.browser && typeof window !== 'undefined'
? new InMemoryCache().restore(window.__APOLLO_STATE__)
: new InMemoryCache()
});
And the dependencies used are:
"apollo-cache-inmemory": "1.3.0",
"apollo-client": "2.4.2",
"apollo-fetch": "^0.7.0",
"apollo-link": "1.2.3",
"apollo-link-http": "1.5.5",
"apollo-link-state": "0.4.2",
"react-apollo": "2.2.2",
Try updating your dependencies? At least apollo-cache-inmemory
(currently 1.4.1) and apollo-client
(currently 2.4.10).
@benjamn Just tried that, same behaviour.
@charlie632 What would you expecting to have in initialApolloState
? This is actually part of the bug issue template which you chose to ignore.
Thanks for reporting this issue originally!
I'm going to close this issue since it hasn't received a lot of traction and could have been resolved already, but without a runnable reproduction, it's difficult to quickly check.
If this is still a problem, would someone who's experiencing the problem (or anyone who comes across this issue and is able to assist further) mind building a reproduction of the problem in to a runnable CodeSandbox reproduction and sharing the link to that CodeSandbox in this issue?
I'm happy to re-open if this is still occurring and someone can provide a reproduction. Thanks again!
I ran in the same issue, calling client.extract()
will not "immediately" include the data, but you could wait for it.
const intervalID = setInterval(() => {
const data = client.extract()
if (JSON.stringify(data) !== '{}') {
clearInterval(intervalID)
console.log(JSON.stringify(data), new Date())
}
}, 50)
@sbstjn this is bug =)
I ran into the same problem.
Perhaps you are using apollo-client v3 and use import { getDataFromTree, renderToStringWithData} from '@apollo/react-ssr';
Try import {getDataFromTree, renderToStringWithData} from '@apollo/client/react/ssr';
It helped me.
More information here https://github.com/apollographql/react-apollo
Most helpful comment
@sbstjn this is bug =)