【问题标题】:Apollo-Client field policy: using readField handle for fields with argumentsApollo-Client 字段策略:对带参数的字段使用 readField 句柄
【发布时间】:2022-04-06 06:21:48
【问题描述】:

在 Apollo-Client 的缓存中定义字段 read 策略时,有人知道是否可以使用 readField 句柄来读取带参数的字段值?

例如,我的缓存中有以下对象:

parent {
   id
   child({"arg1"="A"})
   child({"arg1"="B"})
}

我需要在parentchild 字段上设置读取策略,以在将第二个参数(例如arg2)传递给该字段时处理这些情况。换句话说,我需要根据child({"arg1"="A"})(存在于缓存中)的值来读取child({"arg1"="A","arg2"=true})(不存在于缓存中)。例如:

read(cached, { readField, args }){
   if(!args?.arg2) return cached;
   const cachedWithoutArg2 = ???
   return doSomething(cachedWithoutArg2);
}

有什么办法吗?

【问题讨论】:

    标签: apollo-client


    【解决方案1】:

    聚会有点晚了,但您可以使用 readField 的扩展版本将参数传递给特定读取 -> readField({ fieldName: 'child', args: { arg1 })

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2021-02-01
      • 2021-10-16
      • 2020-08-24
      • 2021-09-01
      • 2021-11-21
      • 1970-01-01
      • 2011-09-30
      • 2015-05-22
      相关资源
      最近更新 更多