Apollo Client 3.0 Enhances Cache Management for GraphQL APIs

On Day 1 of the GraphQL Summit, being held from October 30-31 in San Francisco, Matt DeBergalis, Co-Founder, CTO at Apollo announced the release of the Apollo Client 3.0.

Apollo Client is a development tool that reduces the labor involved in creating client-side GUI applications that interact with server-side GraphL APIs. Apollo Client 3.0 attempts to address many of the pain points reported previously in Apollo Client 2.0. Chief among the issues addressed are is cache management, particularly around cache invalidation. (Cache invalidation is a process that deems cached data as invalid when it goes out of date or becomes out of sync with current data instances.)

As DeBergalist said in a post-presentation interview with ProgrammableWeb, “If you want to build a mobile app, for example, and you need to carefully track how much memory your cache is using so that you can persist it and have offline features, those are the types of things that Apollo Client 3.0 makes very easy.”

Past versions of Apollo Client split cache configuration functions across a variety of inconsistent APIs. To address the shortcoming, Apollo Client 3 introduces a consolidated type policies API that allows developers to configure the way that the cache interacts with individual types in the given GraphQL API schema.

Apollo Client 3 enhances cache invalidation by introducing InMemoryCache methods to garbage-collect cache entries that are no longer reachable. Also, the new version will evict a specific entry by its ID. These new features are implemented by the addition of new methods to the Apollo Client InMemoryCache object. These methods are InMemoryCache.gc() to execute garbage collection against the client-side cache and InMemoryCache.evict(id) to remove a specific entry from the client-side cache.

Apollo Client 3.0 has additional features that improve the client-side programming experience. There’s a new, reference-ID-based approach to cache storage in which only entities with a unique identifier (id)  are stored separately. Entities without a unique identifier are stored within the entity’s parent data structure. This change optimizes cache size and increases performance.

The InMemoryCache object also allows for lookup according to the GraphQL interface or union abstraction types should the entity be based on these. Finally, cache results are now immutable by default. Developers no longer need to use the freezeResults option as was required in previous versions.

As DeBergalis says, “We’re confident now that all the key patterns you need to build out a common app are going to be approachable to the typical team using Apollo Client 3.0.”

For more information about GraphQL Client go here.

Be sure to read the next GraphQL article: Hasura Brings Declarative Programming to GraphQL API Development