<?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 Marcelo Nalon on Medium]]></title>
        <description><![CDATA[Stories by Marcelo Nalon on Medium]]></description>
        <link>https://medium.com/@marcelo.nalon_37050?source=rss-1c364ff46e37------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3jnWc52qAewznlbj.jpg</url>
            <title>Stories by Marcelo Nalon on Medium</title>
            <link>https://medium.com/@marcelo.nalon_37050?source=rss-1c364ff46e37------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 10:41:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcelo.nalon_37050/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[Connect to Redis on different operation modes with Redis CLI]]></title>
            <link>https://medium.com/@marcelo.nalon_37050/connect-to-redis-on-different-operation-modes-with-redis-cli-d2e84fbd1d09?source=rss-1c364ff46e37------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2e84fbd1d09</guid>
            <category><![CDATA[redis-cluster]]></category>
            <category><![CDATA[redis-sentinel]]></category>
            <category><![CDATA[redis-cli]]></category>
            <category><![CDATA[redis]]></category>
            <dc:creator><![CDATA[Marcelo Nalon]]></dc:creator>
            <pubDate>Tue, 28 Feb 2023 01:45:27 GMT</pubDate>
            <atom:updated>2023-02-28T01:59:03.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J4kd7LFBOzjCDtba2QTf2g.gif" /></figure><p>If you have ever used <a href="https://redis.io/">Redis</a> as a component of any project you have worked on, you likely had to interact with it to verify how data was being stored. Depending on the setup used to scale or increase availability, the steps to interact with the correct Redis instance may vary.</p><p>This post is a quick reference on how to use Redis CLI to connect to a Redis instance that really holds your stored keys depending on the operation mode (Single Instance, Cluster, or Sentinel).</p><h3>Redis CLI</h3><p><a href="https://redis.io/docs/ui/cli/">Redis CLI</a> is a command-line utility that can be used to send commands and read replies from a Redis server. It is available as part of <a href="https://redis.io/docs/getting-started/installation/">Redis installation packages</a>. Depending on your OS and package manager it can be installed as a standalone tool (<strong>e.g.</strong> <em>sudo apt install redis-tools</em>).</p><blockquote>The Redis command line interface (redis-cli) has two main modes: an interactive Read Eval Print Loop (REPL) mode where the user types Redis commands and receives replies, and a command mode where redis-cli is executed with additional arguments and the reply is printed to the standard output.</blockquote><p><strong><em>Note</em></strong><em>: </em>All the following instructions were tested in a macOS Monterey with redis-cli 7.0.4.</p><h3>Single Instance</h3><p>This is the simplest operation mode where you have only one instance of Redis running in a single host. To connect to this instance, you only need to know the following info:</p><p><strong>&lt;host&gt;</strong>: The <em>host IP</em> where Redis is running<em> </em>(127.0.0.1 if you are running it locally on your machine)</p><p><strong>&lt;port&gt;</strong>: <em>port</em> where Redis is listening for connections (6379 by default)</p><p><strong>&lt;credential&gt;</strong>: <em>credential to authenticate </em>(it may vary depending on which authentication mode is configured. We will be using the password authentication method)</p><p>To start the interactive mode using Redis CLI type:</p><pre>redis-cli -h &lt;host&gt; -p &lt;port&gt; -a &quot;&lt;password&gt;&quot;</pre><p>or if you are running a Redis server locally with no authentication just type:</p><pre>redis-cli</pre><p>After that, you can use any <a href="https://redis.io/commands/">command</a> to interact with your Redis instance.</p><p>To use the command mode, the arguments to establish the connection are the same but additionally, you need to provide the desired command altogether. For example, to use the extremely useful command <a href="https://redis.io/commands/monitor/">MONITOR</a>, which logs every command Redis is processing, you can type:</p><pre>redis-cli -h &lt;host&gt; -p &lt;port&gt; -a &quot;&lt;password&gt;&quot; MONITOR</pre><h3>Cluster</h3><p><a href="https://redis.io/docs/management/scaling/">Cluster</a> mode is a special Redis deployment topology that aims to provide horizontal scalability. In this mode, data is split across multiple Redis instances (nodes). Then depending on which portion of your data set you want to interact with, you will need to know the exact host is holding it.</p><p>Considering you know the host IPs of the nodes, you can connect with each of them as you do to interact with a single instance. But in doing this you would need to verify if you can perform a desired command (<strong>e.g.</strong> get a desired key or set one) on each connected node. To avoid this painful task you can instead connect with any of the nodes using Redis CLI in cluster mode (-c parameter):</p><pre>redis-cli -h &lt;host&gt; -p &lt;port&gt; -a &quot;&lt;password&gt;&quot; -c</pre><p>Since in the Redis Cluster mode nodes can redirect the client to the correct node, this option instructs the Redis CLI to follow this redirection and connect you to the node that really can execute the command you are trying to.</p><p>Let&#39;s see an example. Consider you have the following URI pattern to describe your Redis Cluster:</p><pre>cluster://:&lt;password&gt;@&lt;host1&gt;:&lt;port1&gt;,&lt;host2&gt;:&lt;port2&gt;,...,&lt;hostN&gt;:&lt;portN&gt;/0</pre><p>For example:</p><pre>cluster://:myP4ssw0rd@redis1.com:6379,redis2.com:6379,redis3.com:6379,redis4.com:6379,redis5.com:6379,redis6.com:6379/0</pre><p>To start the interactive mode on Redis CLI you can choose any host available on the URI. For example:</p><pre>redis-cli -h redis3.com -p 6379 -a &quot;myP4ssw0rd&quot; -c</pre><p>It allows you to interact with any part of your dataset without worrying about whether you are on the correct node. Unfortunately, this is only true while considering operations through a single key. Commands to interact with a bunch of keys at the same time (<strong>e.g </strong><em>keys *</em>) will only consider the data available in the connected node. There are some ways to perform operations that deal with more than one key considering all data set split through the cluster. For example, to list all keys, you can use the command mode, also pointing to any node host (avoid this in production!):</p><pre>redis-cli -a &quot;myP4ssw0rd&quot; --cluster call redis5.com:6379 KEYS &quot;*&quot;</pre><p>Or to count the total keys:</p><pre>redis-cli -a &quot;myP4ssw0rd&quot; --cluster call redis1.com:6379 KEYS &quot;*&quot; | wc -l</pre><h3>Sentinel</h3><p>Sentinel is an operation mode used to provide high availability for Redis. It is composed of processes called sentinels, that run on different hosts and operate together to keep the Redis master instance (instance that must be used by the clients) available and healthy. They do this by monitoring the Redis instances (master and replicas) and adopting failover strategies to elect a new master (promoting a replica) in case the current one is not working as expected.</p><p>Then considering the sentinels are the ones who know which Redis instance is the master, we need to ask them about it. We do this using Redis CLI to connect with any of the available sentinels (supposing you know their hosts) in the same way we connect to a single Redis instance:</p><pre>redis-cli -h &lt;sentinel_host&gt; -p &lt;sentinel_port&gt; -a &quot;&lt;password&gt;&quot;</pre><p>Once connected with a sentinel, we can type the following command to get the current master info:</p><pre>&gt; SENTINEL get-master-addr-by-name &lt;service-name&gt;</pre><p>where <strong>&lt;service_name&gt;</strong> is obtained during the sentinel configuration. This command will receive an IP/port pair as a response. We use this info to connect to the master instance.</p><p>Let’s see an example. Consider you have the following URI pattern to describe your Sentinel configuration:</p><pre>sentinel://:&lt;password&gt;@&lt;sentinel1_host&gt;:&lt;sentinel1_port&gt;,&lt;sentinel2_host&gt;:&lt;sentinel2_port&gt;,...,&lt;sentinelN_host&gt;:&lt;sentinelN_port&gt;/service_name:&lt;service_name&gt;</pre><p>For example:</p><pre>sentinel://:myP4ssw0rd@sentinel1.com:26379,sentinel2.com:26379,sentinel3.com:26379/service_name:my_master</pre><p>You can start the interactive mode on Redis CLI with any sentinel host available on the URI. For example:</p><pre>redis-cli -h sentinel1.com -p 26379 -a &quot;myP4ssw0rd&quot;</pre><p>After that you can discover the master info:</p><pre>&gt; SENTINEL get-master-addr-by-name my_master</pre><p>Supposing you have an answer like this:</p><pre>1) &quot;10.98.196.110&quot;<br>2) &quot;6379&quot;</pre><p>You can use this info to connect to the Redis instance:</p><pre>redis-cli -h 10.98.196.110 -p 6379 -a &quot;myP4ssw0rd&quot;</pre><p>And now you are able to interact with the Redis instance which is the current master.</p><h3>TL;DR</h3><ul><li>Single Instance:</li></ul><pre>redis-cli -h &lt;host&gt; -p &lt;port&gt; -a &quot;&lt;password&gt;&quot;</pre><ul><li>Cluster</li></ul><pre>redis-cli -h &lt;host&gt; -p &lt;port&gt; -a &quot;&lt;password&gt;&quot; -c</pre><ul><li>Sentinel</li></ul><pre>redis-cli -h &lt;sentinel_host&gt; -p &lt;sentinel_port&gt; -a &quot;&lt;password&gt;&quot;</pre><pre>&gt; SENTINEL get-master-addr-by-name &lt;service-name&gt;</pre><pre>redis-cli -h &lt;host&gt; -p &lt;port&gt; -a &quot;&lt;password&gt;&quot;</pre><p>I hope this was helpful!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2e84fbd1d09" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pitfalls while consuming a GraphQL API]]></title>
            <link>https://medium.com/@marcelo.nalon_37050/pitfalls-while-consuming-a-graphql-api-cbd1c04e4c46?source=rss-1c364ff46e37------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbd1c04e4c46</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo]]></category>
            <dc:creator><![CDATA[Marcelo Nalon]]></dc:creator>
            <pubDate>Tue, 06 Dec 2022 23:09:18 GMT</pubDate>
            <atom:updated>2022-12-07T00:49:11.037Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GxPy59EYCyDF66lDPbS1PQ.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/stevepb-282134/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=709045">Steve Buissinne</a> from <a href="https://pixabay.com//?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=709045">Pixabay</a></figcaption></figure><p>GraphQL is an amazing technology that drives data composition for APIs and provides a powerful and flexible way for clients to consume data. Nevertheless, this power comes with some care required by the nature of GraphQL, and when this is not considered it can lead to a lot of unexpected results on the client side.</p><p>I have been working with GraphQL for about four years and one of the things that caught my attention during this time is how clients tend to neglect or take some time to notice/understand some important aspects that should be considered while consuming a GraphQL API.</p><p>In this post, I am going to list the main topics that I believe everyone should be aware of before starting to consume a GraphQL API aiming to avoid undesired surprises on client-side applications. A minimum understanding of <a href="https://graphql.org/learn/"><strong>GraphQL concepts</strong></a> is required.</p><h3>Error Handling</h3><p>GraphQL is protocol agnostic. It only depends on communication protocols to transfer queries and responses, having its own way of expressing errors related to the queries sent to the API. It means clients must be aware that there will be some errors inherent in the protocol used to establish the communication between client and server and others inherent in the attempt to process queries on the server side.</p><p><a href="https://graphql.org/learn/serving-over-http/"><strong>HTTP</strong></a> is the most common choice for client-server protocol when using GraphQL, so some clients (mainly those familiar with REST APIs) tend to assume the only trusted source available to identify that something went wrong in the requested query is the HTTP status code of the response. Although, while using HTTP as the protocol to communicate with a GraphQL API, the errors can be classified into two categories: <strong>Network errors</strong> and <strong>GraphQL errors.</strong></p><h4><strong>Network Errors</strong></h4><p>These are any kind of errors related to communication with the server. When the TCP connection is established but for some reason, the server can not process the request there will be a response with an HTTP error status code, 4xx or 5xx (<em>e.g.</em> a <em>400</em> when the requested payload is malformed or a <em>500</em> when the server crashes due to some logic to generate the response). On the other hand, the TCP connection can also fail (<em>e.g.</em> due to a failure in the DNS resolution or when the server becomes unachievable for some reason) producing a network error as well. So far, no difference compared to consuming a REST API, right?</p><h4>GraphQL Errors</h4><p>These are errors related to the processing of a valid query sent to the API (when the connection is successfully established). Those errors are expressed in the response payload (field <em>errors</em>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c9e763c5fface7e1b4578c728e63abc/href">https://medium.com/media/5c9e763c5fface7e1b4578c728e63abc/href</a></iframe><p>And maybe here comes the first misunderstanding for those who are starting to work with GraphQL.</p><h4>Partial Data</h4><p>To explain why sometimes GraphQL errors become a pitfall it is necessary to understand how a GraphQL API works. It will try to make its best effort to deliver the data value for every field requested in a query. But sometimes some piece of the requested data can be in trouble on the server side (e.g a database where the data is retrieved can be down). Though if there are other parts in the query that the server can successfully process and respond it will do so. This capability of returning partial data, even when there are errors associated with some requested parts is one of the main features of GraphQL. Usually, the requested fields that are associated with any kind of error will become <em>null</em> values, and all other parts successfully processed will be returned altogether in the same response (field <em>data</em>), plus an <em>errors</em> field explaining why some fields were not returned correctly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a776e9c26b2f8a9ebf25020fb753577/href">https://medium.com/media/4a776e9c26b2f8a9ebf25020fb753577/href</a></iframe><p>Of course, which info will be exposed by the <em>errors</em> field can vary by API and there are some approaches to make this more intuitive and less complex, but this is not the focus of this post.</p><p>Do you remember when I said GraphQL is protocol agnostic? This is an important consideration here because, with partial data, the only way to really guarantee that the query has no error is to analyze the overall response payload. Using HTTP you can receive a 200 status code in the response even when your query receives some error from the backend.</p><p>So always keep in mind you need to consider all those sources of errors if you want to deal with them predictably on your client-side application. Some client libraries like <a href="https://www.apollographql.com/docs/react/data/error-handling/">Apollo Client</a> provide means to set up how you want to consider those classes of errors for each query done by the client.</p><h3>Nullability</h3><p><a href="https://graphql.org/learn/best-practices/#nullability"><strong>All fields in GraphQL are nullable by default</strong></a>. It means that a GraphQL API can return <em>null</em> values for fields even when there is no error associated with them in a query. It can happen either because a value for a field is really “empty” or because some failure happened on the backend side but, in favor of delivering partial data, it decides to just return <em>null</em> for the field and omit the failure (of course it depends on the API design when partial data makes sense).</p><p>Clients must be aware of this because it is important to be prepared to deal with <em>null</em> values on fields even when the payload response does not indicate errors.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c93a899dc42507ac627677a8606a80b9/href">https://medium.com/media/c93a899dc42507ac627677a8606a80b9/href</a></iframe><p>This behavior is sometimes confusing because when some GraphQL Error happens the values of fields associated with the errors also become <em>null. </em>So clients tend to think that the only way a field can assume a <em>null</em> value is when there is some error related to this requested field on a query.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7e5291855e78e18e7fec5fe78df29c4/href">https://medium.com/media/d7e5291855e78e18e7fec5fe78df29c4/href</a></iframe><p>Plus GraphQL provides a mechanism to define <a href="https://graphql.org/learn/schema/#lists-and-non-null"><strong>non-null</strong></a> fields (it is denoted by a trailing &quot;!&quot; beside the type value of a field if you can see the schema of the API you are consuming). In summary, it tells the client that, if for some reason the backend can not fill the value an error will be generated and the previous parent field will become <em>null</em> instead (actually the <em>null</em> result bubbles up to the nearest nullable parent field).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0166c565914d2cf300f1e040fe0a383f/href">https://medium.com/media/0166c565914d2cf300f1e040fe0a383f/href</a></iframe><p>The default behavior of nullability can become even more confusing when we are dealing with lists. When a value of a field is a list and there is no non-null modifier on it you can receive some <em>null </em>items inside the list.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd7587f6c312da3a5b314de27b940cc5/href">https://medium.com/media/fd7587f6c312da3a5b314de27b940cc5/href</a></iframe><p>Nullability has also implications on arguments and input fields, but from the client’s perspective those are more predictable: non-null arguments/input fields not sent in the query will produce an error related to the request itself and the query will be rejected (APIs provided through HTTP will normally respond with a bad request status on these cases).</p><h3>Incremental Types</h3><p>I would classify three GraphQL types as “incremental types”: <a href="https://graphql.org/learn/schema/#enumeration-types"><strong>enum</strong></a>, <a href="https://graphql.org/learn/schema/#union-types"><strong>union</strong></a>, and <a href="https://graphql.org/learn/schema/#interfaces"><strong>interface</strong></a>. I say they are <strong>incremental</strong> because while a GraphQL API evolves you can have additions on what those types can return as their values without this being considered a breaking change (unless a design decision is made and guarantees it will never happen).</p><h4>Enum</h4><p>An <a href="https://graphql.org/learn/schema/#enumeration-types"><strong>enum</strong></a> is a <a href="https://graphql.org/learn/schema/#scalar-types"><strong>scalar type</strong></a> that can assume some predefined values.</p><p>Suppose at some point an API defines an <em>enum</em> and a <a href="https://graphql.org/learn/execution/#root-fields-resolvers"><em>root query field</em></a> that returns the <em>enum</em> as its value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ded05b486a7cec37ffb69e4ffe4b62/href">https://medium.com/media/01ded05b486a7cec37ffb69e4ffe4b62/href</a></iframe><p>Initially, a client could do the following query requesting a <em>LocationType</em> by its <em>id:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34594819b1da5136613d7c0a9e701eca/href">https://medium.com/media/34594819b1da5136613d7c0a9e701eca/href</a></iframe><p>and expect to receive any of the three values predefined by <em>LocationType </em>as the value of the <em>locationType</em> field.</p><p>Although while the API evolves new values can be incremented on the <em>LocationType:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f038f21a991ec6980cc9192a10cbb30b/href">https://medium.com/media/f038f21a991ec6980cc9192a10cbb30b/href</a></iframe><p>Since this is not considered a breaking change from the API perspective, clients that have specific logic depending only on the three previous values can be caught off guard. Receiving an unrecognized value for the <em>location</em> field can break the application.</p><p>So always keep in mind when a field has an <em>enum</em> type as its value, you need to decide what to do considering new values not mapped at some point will appear in the future.</p><h4>Interface</h4><p>An <a href="https://graphql.org/learn/schema/#interfaces"><strong>interface</strong></a> is an &quot;abstract&quot; type that defines a set of fields that other &quot;concrete&quot; types must define to implement the declared <em>interface</em>. It is similar to the interface concept in <a href="https://en.wikipedia.org/wiki/Object-oriented_programming"><strong>OOP</strong></a><strong> </strong>but here the focus is on establishing a list of fields a type must have to implement some interface.</p><p>That said when a field defines its value as an interface it means that this field can receive any of the types that implement the interface as its value. You can query the fields defined by the interface and extra fields defined by the concrete types (using <a href="https://graphql.org/learn/queries/#inline-fragments">inline fragments</a>).</p><p>Let’s see what happens when you are consuming a field that is an interface and new types implement this interface while the API evolves.</p><p>Suppose at some moment an API defines an <em>interface</em>, two concrete types that implement this <em>interface,</em> and a <em>root query field</em> that returns the <em>interface</em> as its value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9234bda7d001f919a09c11645cf2ce7/href">https://medium.com/media/d9234bda7d001f919a09c11645cf2ce7/href</a></iframe><p>Initially, a client could do the following query requesting an <em>Event</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bd03247d2ee0afb0fe936cf9399ae73/href">https://medium.com/media/8bd03247d2ee0afb0fe936cf9399ae73/href</a></iframe><p>Supposing the API fills out <em>topEvent </em>field<em> </em>with a <em>SoccerMatch</em>, the response would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9b6f037d41e8a2ef3d079845a5654a9/href">https://medium.com/media/a9b6f037d41e8a2ef3d079845a5654a9/href</a></iframe><p>In the same way, if the value becomes a MusicFestival:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88017a93d0296d9a4c903d8618159553/href">https://medium.com/media/88017a93d0296d9a4c903d8618159553/href</a></iframe><p>So if you are an application that is prepared to consume those two kinds of <em>Event </em>everything would be fine if the API responds with one of them as a value for the <em>topEvent </em>field<em>.</em></p><p>But if at some moment a new kind of Event is implemented by the API, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aa3a6401eb6163fcf8a12539b598a10/href">https://medium.com/media/0aa3a6401eb6163fcf8a12539b598a10/href</a></iframe><p>the API makes it a new eligible type to be returned as a <em>topEvent </em>field value.</p><p>Then for the clients that keep doing the previous query (really common when you support multiple versions of an application for end users), if the API fills out the <em>topEvent </em>field<em> </em>value<em> </em>with a<em> RealityShow, </em>the response would become something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79f7e7a4e11c85246aca755cf5a03041/href">https://medium.com/media/79f7e7a4e11c85246aca755cf5a03041/href</a></iframe><p>So if the application is not prepared to deal with unrecognized types something can go wrong.</p><p>As with enum values, always bear in mind that when a field has an <em>interface</em> type as its value you need to decide what to do considering new concrete types not mapped at some moment will appear in the future. Notice that since we are dealing with interfaces it allows the client application to rely on the fields defined by the interface regardless of the returned concrete type (considering the <strong>nullability</strong> issues of course).</p><h4>Union</h4><p>A <a href="https://graphql.org/learn/schema/#union-types"><strong>union</strong></a> is also an &quot;abstract&quot; type although, unlike interfaces, it is not a type defined to be implemented. It defines a list of types that can be concretely returned as values on fields that have a defined <em>union</em> as their values. Furthermore, the types that make up the list of types defined by a union do not even need a common field.</p><p>Let’s see what happens when you are consuming a field that is a <em>union</em> and new types are added to the list of types defined by it.</p><p>Suppose at some point an API defines two concrete types, a <em>union</em> composed by them<em>,</em> and a <em>root query field</em> that returns the <em>union</em> as its value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8f60d48d92ea24d392e14a346e2a90f/href">https://medium.com/media/c8f60d48d92ea24d392e14a346e2a90f/href</a></iframe><p>Knowing the current API schema the client could do the following query:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c39413b0d3468ce0a5b0a5556fd03d0d/href">https://medium.com/media/c39413b0d3468ce0a5b0a5556fd03d0d/href</a></iframe><p>Considering the API returns a <em>Circle </em>for the <em>shape</em> field<em> </em>the response would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d427e99a94454080de488fb1bbbd51d/href">https://medium.com/media/2d427e99a94454080de488fb1bbbd51d/href</a></iframe><p>In the same way, if the value would become a <em>Square</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64f83924d970bc3fcc2c916ee93fff94/href">https://medium.com/media/64f83924d970bc3fcc2c916ee93fff94/href</a></iframe><p>If at some moment a new type would be declared and added to the union type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e49b0338960c674369518c7583f6a63/href">https://medium.com/media/7e49b0338960c674369518c7583f6a63/href</a></iframe><p>If the API starts to return a Rectangle as the field value the client who keeps doing the previous query would receive something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9204cea2fc022fe8297c717b32e47ce/href">https://medium.com/media/e9204cea2fc022fe8297c717b32e47ce/href</a></iframe><p>Note that since no fragment is mapping the new type the only field received would be the <em>__typename. </em>Just as we deal<em> </em>with all incremental types if the client has no strategy to deal with future types added to the union the client application can end up in an inappropriate state.</p><p>The same considerations apply when a field has its value as a list of any &quot;incremental type&quot;. The list items can bring new values or types not mapped at some point, so be aware of this while iterating through them.</p><h3>Conclusion</h3><p>If you are new to the GraphQL world, take a moment to learn about the main concepts and traits of this fantastic technology (there are thousands of great materials on the web). I have tried to summarize here the most common pitfalls clients face while consuming a GraphQL API, mainly considering reading operations. There are other peculiarities related to <a href="https://graphql.org/learn/queries/#mutations">writing operations</a> and the strategies to optimize the API consumption (like caching), but those are subjects for a future post, perhaps. Anyway, do not be daunted by all these details. Master them and embrace the power GraphQL can offer you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbd1c04e4c46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>