我尝试了所有发现的一切,但是没有运气。
- persistCache(将缓存保存到localStorage,但刷新后又恢复了,因此没有用)。
- stateLink(withClientState)。不知道这是干什么的,但是没有成功。
在类星体之前,我使用nuxt来设置缓存和网络,并且效果很好。
我该怎么办,以便刷新时缓存不会恢复为登录状态?
这是我的整个apollo.js配置文件:
import { ApolloClient } from 'apollo-client'
import { InmemoryCache } from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'
import fetch from 'node-fetch'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import Cookie from 'universal-cookie'
//import { split } from 'apollo-link'
import { WebSocketLink } from 'apollo-link-ws'
import { getMainDefinition } from 'apollo-utilities'
import { persistCache } from 'apollo-cache-persist'
import { withClientState } from 'apollo-link-state'
import { ApolloLink,split } from 'apollo-link'
const token_name = 'apollo-token'
const httpLink = createHttpLink({ uri: 'http://localhost:4000/graphql',fetch: fetch })
const wsLink = process.browser ? new WebSocketLink({ // if you instantiate in the server,the error will be thrown
uri: `ws://localhost:4000/graphql`,options: { reconnect: true }
}) : null
const authLink = setContext((req,{ headers }) => {
const cookies = new Cookie(headers && headers.cookie)
const token = cookies.get(token_name)
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,...!!token && { authorization: token }
//authorization: token ? token : "",}
}
})
const link = process.browser ? split( //only create the split in the browser
({ query }) => {
const { kind,operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},wsLink,authLink.concat(httpLink)
) : authLink.concat(httpLink)
// persist cache
const cache = new InmemoryCache()
if(process.browser)
persistCache({
cache,storage: window.localStorage,})
// not used at the moment
const stateLink = withClientState({
cache,resolvers: {
Mutation: {
updateNetworkStatus: (_,{ isConnected },{ cache }) => {
const data = {
networkStatus: {
__typename: 'NetworkStatus',isConnected
},};
cache.writeData({ data });
return null
},},}
})
// Create the apollo client
const apolloClient = new ApolloClient({
link,// : ApolloLink.from([stateLink,link]),cache: cache,// new InmemoryCache(),// comment out for persist cache
connectToDevTools: true
})
export const apolloProvider = new VueApollo({
defaultClient: apolloClient,}
export default ({ app,Vue }) => {
Vue.use(VueApollo)
app.apolloProvider = apolloProvider
}