<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harshmadhogaria on Medium]]></title>
        <description><![CDATA[Stories by Harshmadhogaria on Medium]]></description>
        <link>https://medium.com/@harshmadhogaria15?source=rss-ca178392c64------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*R-Fn0zbZzVjXURdzcvf5-w.jpeg</url>
            <title>Stories by Harshmadhogaria on Medium</title>
            <link>https://medium.com/@harshmadhogaria15?source=rss-ca178392c64------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:05:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harshmadhogaria15/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Graphql query returning an unexpected data — Is it an Apollo cache issue?]]></title>
            <link>https://harshmadhogaria15.medium.com/graphql-query-returning-an-unexpected-data-is-it-an-apollo-cache-issue-99f2ad18456?source=rss-ca178392c64------2</link>
            <guid isPermaLink="false">https://medium.com/p/99f2ad18456</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo-cache]]></category>
            <dc:creator><![CDATA[Harshmadhogaria]]></dc:creator>
            <pubDate>Thu, 18 Mar 2021 13:59:12 GMT</pubDate>
            <atom:updated>2021-03-18T13:59:12.146Z</atom:updated>
            <content:encoded><![CDATA[<h3>Graphql query returning an unexpected data — Is it an Apollo cache issue?</h3><p>Most of us have not spent a lot of time understanding how the Apollo’s caching mechanism really works and this is because how seamlessly it handles caching the Graphql object that is received as part of the API response.</p><p>Let me start with some details on how the Apollo caching works and how it stores the data in the cache and then adding how it lead to an unexpected response in our case. Apollo uses In-memory cache to store response object from a graphql query in normalized form.</p><p>Let us assume we are getting an object as a response from a Graphql API call which looks like this —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r7KNTigCu6DrM5IffmkHZA.png" /><figcaption>Graphql response object from API</figcaption></figure><p>Now for this response the Apollo client will create an in-memory cache something like this —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPJ0q8Cj7yNqvsHW1OUE4g.png" /><figcaption>Apollo cache for the above response</figcaption></figure><p>Apollo creates a flat normalized structure to store the results from the API and every time the response object has an <strong>id</strong> and <strong>__typeName</strong> it creates a new cache key by combining them to cache the object. Next time if we try to hit the same query it will access the data from cache and will return the result instead of making a network call (This totally depends on the fetch policy of your network call, if your system requires a strictly accurate result, you can configure it to make network-only API calls).</p><p>Now let me share a more specific use case for which we were using Apollo cache. This is how the query and API response looked like —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMedpz0bU_oL7GSE77lqDA.png" /></figure><p>Now as you can guess Apollo will create the cache entry something like this—</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*eJzrU-Fa-L7CAxe5P3hT7w.png" /></figure><p>Now lets say I make another query call, but this time with a different entity in the template filter —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vh3Fy40lFrmW7zj5JeFkyQ.png" /></figure><p>In this case the template details are different and also the values in condition and action. The ids for the condition and action are same as the first API call where the entity was <strong>Invoice</strong>. So lets look at how Apollo will create a cache entry for this request —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*MnC3evsiyBYbvyCrBDqzmA.png" /></figure><p>Again we have Apollo cache creating keys <strong>Template_Condition:8</strong> and <strong>Template_Action:4</strong>, it overrides the values that were cached by the first API call.</p><p>Let’s consider a case where we again make a network call similar to the first one and pass the entity as <strong>Invoice</strong>, Apollo client already has the value for this request in the cache so it doesn’t make a network call to the backend service, instead it starts forming the response object from cache. So for the condition when it tries to search for the key <strong>Template_Condition:8</strong> it gets the value as “<strong>Bill is Unpaid</strong>” instead of “<strong>Amount &gt; 0</strong>”, the value was overwritten by the second network call and a similar case happens for the action. We end up getting an incorrect response.</p><p>At first this might look like a bug in Apollo but its not, Apollo considers an API response object to be idempotent, which means it expects an API to return the same object for a given id but in this case we have different values for the same ids.</p><p>Is there a problem with how the API or the graphql schema is designed? — Most of the time yes, we can design the schema such that this problem can be avoided. There can be a situation where you are working on building something big and generic which will require you to have this kind of schema like we had in our case. Can we avoid this issue to happen on the frontend side? Definitely yes and there can be multiple approaches to this:-</p><ol><li>Change the fetch-policy to network-only: This is the most straightforward solution, but we end up losing the benefits of Apollo cache and end up making network calls every time.</li><li>We can skip the __typename field which is used by Apollo to cache the object: This is the method that we are using in our project. If your Apollo client automatically adds __typename you can explicitly add skip command to the fields where the results are not idempotent. Here is an example for the above query —</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kvzex8_Gw1kD05fbrK10TQ.png" /></figure><p>With this a different key is not created for condition and action as the __typename doesn&#39;t exist thus avoiding the unexpected result. There could be other ways of solving this by aliasing the id which again leads to Apollo client not creating a separate key for action and condition.</p><p>It took us a while to figure this out but it helped us understand few internals of how the Apollo caching works. With graphql and Apollo client it sometime seems complex but as you understand it better it starts making more sense.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99f2ad18456" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>