<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rohini Senthil</title>
    <description>The latest articles on DEV Community by Rohini Senthil (@rohinivsenthil).</description>
    <link>https://dev.to/rohinivsenthil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F481293%2F21749fec-7db8-438b-a2f1-9a176cad9a9e.jpg</url>
      <title>DEV Community: Rohini Senthil</title>
      <link>https://dev.to/rohinivsenthil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohinivsenthil"/>
    <language>en</language>
    <item>
      <title>GraphQL Unions – Customize your results!</title>
      <dc:creator>Rohini Senthil</dc:creator>
      <pubDate>Sun, 09 May 2021 20:43:19 +0000</pubDate>
      <link>https://dev.to/rohinivsenthil/graphql-unions-customize-your-results-54h5</link>
      <guid>https://dev.to/rohinivsenthil/graphql-unions-customize-your-results-54h5</guid>
      <description>&lt;p&gt;What's not to love about &lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;GraphQL&lt;/a&gt;? It's got your back when it comes to returning predictable results - the data you get is exactly what you need, nothing more and nothing less.&lt;/p&gt;

&lt;p&gt;If you’ve worked with GraphQL, then you’re probably familiar with query and mutation types — so what exactly is a GraphQL union? 🤔&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unions are abstract GraphQL types that enable a schema field to return one of multiple object types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not really sure what that means? Don't worry! I got you. In this tutorial, I will be walking you through the concept of GraphQL &lt;strong&gt;unions&lt;/strong&gt; by addressing the two important questions below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why do I need GraphQL unions?&lt;/li&gt;
&lt;li&gt;How do I implement GraphQL unions?
&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Why do I need GraphQL unions?
&lt;/h2&gt;

&lt;p&gt;Let's consider a simple scenario: You own a small candy shop and the website is using GraphQL APIs. Each candy is associated with a unique identifier, it's name and price.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmsk0cw8h6an3lpdm5x06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmsk0cw8h6an3lpdm5x06.png" alt="Object type for candy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's have a basic query in place to retrieve the details of the candy when passed the unique identifier. Here's how that's gonna look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkk12ipgcxylzbjbaqzwy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkk12ipgcxylzbjbaqzwy.png" alt="GraphQL query for candy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Building on this scenario, let's assume that it's also possible for a queried candy to either be out of stock or unavailable for shipping in the region. We can visualize this as three three states of a candy:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmtyeyzo8wo5sholzzih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmtyeyzo8wo5sholzzih.png" alt="States of a candy – In Stock, Out of Stock and Region Unavailability"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will consider all three states as valid responses and hence, instead of throwing an error when a candy is out of stock or unavailable for shipping, we provide the user with additional information like when a candy would be restocked if out of stock and an array of regions where it is available if unavailable for shipping.&lt;/p&gt;

&lt;p&gt;Now, if we add all these new fields on our existing type: Candy, then we would have to over-fetch data to determine the state of a candy. And if we create separate queries for the different states, we would be under-fetching data.&lt;/p&gt;

&lt;p&gt;In essence, what we're itching for is a way to somehow define 'out of stock' and 'region unavailability' as separate types (say,  &lt;code&gt;OutOfStock&lt;/code&gt; and &lt;code&gt;RegionUnavailability&lt;/code&gt; ) along with the existing type: &lt;code&gt;Candy&lt;/code&gt; under the same query to ensure we're only getting the data we need.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;How do we do this? That's right, this is where GraphQL unions steps in. We model the identified "states" as separate types and use unions to enable a query to return one of the multiple types defined.&lt;/p&gt;



&lt;h2&gt;
  
  
  How do I implement GraphQL unions?
&lt;/h2&gt;

&lt;p&gt;Now that we have our states as object types. Let's understand unions: &lt;strong&gt;Unions&lt;/strong&gt; are abstract schema types. To define a union type, we declare which object types are included in the union.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;union&lt;/span&gt; &lt;span class="nx"&gt;CandyResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Candy&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;OutOfStock&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;RegionUnavailability&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;It's important to understand here that all of a union's included types &lt;em&gt;must&lt;/em&gt; be object types (not scalars, input types, etc.) and that the included types do &lt;em&gt;not&lt;/em&gt; need to share any fields.&lt;/p&gt;
&lt;h4&gt;
  
  
  Resolving a GraphQL union (server-side):
&lt;/h4&gt;

&lt;p&gt;After adding the union type to our schema file, it's going to be looking something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;To resolve a union, we are required to specify which of the union’s type is being returned. To achieve this, we define a &lt;code&gt;__resolveType&lt;/code&gt; function for the union.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;__resolveType&lt;/code&gt; function uses a returned object's unique field to determine its type. It then returns the name of that type as a string.&lt;/p&gt;

&lt;p&gt;Here’s how we would use the &lt;code&gt;__resolveType&lt;/code&gt; function for the CandyResult union we’ve defined in the schema:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Querying for a GraphQL union (client-side):
&lt;/h4&gt;

&lt;p&gt;When the return type of a query is a union, the GraphQL client doesn’t know which object type a field will return. To account for this, a query can include the subfields of multiple possible types.&lt;/p&gt;

&lt;p&gt;Here’s how our query going to be looking:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;candy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Candy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt;
      &lt;span class="nx"&gt;price&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;OutOfStock&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;restockDate&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;RegionUnavailability&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;availableRegions&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s add a candy to our data dump that’s out of stock with a &lt;code&gt;restockDate&lt;/code&gt; key. When we use the query above to retrieve data for the candy, we can now expect to see the results tailored to exactly what we need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fslhglrfrzsrnnkg85vfz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fslhglrfrzsrnnkg85vfz.png" alt="Out of Stock Query"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s it! — we’ve made a query that supports multiple object types with the help of unions.&lt;/p&gt;

&lt;p&gt;You can find the entire code as a Github repository &lt;a href="https://github.com/rohinivsenthil/graphql-unions" rel="noopener noreferrer"&gt;here&lt;/a&gt; 🚀&lt;/p&gt;

&lt;p&gt;Share if you found this useful 😄&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>unions</category>
      <category>apollo</category>
    </item>
  </channel>
</rss>
