Skip to content
Home » Blog » Closer Examination of Apollo GraphQL Caching

Closer Examination of Apollo GraphQL Caching

  • by

One of essentially the most thrilling (and every so often, irritating) components of frontend improvement is the ever-changing customary of fashionable practices, frameworks, and instruments. In this case, GraphQL was the factor looming over the horizon—a brand new framework that was changing into broadly adopted within the internet improvement world. Given its recognition, we had been excited to include it into our utility.

There are many articles and sources that element the benefits of adopting GraphQL. Here at Iterable, we had been enthusiastic about with the ability to specify the data a web page wanted and fetch this as a singular request, slightly than having to make a hodgepodge of varied REST API calls. 

To ship and obtain responses from GraphQL queries on our React frontend, we use Apollo Client. As talked about within the documentation, one of the benefits of Apollo Client is its declarative method to knowledge fetching. 

“Apollo Client takes care of the request cycle from start to finish, including tracking loading and error states for you. There’s no middleware to set up or boilerplate to write before making your first request, nor do you need to worry about transforming and caching the response. All you have to do is describe the data your component needs and let Apollo Client do the heavy lifting.”

Iterable’s Use Case

The web page on this specific use case comprises a paginated desk displaying an inventory of gadgets. Users may also place gadgets in folders, so the desk row contents are both folders or gadgets.

To fetch the information for the desk, right here’s a simplified model of the question we’re utilizing:

The question takes in some web page data info, resembling folder vacation spot ID and most quantity of gadgets. Our backend implementation makes use of these variables within the GraphQL question to then return the specified knowledge we wish to show within the desk.

Then, to entry this returned knowledge, merely use the “useQuery” hook within the web page element. It seems one thing like this:

“folderQueryData” is then handed to and displayed within the desk element. So far, so good.

Problems started to come up nonetheless after we began utilizing the question on different components of the web page, albeit with completely different variables handed in. The first use of the question is for the desk, as defined above, and the second is a modal pop-up. In these two locations, calls had been made to fetch an inventory of gadgets through the “fetchItemsQuery” name, however with completely different info handed into the question variables since we needed the desk and modal to show completely different portions of gadgets.

For occasion, the “info” parameter for the question comprised of the desk element would include a restrict of 10 if we would like the desk to solely show 10 gadgets at a time:

While the “info” parameter for the question made for the modal would include a special quantity for the restrict:

After implementing the modal performance, we found a flickering concern when the modal can be open.

Examination of the community tab revealed that the queries for the modal and element had been always being fired and overwriting one another within the cache, resulting in infinite graphql calls being made and conflicting show info being handed into the presentational elements.

Why Did This Happen?

According to the documentation, Apollo caching is decided by the question response. The cache generates a cache ID for every identifiable object within the response knowledge by concatenating the article’s typename and ID fields. Since the touchdown web page of the desk is on the root folder (with a particular root ID resembling “0”) and the modal for folder creation additionally queries on the root folder degree, these question outcomes had been recognized by the cache as the identical—each with a typename of “folder” and an ID of “0.”

As a end result, within the cache, the incoming response was in comparison with the present one within the cache. Since the cache ID’s had been the identical, this may override the earlier name’s response.

From Apollo

Whenever an incoming object has the identical cache ID as an current cached object, the fields of these objects are merged:
• If the incoming object and the present object share any fields, the incoming object overwrites the cached values for these fields.

This precisely displays the flickering concern we had been seeing. The incoming response would overwrite (not be concatenated with) one another since that they had the identical cache object ID, and these adjustments would in flip trigger the web page to repeatedly re-render its show for the reason that info it was receiving from the cache would change.


There are a number of potential options to this concern. The easiest, which we applied, was including a “no-cache” coverage to at least one of the queries.

This coverage allowed the question to go on to the backend to seize the obtainable knowledge with out studying or writing it to the cache, and subsequently having no impact on the opposite question (which had the default caching coverage.) In our use case, this was an ideal one-liner answer since we have now a recognized most quantity of outcomes allowed for the question on the web page.

Disabling the cache was an appropriate answer for us as a consequence of an imposed ceiling on the information measurement and rare calling of the question. If the information fetched was bigger or extra performance-intensive, right here’s another approaches we’ve discovered that would work: 

Leave a Reply

Your email address will not be published. Required fields are marked *