2

I'm trying to prevent re-fetch of previously cached data. But the documentation provides a couple of ways of achieving this through cacheRedirects and dataIdFromObject. I'm trying to understand when one technique is used over the other.

He's an example flow using dataIdFromObject -- would this provide enough context for Apollo to fetch the detail view data from cache, or do I additionally need a cacheRedirect to link the uuid query?

List view query:

query ListView {
   books {
     uuid
     title
     abstract
   }
}

Detail view query:

query DetailView {
   book(uuid: $uuid) {
     uuid
     title
     abstract
   }
}

cache constructor args with dataIdFromObject:

new InMemoryCache({
   dataIdFromObject: object => {
     switch (object.__typename) {
       case 'book': return `book:${object.uuid}`;
       default: return defaultDataIdFromObject(object); // default handling
     }
   }
});
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
tgk
  • 3,857
  • 2
  • 27
  • 42

1 Answers1

3

I believe you are incorrect when you say

But the documentation provides a couple of ways of achieving this through cacheRedirects and dataIdFromObject.

I believe only cacheRedirects achieve what you want.

dataIdFromObject allows you to customize how ApolloClient should uniquely identify your objects. By default, ApolloClient assumes your objects have either a id or _id property, and it combines the object __typename with the id property to create a unique identifier.

By providing a dataIdFromObject function, you can customize this unique identifier. For example, if all of you objects have an id which is a uuid, then you could supply a dataIdFromObject function which simply instructs ApolloClient to use the object's id property, without appending __typename.

John
  • 9,249
  • 5
  • 44
  • 76