我和阿波罗客户有一个反应应用程序 . 当用户登录时,我可以查询客户端状态并检索用户,但是, when I refresh the page, the user data is no longer set .

正如你所看到的,我有我的 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 时,我可以看到我登录的 user 对象 . 当我在刷新页面后查询用户时,我只获得在clientState默认值中设置的默认值 .

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

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

gql`
  {
    user @client {
      username
      salesChannel
      fullName
      isLoggedIn
    }
  }
`

重新加载页面后,为什么用户对象不会在缓存中持久存在?