javascripteverywhere / web

💻 Web code examples for JavaScript Everywhere by Adam Scott, published by O'Reilly Media
https://www.jseverywhere.io/
MIT License
75 stars 56 forks source link

InMemoryCache #20

Open ivonakov opened 4 years ago

ivonakov commented 4 years ago

https://github.com/javascripteverywhere/web/blob/26319b2669bb9b7e80ae7015a5cd60d59fd26629/final/App.js#L7

The InMemoryCache class resides in a different package from the Apollo Client core. Make sure the apollo-cache-inmemory package is installed in your project

https://www.apollographql.com/docs/react/caching/cache-configuration/

https://github.com/javascripteverywhere/web/blob/26319b2669bb9b7e80ae7015a5cd60d59fd26629/final/App.js#L45 => Uncaught TypeError: cache.writeData is not a function at Object.parcelRequire.App.js.react (App.js:45)

ascott1 commented 4 years ago

For anyone who comes across this issue, we discuss it in more detail here: https://spectrum.chat/jseverywhere/general/chapter-15-web-authentication-and-state-issue-with-cache-writedata~998a84e5-9caa-4dab-bccb-d97fea79c1b1

The tl;dr summary is: If you are using this repo as a starter project as shown in the book, you should not experience this issue.

However, when using the latest version of Apollo the two packages need to be installed separately:

import { ApolloClient, ApolloProvider, createHttpLink } from '@apollo/client';
import { InMemoryCache } from 'apollo-cache-inmemory';

Otherwise, the code should be the same.

Hopefully that helps!

cip123 commented 4 years ago

I think this is related to this issue, @appolo/client removed client.writeData in version 3.0.0-beta.48 so the signIn code is no longer working. I tried this workaround

client.cache.writeData({ data: { isLoggedIn: true } });

but now the problem is that

 const { loading, error, data } = useQuery(IS_LOGGED_IN);

in PrivateRoute is returning stale data after the succesful login redirect so I need to log in twice