我正在尝试使用apollo-link-state来管理本地状态。我读到过你应该用withClientState()设置解析器,默认值和缓存。所以我们开始了:
const cache = new InMemoryCache();
const defaultState = {
editGraph: {
__typename: 'EditGraph',
addNodeForm: 1,
addRelForm: 'test',
editGraphForm: false
}
};
const stateLink = withClientState({
defaults: defaultState,
cache
});
const client = new ApolloClient({
link: ApolloLink.from([
stateLink,
new HttpLink(),
]),
cache
});然后将client传递给apolloProvider:<ApolloProvider client={client}>,但我得到了错误TypeError: cache.writeData is not a function。它指出了这一行:const stateLink = withClientState({
我想查询和console.log默认值。但到目前为止我什么也没做。
从阿波罗的文档、youtube教程和博客文章中可以明显看出,cache.writeData错误不应该出现。那么为什么会在这里呢?
发布于 2018-01-11 12:43:27
该问题是由未满足的对等依赖项引起的。我再次安装了我使用的每个依赖项,现在它可以工作了。因此,如果您想在不久前启动的项目中安装和使用apollo-link-state,则需要更新它的对等依赖项。
发布于 2018-01-26 01:46:42
这也让我犯了一次错,为了给出更多信息,apollo-link-state用于将方法路径到apollo缓存,但后来api中包含了writeData。
因此,如果您使用的是apollo-cache-inmemory,请确保大于1.1.5。
这里是相关的PR:https://github.com/apollographql/apollo-link-state/pull/164/files
发布于 2020-08-28 21:13:39
如果要从@apollo/client导入Inmemory cache,则需要执行以下操作
import {
InMemoryCache
} 来自apollo-cache-inmemory,甚至在阿波罗版本3上
https://stackoverflow.com/questions/48181317
复制相似问题