Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo client - local field with computed value from object

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/"
}]
like image 800
SSS Avatar asked Oct 31 '25 22:10

SSS


1 Answers

Found answer, all you need to do is pass readField

new InMemoryCache({
typePolicies: {
  OrganizationType: {
    fields: { 
      url: { 
        read(_,{readField}) { 
          return `/organization/${readField('name')}/`
        }
      }
    }
  }
}
like image 158
SSS Avatar answered Nov 03 '25 13:11

SSS