<?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 Harshad Bankar on Medium]]></title>
        <description><![CDATA[Stories by Harshad Bankar on Medium]]></description>
        <link>https://medium.com/@harshadbankar?source=rss-286dc62565c9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9wzGun5HtgmKFMIL4YyjoQ.jpeg</url>
            <title>Stories by Harshad Bankar on Medium</title>
            <link>https://medium.com/@harshadbankar?source=rss-286dc62565c9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:30:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harshadbankar/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[Micro component with Angular and Apollo GraphQL]]></title>
            <link>https://medium.com/@harshadbankar/micro-component-with-angular-and-apollo-graphql-311566dd2890?source=rss-286dc62565c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/311566dd2890</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo-client]]></category>
            <dc:creator><![CDATA[Harshad Bankar]]></dc:creator>
            <pubDate>Tue, 31 Mar 2020 15:32:02 GMT</pubDate>
            <atom:updated>2020-03-31T15:32:02.130Z</atom:updated>
            <content:encoded><![CDATA[<p>I always start developer conversation with <strong>Why? </strong>especially when someone wants to discuss ideas with me. Due to my this habit of asking <strong>why</strong>? I got to know an amazing concept from <a href="https://medium.com/u/6f49dc4c27f9">Steven Chim</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N1YDpmr94mVvNp8rO8N6jw.png" /></figure><p>So this blog is based on his concept, which I regret I should have known beforehand.</p><p>It boils down to state management in frontend. But here you really don’t need to do anything than just querying data using Apollo GraphQL.(If you have worked with Angular and GraphQL, then it will be easy for you to understand the concept that I am going to talk about, otherwise learning new things never harms, keep reading…)</p><p><strong>When this concept is useful?</strong></p><p>If you have nested components in your application and each one of them relies on data from parent…child…child…</p><p>Imagine you have top parent, which is responsible for fetching data in one go and then passing it to child&#39;s using Angular @Input() or custom Angular services to store data. Thats means more code and it makes life hard to maintain that code.</p><p><strong>Less is more!</strong></p><p>With this concept you need to write less code which helps to maintain and write clean code, to achieve same functionality that you would normally write using @Input() or by writing custom Angular services.</p><p><strong>Here is how you can achieve it:</strong></p><p>Lets start by writing simple query which top parent normally will do:</p><pre>query user($id: ID!) {<br>  id<br>  firstName<br>  lastName<br>  address {<br>    id     <br>    houseNumber<br>    street<br>    postalCode<br>    country<br>  }<br>}</pre><p>Then in above case parent is responsible to provide data to child components, but using <strong>Fragment On Query, </strong>child component can query its own data from Apollo cache.</p><p>Let’s transform the same query into <strong>Fragment on Query</strong></p><p><strong>Now you can use query fragment UserAddressFragment in any component which needs address information.</strong></p><pre>query user($id: ID!) {<br>  id<br>  firstName<br>  lastName<br>  ...UserAddressFragment<br>}</pre><pre>fragment UserAddressFragment on Query {<br>  user(id: $id) {<br>    address {<br>     id     <br>     houseNumber<br>     street<br>     postalCode<br>     country<br>    }<br>  }<br>}</pre><p><em>(Beware, this is not Fragment on Type)</em></p><p><strong>How can component use this query fragment to get data from Apollo cache?</strong></p><p>Using Apollo <strong>readQuery() </strong>method</p><pre>const { user: { address } } = readQuery({<br>  query: gql`<br>    query user($id: ID!) {<br>      ...UserAddressFragment<br>    }`,<br>    variables: {<br>      id: id<br>   }<br>});</pre><p><strong>Example:</strong></p><p><a href="https://stackblitz.com/edit/basic-apollo-angular-app-zhdb3r?embed=1&amp;file=src/app/gql-query/address-fragment.ts">basic-apollo-angular-app-zhdb3r - StackBlitz</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=311566dd2890" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>