在从 apollo-boost 导出的 ApolloClient 中禁用缓存

Disabling Cache in ApolloClient exported from apollo-boost

我想禁用缓存或将缓存限制为 24 小时。我的 ApolloClient 只在服务器端运行。

我的环境:

现在,这就是我配置 ApolloClient 的方式。

new ApolloClient({
      ssrMode: true,
      cache: new InMemoryCache(),
      link: WithApollo.BatchLink(),
      credentials: 'same-origin',
    });

The closest thing I saw in docs is FetchOptions ...但它没有指定我实际可以传递哪些选项来实现禁用或限制缓存的需要。

Apollo Boost 做不到这一点。您需要迁移到 migrate to using Apollo Client. This will allow you to provide a defaultOptions option to your ApolloClient constructor as shown in the docs:

const defaultOptions = {
  watchQuery: {
    fetchPolicy: 'no-cache',
  },
  query: {
    fetchPolicy: 'no-cache',
  },
}

fetchPolicy 选项实际上可以在每个单独的 query 调用或 Query 组件上设置——通过提供 defaultOptions 对象,您不必指定 no-cache 作为您使用的每个 Query 组件的获取策略。这也意味着,如果您决心保留 Boost,则可以在每个组件上都这样做。然而,以上是如何有效地"turn off" 对整个客户端进行缓存。

也许有人想知道如何禁用 apollo-boost ApolloClient 的缓存,所以我们来谈谈。

@Daniel 说的是实话,我们直接做apollo-boostnew ApolloClient是不能关闭缓存的,但是我们可以在发出请求的时候设置fetchPolicy。代码如下:

  // create client first
  import ApolloClient from "apollo-boost";
  const client = new ApolloClient({ uri: GRAPHQL_URL })

  // Set the fetchPolicy when we send request
  import { gql } from 'apollo-boost';
  client.query({
    query: gql`
      query someInfo($id: ID!) {
        info(id: $id) {
          id
          name
        }
      }`,
    variables:{id: '123'},
    fetchPolicy: 'no-cache'
  })

您可以从 there.

中找到 fetchPolicy 的有效值