Apollo client - local field with computed value from object

Viewed 459

In Apollo client 3 I want to create local field based on object values (computed field).

How I can reference object fields in apollo local fields

I am imaging something like this:

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


query OrganizationList {
   organizationList {
      name
      url @client
   }
}

Result

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

Found answer, all you need to do is pass readField

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