<?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 Agnish Mukherjee on Medium]]></title>
        <description><![CDATA[Stories by Agnish Mukherjee on Medium]]></description>
        <link>https://medium.com/@nishnoob?source=rss-2bf262989dd8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*lxWBW7sYDHwcbWR-.</url>
            <title>Stories by Agnish Mukherjee on Medium</title>
            <link>https://medium.com/@nishnoob?source=rss-2bf262989dd8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:16:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nishnoob/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[Why Apollo GraphQL’s writeQuery is necessary?]]></title>
            <link>https://medium.com/building-aasaanjobs/why-apollo-graphqls-writequery-is-necessary-cd46cecd2757?source=rss-2bf262989dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd46cecd2757</guid>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[immutability]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Agnish Mukherjee]]></dc:creator>
            <pubDate>Mon, 18 Mar 2019 06:42:03 GMT</pubDate>
            <atom:updated>2019-03-18T06:42:03.845Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PIXgmURZKvkuYRv9IcEi5A.png" /></figure><p>All of you for once, must have found yourself in a clueless situation having spent couple of hours locating a problem in the source code which happens to be the reason behind an undesired behaviour in the application. Well, at least that’s how I found myself in the avenue of finding out why the function <em>writeQuery()</em> is a necessary step once we manually update the apollo cache in react, which was completely unrelated to the problem we were trying to diagnose earlier!</p><p>In our current project architecture, we’ve opted to go with Apollo GraphQL as the bridge between the backend and the frontend (React Native). Apollo is equipped with a cache which stores the result of the queries made by the user. Now, once a mutation is performed, Apollo’s cache isn’t updated in real time. For the cache to be updated with the latest version of data either the user has to reload the page or we can use the <em>update</em> callback provided by the Apollo’s mutation api which executes after a successful mutation.</p><h3><strong>The Wrong Way</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Wq2drZZ01P2HiqGj" /><figcaption>Source Code 1</figcaption></figure><p>Here we’ve picked up a code block from our active mobile web app, to have a better understanding at how things work. What we have here, is an <em>onSubmit()</em> function which makes a mutation call with Skill IDs as the payload using the <em>graphQLMutationHelper()</em>. After this mutation succeeds, we need to update our local store with the <em>update</em> API. In the update function in ‘Source Code 1’, we’re obtaining the query from cache and updating it with the new data returned from the successful mutation.</p><p>The Apollo cache gets updated by making changes to the <em>me</em> variable which stores the reference of the <em>USER_DETAILS</em> query from the cache. By now we can see the updated data being reflected in all the places and tag this job as done. But is it so? Let’s have a look at the Apollo cache.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/0*lABDb2MMEuBJ5dHC" /><figcaption>Fig. 1</figcaption></figure><p>The contents of the Apollo cache can be read using the <a href="https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en">React Developer Tools chrome extension</a>. With the React DevTools tab open, we select the component wrapped with the <em>withApollo</em> HOC and in the Props section (Fig. 1) we see <em>apolloState</em>. This prop contains the results of all the successful queries made in the current session. In here the whole store is divided into chunks which makes it a bit of a task to find the chunk which holds the Skills (named <em>skill_ratings</em>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*iggJ5kBcTQV4bHMo4chCVA.jpeg" /><figcaption>Fig. 2</figcaption></figure><p>Let’s take up the first scenario, i.e., updating the cache using the reference to the data. When we look at the store, we see the user with no Skills initially (Fig. 2). Now let’s add a couple of skills to the user and run the onSubmit function (Source Code 1) we saw earlier. As per our understanding the Apollo cache should have these new skills added, but it’s contrary to our belief! What just happened here?</p><p>So, all of you must be sitting there scratching your heads to what just happened. This behaviour of the Apollo cache is quite understandable as we just updated the cache through reference but never alerted the Apollo Client about this update. Although, the data is updated the client doesn’t know about it, it doesn’t get to update the props that track the cache and neither does it update the elements that consume the store about the updates. Also, turns out if you refresh the page you get to see the result you expected to (Fig. 3) because we refreshed the store, by refetching the queries from the database.</p><h3><strong>The Immutability Principle</strong></h3><p>Before we get further, a little knowledge about the concept of immutability would only do good. Everyone in their initial days of working with React would remember one of the first things they had learned that objects shouldn’t be mutated. Ever wondered why?</p><p>In Javascript, all values except primitive values, are ‘passed by reference’. This means if you provide a complex javascript object as an argument to a function, only its ‘reference’ in the memory is passed on. When you simply change contents for the provided argument, the reference stays intact. React’s diffing algorithm assumes if a reference has not changed, the object (as well as its members) itself should not have changed. All react libraries such as Apollo, adhere to this immutability principle. Thus, in Source Code 1, any change made to the <em>userDetails</em> object, will not trigger a diff evaluation on the cache object.</p><h3><strong>The Right Way</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2leC3ABiJnaDw9dN" /><figcaption>Source Code 2</figcaption></figure><p>Use the Apollo’s <em>writeQuery</em> API. Simple. Here we use an updated onSubmit function (Source Code 2) which uses the <em>writeQuery</em> API to alert the Apollo Client about the updated query. We modify our orignal <em>update</em> method to call <em>writeQuery</em> passing on the updated cache object. This makes the Apollo Client update it’s records about the local store and in turn, alerts the consuming elements about the updates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*tfmoBqZmaTf8VRrgPtgcuA.jpeg" /><figcaption>Fig. 3</figcaption></figure><p>Now the next time you’re wondering why your components aren’t getting refreshed with the updated data, evaluating your object mutability and inspecting your use of <em>writeQuery()</em> would be a good place to start debugging.</p><p><em>Thank you for reading till the end. This happens to be one of the plethora of issues we encounter working with React at AasaanJobs and we believe in spreading the word on how to recover from it gracefully and take home something new at the end of the long day.</em></p><p><em>You’ve got something similar? We encourage you to share!</em></p><p><em>Like what we do? We are </em><a href="/service/mailto:apply@aasaanjobs.com"><em>hiring</em></a><em>!</em></p><h3>Resources:</h3><ul><li><a href="https://www.apollographql.com/docs/react/api/apollo-client.html#ApolloClient.writeQuery">writeQuery Documentation</a></li><li><a href="https://www.apollographql.com/docs/react/features/caching.html#writequery-and-writefragment">How to use writeQuery</a></li><li><a href="https://reactjs.org/docs/update.html#the-main-idea">Immutability — React Docs</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd46cecd2757" width="1" height="1" alt=""><hr><p><a href="https://medium.com/building-aasaanjobs/why-apollo-graphqls-writequery-is-necessary-cd46cecd2757">Why Apollo GraphQL’s writeQuery is necessary?</a> was originally published in <a href="https://medium.com/building-aasaanjobs">Building Aasaanjobs (An Olx Group Company)</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>