简体   繁体   English

cache.writeData 不是函数 -> inMemoryCache 错误

[英]cache.writeData is not a function -> inMemoryCache error

I'm working with react and apollo client.我正在与 React 和 apollo 客户合作。 i'm get confusing error and i can't find exactly the problem.我遇到了令人困惑的错误,我找不到确切的问题。 i have the code.我有代码。

import logo from './logo.svg';
import './App.css';
import Pages from './pages';
import GlobalStyles from './components/GlobalStyles';

import { ApolloClient, ApolloProvider, createHttpLink, InMemoryCache } from '@apollo/client';
import { setContext } from 'apollo-link-context';

const uri = process.env.REACT_APP_BASE_URL;
const httpLink = createHttpLink({ uri });
const authLink = setContext((_, { headers }) => {
    return {
        headers: {
            ...headers,
            authorization: localStorage.getItem('token') || ''
        }
    }
})
const cache = new InMemoryCache();

// configure Apollo Client
const client = new ApolloClient({
    link: authLink.concat(httpLink),
    cache,
    resolvers: {},
    connectToDevTools: true
})

const data = {
    isLoggedIn: !!localStorage.getItem('token')
}

//write the cache data on initial load

// here's the problem
cache.writeData({ data });

function App() {
    return (
        <ApolloProvider client={client}>
      <GlobalStyles/>
      <Pages/> 
    </ApolloProvider>
    );
}

export default App;

i got an error.我有一个错误。 在此处输入图像描述

i hope this error is cleared and removed.我希望清除并删除此错误。 so it can loaded login data.所以它可以加载登录数据。 any help will be appreciated.任何帮助将不胜感激。 thank you.谢谢你。

from what I know you can't just write directly to the cache like that据我所知,你不能像那样直接写入缓存

you should be using writeQuery to write to the cache.您应该使用 writeQuery 来写入缓存。 use the folowing link for more information on how to use it.使用以下链接获取有关如何使用它的更多信息。

https://www.apollographql.com/docs/react/caching/cache-interaction/ https://www.apollographql.com/docs/react/caching/cache-interaction/

if i'm wrong let me know then i can learn too如果我错了让我知道然后我也可以学习

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:cache.writeData 不是函数 --&gt; apollo-link-state - TypeError: cache.writeData is not a function --> apollo-link-state 如何最好地使用 ApolloClient / InMemoryCache 并为 API 启用按需缓存? - How to best use ApolloClient / InMemoryCache and enable Cache on demand for API's? 定义自定义合并 function 以解决 GraphQL 中的 InMemoryCache 合并 - Defining custom merge function to resolve InMemoryCache merge in GraphQL Apollo客户端状态-缺少使用writeData写入缓存的对象 - Apollo Client State - object written to cache with writeData is missing function RouteHandler in./.cache/root.js:21 中的错误 - Error in function RouteHandler in ./.cache/root.js:21 如何在不更改获取策略的情况下使用 InMemoryCache 重新获取 Apollo GraphQL 查询? - How to refetch an Apollo GraphQL query with InMemoryCache and without changing the fetch policy? 反应通过缓存并在上下文中获取/设置 function - React pass cache and get/set function in context Apollo:graphql 错误后如何处理缓存? - Apollo: How to handle cache after graphql error? 尝试更新缓存阿波罗并绘制UI时出错 - Error to try update cache apollo and draw the UI 多次使用同一个Query时inMemoryCache中的relayStylePagination问题 Apollo3 - relayStylePagination in inMemoryCache problem when using the same Query multiple times Apollo3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM