首页 > 解决方案 > Apollo-Client 字段策略:对带参数的字段使用 readField 句柄

问题描述

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

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

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

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

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

有什么办法吗?

标签: apollo-client

解决方案


推荐阅读