我正在使用apollo-client,apollo-link和react-apollo,我想完全禁用缓存,但不知道该怎么做.
我读了apollo-cache-inmemory
它的源代码,它config
的构造函数中有一个参数,但是我无法构建一个虚拟函数storeFactory
来使它工作.
你可以defaultOptions
像这样设置你的客户:
const defaultOptions = { watchQuery: { fetchPolicy: 'no-cache', errorPolicy: 'ignore', }, query: { fetchPolicy: 'no-cache', errorPolicy: 'all', }, } const client = new ApolloClient({ link: concat(authMiddleware, httpLink), cache: new InMemoryCache(), defaultOptions: defaultOptions, });
fetchPolicy
为network-only
避免使用缓存.
实际上,设置fetchPolicy
为network-only
"仍然保存对缓存的响应以供以后使用,绕过读取并强制网络请求".
如果你真的要禁用缓存,读取和写入,使用no-cache
.
看看官方文档:https://www.apollographql.com/docs/react/advanced/caching.html#ignore
I would always suggest not to disable inbuild caching feature from apollo client. Instead you can always set fetchPolicy: 'network-only'
for an individual queries.
Something like this
{({ loading, error, data, refetch, networkStatus }) => { ... }}
While fetching data with this Query, it would always do a network request instead of reading from cache first.