【问题标题】:Persist cache with apollo-boost使用 apollo-boost 持久化缓存
【发布时间】:2023-03-19 10:16:01
【问题描述】:

我有个小问题。

我们如何使用 apollo-boost lib 持久化缓存?

我不确定如何使用以下配置实现apollo-cache-persist

const client = new ApolloClient({
  uri: 'http://localhost:8080/_/service/com.suppliers/graphql',
  clientState: {
    defaults: {
      networkStatus: {
        __typename: 'NetworkStatus',
        isConnected: false,
      },
    },
    resolvers: {
      Query: {},
      Mutation: {
        updateNetworkStatus: (_, { isConnected }, { cache }) => {
          cache.writeData({
            data: {
              networkStatus: {
                __typename: 'NetworkStatus',
                isConnected,
              },
            },
          })
          return null
        },
      },
    },
  },
})

提前谢谢!

【问题讨论】:

    标签: javascript reactjs caching graphql react-apollo


    【解决方案1】:

    按照缓存持久化示例here设置缓存:

    然后,将其作为自定义缓存传入boost配置中,如缓存配置部分here所示:

    例如:

    import { InMemoryCache } from 'apollo-cache-inmemory';
    import { persistCache } from 'apollo-cache-persist';
    
    const cache = new InMemoryCache({...});
    
    persistCache({
      cache,
      storage: window.localStorage,
    });
    
    import ApolloClient from "apollo-boost";
    
    const client = new ApolloClient({
      uri: "https://48p1r2roz4.sse.codesandbox.io",
      cache: cache
    });
    

    【讨论】:

      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 2019-02-20
      • 2021-10-29
      • 1970-01-01
      • 2021-04-06
      • 2017-11-28
      • 2014-08-16
      • 2020-10-23
      相关资源
      最近更新 更多