Apollo 客户端 - 具有来自对象的计算值的本地字段

Apollo client - local field with computed value from object

在 Apollo 客户端 3 中,我想根据对象值(计算字段)创建本地字段。

如何在 apollo 本地字段中引用对象字段

我正在想象这样的事情:

new InMemoryCache({
typePolicies: {
  OrganizationType: {
    fields: { 
      url: { 
        read(object) { 
          return `/organization/${object.name}/`
        }
      }
    }
  }
}


query OrganizationList {
   organizationList {
      name
      url @client
   }
}

结果

[{
   "__typename": "OrganizationType",
   "name": "google",
   "url": "/organization/google/"
},
{
   "__typename": "OrganizationType",
   "name": "apple",
   "url": "/organization/apple/"
}]

已找到答案,您只需传递 readField

new InMemoryCache({
typePolicies: {
  OrganizationType: {
    fields: { 
      url: { 
        read(_,{readField}) { 
          return `/organization/${readField('name')}/`
        }
      }
    }
  }
}