【问题标题】:With GraphQL Apollo Client, How To set @client fields to store in Cache object?使用 GraphQL Apollo 客户端,如何设置 @client 字段以存储在缓存对象中?
【发布时间】:2021-02-05 13:31:20
【问题描述】:

我有一个 Apollo 查询集,如下所示。

query { 
  speakers {
    id
    name
    checkBoxField @client
  }
}

我的字段策略设置如下:

export const genericBoolean = cache.makeVar(false);

typePolicies: {
   Speaker: {
      fields: {
        checkBoxField: {
           read(checkBoxField = false) {
             return genericBoolean === true ? "true" : "false";
           ...

我的页面上有一个按钮,我使用 useQuery 定义并且该按钮执行代码:

onClick : genericBoolean(true)

当我转储 (JSON.stringify) 从 useQuery 返回的数据时,checkBoxField 会按预期对所有行更改为 true,但在我的 Apollo 缓存中(从 chrome 扩展中可以看出),没有为 checkBoxField 存储字段。我想要那个,这样我就可以遍历我的缓存并为每一行获取值。

【问题讨论】:

    标签: graphql apollo react-apollo


    【解决方案1】:

    根据 Apollo 文档,您可以使用反应变量或 Apollo 客户端缓存来存储本地状态:https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#storing

    你上面的例子是使用反应变量。要将数据存储在缓存中,您需要使用 writeQuerywriteFragmenthttps://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#storing-local-state-in-the-cache

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 2020-08-07
      • 2018-04-17
      • 2021-06-10
      • 2017-03-18
      • 2019-01-10
      • 2021-02-01
      • 2019-11-12
      • 2019-07-29
      相关资源
      最近更新 更多