Apollo客户端持久缓存不起作用

时间:2018-08-01 09:57:12

标签: reactjs graphql apollo react-apollo apollo-client

我有一个带有apollo客户端的react应用。当用户登录时,我可以查询客户端状态并检索用户,但是刷新页面时,不再设置用户数据

如您所见,我有一个<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools" android:drawableEnd="@drawable/ic_spinner" android:drawablePadding="4dp" android:drawableRight="@drawable/ic_spinner" android:gravity="right" android:padding="8dp" android:textColor="@color/colorPrimary" android:textSize="14dp" /> 文件,在其中设置了客户端,然后将其传递给包裹整个应用程序的index.js

ApolloProvider

我应该补充一点,刷新页面后登录// index.js const client = () => { const cache = new InMemoryCache() persistCache({ cache, storage: window.localStorage }) return new ApolloClient({ uri: graphQLUri, credentials: 'include', clientState: { defaults: { locale: "en-GB", user: { __typename: "User", isLoggedIn: false, username: "", salesChannel: "", fullName: "" } }, typeDefs: ` enum Locale { en-GB fr-FR nl-NL } type Query { locale: Locale } ` }, cache }) } ReactDOM.render( <ApolloProvider client={client()}> <App /> </ApolloProvider>, document.getElementById("root") ) 时,我可以看到登录的window.localStorage对象。刷新页面后查询用户时,我仅获得在clientState默认值中设置的默认值。

此外,我知道user查询是有效的,因为在登录并写入用户对象缓存之后,GET_USER查询将运行并返回用户对象。

作为参考,我的GET_USER查询如下:

GET_USER

为什么用户对象在重新加载页面后不保留在缓存中?

1 个答案:

答案 0 :(得分:1)

显然,用InMemoryCachewriteQuerywriteFragment进行的缓存更改在重新加载页面后消失

Apollo Client docs:

  

如果重新加载环境,则对writeQuery和writeFragment所做的更改将消失。

相关问题