【问题标题】:Is there a way to check if Apollo has data cached before trying to call the cache?有没有办法在尝试调用缓存之前检查 Apollo 是否缓存了数据?
【发布时间】:2020-08-05 21:33:24
【问题描述】:

在尝试从缓存中调用该数据的查询之前,我需要确定 Apollo 是否缓存了数据。

如果我们尚未调用 USER_QUERY,此代码将引发 "Invariant Violation: Can't find field ...... on object" 错误。但是我没有办法检查我们是否曾经调用过它。

const cachedData = client.readQuery({
   query: USER_QUERY,
   variables: queryVariables,
});

我的问题是我不知道如何在调用任何缓存之前检查我们是否缓存了数据。

【问题讨论】:

    标签: graphql apollo react-apollo apollo-client


    【解决方案1】:

    您可以按照建议 herehereclient.readQuery 部分包装为 try catch ;)

    try {
      const cachedData = client.readQuery({
         query: USER_QUERY,
         variables: queryVariables,
      });
    } catch(e) {
      // maybe you can safely swallow the error
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2011-08-27
      • 2021-06-21
      • 2019-12-29
      • 2020-06-06
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多