Apollo 不会将查询重新发送到服务器

Apollo is not re-sending the query to the server

我构建了一个带有 graphql 端点的 api 平台应用程序。在客户端,我像这样使用 apollo 客户端管理查询和突变(在 vuejs 前端):

import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'

const httpLink = new HttpLink({
  uri: 'http://localhost:8000/api/graphql'
})

export default new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache(),
  connectToDevTools: true
})

在我的 vuex 商店中,我是这样使用它的:

fetchItems({commit}, options) {
      commit(mutationsTypes.FETCHING_ITEMS);
      client.query({
        query: organismoQueries.SEARCH_ITEMS_QUERY(options),
      }).then((res) => {
        commit(mutationsTypes.FETCHING_ITEMS_SUCCESS, res.data.organismos.edges)
      }).catch(error => console.error(error));
    }

查询工作正常,问题是我无法重新加载 table 结果。当我使用相同的选项重新发送相同的查询时,永远不会请求 graphql 端点。我需要更改 table 的排序,但是一旦达到所有组合,我就无法再次重新加载 table。

我试图在查询中引入一个随机参数,但我从服务器收到一个错误(很明显)。

有没有办法重新发送相同的查询并执行类似重新加载 table.

的操作

提前致谢!

Apollo 客户端可以通过从服务器或缓存中获取查询来解析特定查询。确切的行为由 fetchPolicy 确定,可以在调用 client.query 时指定。来自文档:

The fetch policy is an option which allows you to specify how you want your component to interact with the Apollo data cache. By default your component will try to read from the cache first, and if the full data for your query is in the cache then Apollo simply returns the data from the cache. If the full data for your query is not in the cache then Apollo will execute your request using your network interface. By changing this option you can change this behavior.

因为默认策略是 cache-first,并且您已经提取了一次查询,所以对 client.query 的后续调用将 return 缓存的结果。要绕过缓存,请选择不同的策略,例如 network-only:

client.query({
  query: organismoQueries.SEARCH_ITEMS_QUERY(options),
  fetchPolicy: 'network-only',
})

您可以阅读有关 fetchPolicy 选项的更多信息 in the docs