DEV Community

Robin Kretzschmar
Robin Kretzschmar

Posted on

Explain apollo graphql partialRefetch like I'm five

Apollo GraphQL Query hook has an option called partialRefetch and the docs state:

If true, perform a query refetch if the query result is marked as being partial, and the returned data is reset to an empty Object by the Apollo Client QueryManager (due to a cache miss). The default value is false for backwards-compatibility's sake, but should be changed to true for most use-cases.

Sadly I don't fully get it by reading the docs and searching online.

  • What is different for queries using this parameter?
  • When to use it?

Please go ahead and ELI5!

Top comments (3)

Collapse
 
stramel89 profile image
Michael Stramel

Hey, I was looking for this too. This was the best and pretty much only answer I could find.

When a Query component is mounted, and a mutation is executed that returns the same ID as the mounted Query, but has less fields in its result, Apollo Client's QueryManager returns the data as an empty Object since a hit can't be found in the cache. This can lead to application errors when the UI elements rendered by the original Query component are expecting certain data values to exist, and they're all of a sudden stripped away. The recommended way to handle this is to use the mutations update prop to reconcile the mutation result with the data in the cache, getting everything into the expected state. This can definitely be a cumbersome process however, so to help address this the partialRefetch prop can be used to automatically refetch the original query and update the cache.

Collapse
 
darksmile92 profile image
Robin Kretzschmar

Hey Michael, thanks for sharing, this makes it more understandable! :)

Collapse
 
stramel89 profile image
Michael Stramel

Glad I could help :)