<?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: Samuel Monteiro</title>
    <description>The latest articles on DEV Community by Samuel Monteiro (@samuelmpinho).</description>
    <link>https://dev.to/samuelmpinho</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%2F168200%2Ff1171114-af59-4851-86c1-d80f5d38b334.jpg</url>
      <title>DEV Community: Samuel Monteiro</title>
      <link>https://dev.to/samuelmpinho</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samuelmpinho"/>
    <language>en</language>
    <item>
      <title>Monorepos and Nx</title>
      <dc:creator>Samuel Monteiro</dc:creator>
      <pubDate>Wed, 01 Sep 2021 20:22:36 +0000</pubDate>
      <link>https://dev.to/samuelmpinho/monorepo-and-nx-2oic</link>
      <guid>https://dev.to/samuelmpinho/monorepo-and-nx-2oic</guid>
      <description>&lt;h2&gt;
  
  
  What's a Monorepo?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;monorepo&lt;/code&gt; is a single repository that can have one or more projects inside of it. You can have frontend, backend and helper tools, all in the same &lt;code&gt;monorepo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You could have a customer and a backoffice applications, both living in separate folders consuming an API living in another folder and a set of &lt;code&gt;interfaces&lt;/code&gt; that would be shared by your applications and APIs.&lt;/p&gt;

&lt;p&gt;If you had a &lt;code&gt;multi-repo&lt;/code&gt;, one &lt;code&gt;repository&lt;/code&gt; would contain the &lt;code&gt;customer&lt;/code&gt; application, another one, the &lt;code&gt;backoffice&lt;/code&gt;, another one would have the &lt;code&gt;API&lt;/code&gt; and another for the &lt;code&gt;interfaces&lt;/code&gt;. Maybe all of your applications would consume the &lt;code&gt;API&lt;/code&gt; and the &lt;code&gt;interfaces&lt;/code&gt; through a &lt;code&gt;package&lt;/code&gt; published on the &lt;code&gt;npm&lt;/code&gt; or some other package manager.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should you use it?
&lt;/h2&gt;

&lt;p&gt;If you're working in a company or a team, most certainly they will have best practices and code standards defined and if they don't, they will have them sooner or later. &lt;/p&gt;

&lt;p&gt;Also as the company grows in people and in complexity you will have separate teams working to achieve the same or correlate goals. You could have a frontend and a backend team, both working to achieve the same goal in different repositories and this could lead to some problems in the future, like duplicated &lt;code&gt;types&lt;/code&gt;,  slow development and deliver time and so on.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;monorepo&lt;/code&gt; addresses some of those issues by having everything in the same &lt;code&gt;repository&lt;/code&gt;. Some of the benefits are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain your company best practices in one place and apply them without much effort&lt;/li&gt;
&lt;li&gt;Every best practice that you have, would be adopted by every project automatically with the help of tools like &lt;code&gt;eslint&lt;/code&gt; and &lt;code&gt;prettier&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It's easier to share code between your projects&lt;/li&gt;
&lt;li&gt;The shared code it's easier to maintain&lt;/li&gt;
&lt;li&gt;Changes that you make could be reflected on every project in the same time that that they're made&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Drawback of a Monorepo
&lt;/h2&gt;

&lt;p&gt;Even though it's has a lot of benefits, like all things in life, it comes with some drawbacks&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every change to your shared code will reflect on every application that you have. So, you need to make sure that everything is still working as expected in every affected application&lt;/li&gt;
&lt;li&gt;Performance issues with your VCS (version system control) as your &lt;code&gt;monorepo&lt;/code&gt; grows in size.&lt;/li&gt;
&lt;li&gt;It's hard for your team/company to give specific access permissions to specific teams, since everything is in the same &lt;code&gt;repository&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Applying some specific rules to your &lt;code&gt;monorepo&lt;/code&gt; could be a problem. Like getting some specific parts of it and make it &lt;code&gt;open-source&lt;/code&gt;. That's not easy to accomplish and probably you would need some specific tools to help you with that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Nx, a tool to help you manage your &lt;code&gt;monorepo&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;And then comes &lt;code&gt;Nx&lt;/code&gt;. A tool that aims to facilitate the process of managing a &lt;code&gt;monorepo&lt;/code&gt; by providing a set of helpers and commands that the developers and the maintainers could use to organize and develop faster within a &lt;code&gt;monorepo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's main structure consists of two folders:&lt;/p&gt;

&lt;h3&gt;
  
  
  /Apps
&lt;/h3&gt;

&lt;p&gt;It's a place to store all of your applications and their &lt;code&gt;e2e&lt;/code&gt; tests. For example, you could have a website for your customer and one for your backoffice, and they both could consume some &lt;code&gt;components&lt;/code&gt; created on the &lt;code&gt;libs&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  /Libs
&lt;/h3&gt;

&lt;p&gt;It's where you'd store every code that you want to share between your applications created on the &lt;code&gt;apps&lt;/code&gt; folder. For example, you could have a &lt;code&gt;components library&lt;/code&gt; inside that folder and use the components created there inside your &lt;code&gt;applications&lt;/code&gt; without a lot of effort. You could also have an API that would be used by your apps for example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code example
&lt;/h3&gt;

&lt;p&gt;Imagine that you have the following button inside &lt;code&gt;/libs/button&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, you want to use that button inside your application that it's located in &lt;code&gt;/apps/customer&lt;/code&gt;. It's easy as that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;your-monorepo-name&amp;gt;/button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CustomPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            This could be a form
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;h2&gt;
  
  
  Why Nx?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Nx helpers
&lt;/h3&gt;

&lt;p&gt;Nx comes with &lt;code&gt;pre-built&lt;/code&gt; generators that aims to facilitate the process of creating Applications and Libraries with &lt;code&gt;React&lt;/code&gt;, &lt;code&gt;Angular&lt;/code&gt; and many other frameworks. These &lt;code&gt;generators&lt;/code&gt; can be used by with the help of commands that you run on your terminal and it will generate and add code inside your folders and update any other files that may be necessary.&lt;/p&gt;

&lt;p&gt;And you can also create your own &lt;code&gt;generators&lt;/code&gt; to suit your team and company needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other things
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It has a very good VsCode extension that make it easier to manage the &lt;code&gt;monorepo&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;IT has a &lt;code&gt;dependency graph&lt;/code&gt; that helps knowing which part of your application should be builded again and tested when somethings changes&lt;/li&gt;
&lt;li&gt;It has a single &lt;code&gt;package.json&lt;/code&gt; file in the root of the &lt;code&gt;monorepo&lt;/code&gt; which aims to make all of your code use the same &lt;code&gt;dependency&lt;/code&gt; versions&lt;/li&gt;
&lt;li&gt;It caches all of your commands, like build and test ones. So, the first time that you run some command, it could take a while, but after that, it will be blazingly fast because of the cache that was made.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The choice between going with a &lt;code&gt;multi-repo&lt;/code&gt; or &lt;code&gt;monorepo&lt;/code&gt; is a difficult one, both of them come with their own set of benefits and drawbacks. One thing that I can say to you is to balance them out with your team and organization to choose the right tool for the job.&lt;/p&gt;

&lt;p&gt;Feel free to &lt;a href="https://twitter.com/samuelmpinho"&gt;send me a tweet and follow me on twitter&lt;/a&gt; 🤙&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to think your application with Relay</title>
      <dc:creator>Samuel Monteiro</dc:creator>
      <pubDate>Tue, 22 Jun 2021 23:16:28 +0000</pubDate>
      <link>https://dev.to/samuelmpinho/how-to-think-your-application-with-relay-2lpo</link>
      <guid>https://dev.to/samuelmpinho/how-to-think-your-application-with-relay-2lpo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;h3&gt;
  
  
  First impression
&lt;/h3&gt;

&lt;p&gt;I didn't have a good first impression about Relay when I've started using it. I found it difficult to understand, verbose to use, and I didn't saw the benefits from it.&lt;/p&gt;

&lt;p&gt;Even though I didn't like it, I was part of a team, and as a team we choose to stick with Relay and see in the long term if it was a good choice or not.&lt;/p&gt;

&lt;p&gt;As the time goes by, I've started to get along with it and to understand how to use it. I still didn't have the full idea, but just to realize how I could use it to solve simple problems like data fetching, and to know what I was doing was enough for me at the time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Responsibilities
&lt;/h3&gt;

&lt;p&gt;Months later, I was promoted to tech lead and with that came the responsibility to understand and to explain to my team why we're using the stuff that we're using. I had a challenge. I needed to understand why we use Relay and not something else.&lt;/p&gt;

&lt;p&gt;And I believe that like any other solution, if you don't know how and why you use it, you're going to face the same or even worse problems that you're trying to solve with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  This article
&lt;/h3&gt;

&lt;p&gt;This article is a grasp of that process of understanding why we use Relay. I'm going to show you how to think your application using Relay, because I believe that to understand other solutions that Relay provides you need to understand first what problems we have right now.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Relay?
&lt;/h2&gt;

&lt;p&gt;It's a JavaScript framework that tries to facilitate the process of fetching data on front-end using &lt;code&gt;GraphQL&lt;/code&gt;. It's developed by Facebook and was conceived with the same ideia of React componentization.&lt;/p&gt;

&lt;h3&gt;
  
  
  React components and Relay
&lt;/h3&gt;

&lt;p&gt;The ideia behind components in React it's to decrease the complexity of your app by dividing it into smaller parts called components. These components are easier to understand and to maintain, and that increases the capability of your app to scale.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;And how does that relates to Relay?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The ideia behind Relay is that you have your data dependencies collocated with your component and that it's beneficial for some reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's easier to understand which data is needed for your component to work.&lt;/li&gt;
&lt;li&gt;If your component needs any other data from your server, you don't need to change your entire &lt;code&gt;query&lt;/code&gt; structure, just your component. (&lt;strong&gt;Not every case works this way, but most of them&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;It's easier to test your component isolated from your entire structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to use Relay?
&lt;/h2&gt;

&lt;p&gt;To understand it, let's take that YouTube page below:&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%2Foizvd8xunk2wmlsgt8zq.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%2Foizvd8xunk2wmlsgt8zq.png" alt="youtube"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We can divide it in four components that receive the data from the servers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;VideoPlayer&lt;/code&gt;: used to render the video that we're watching. Probably need the &lt;code&gt;videoSrc&lt;/code&gt; from the server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;VideoDetails&lt;/code&gt;: show the video details like &lt;em&gt;title&lt;/em&gt;, &lt;em&gt;description&lt;/em&gt;, &lt;em&gt;author&lt;/em&gt;, &lt;em&gt;number of likes&lt;/em&gt; and &lt;em&gt;dislikes&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RelatedVideos&lt;/code&gt;: it's a list of videos that the YouTube algorithm believes that you would like to see.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UserImg&lt;/code&gt;: renders the logged user profile image.&lt;/li&gt;
&lt;/ul&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%2Faawa0bqst53ytnu7b1n7.jpeg" 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%2Faawa0bqst53ytnu7b1n7.jpeg" alt="youtube_components"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just remembering that all of that it's just an example. For sure the real YouTube architecture it's much more complex than this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With these components in mind, we have two approaches to get the data from the server using Relay.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Each component fetches the data that it needs
&lt;/h3&gt;

&lt;p&gt;We can draw a diagram like this to represent that solution:&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%2Fv0e3zua12qli9dn7u6mh.jpg" 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%2Fv0e3zua12qli9dn7u6mh.jpg" alt="first-architecture"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;On the left side, we have a simplified version of the YouTube page. Where each component is represented by a gray circle and they call the server through a &lt;code&gt;GraphQL&lt;/code&gt; query, like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="err"&gt;graphql`&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;NavbarQuery&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;profileImg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Benefits
&lt;/h4&gt;

&lt;p&gt;With this solution, we could show different loading indicators in each part of our application. Something like this:&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%2F7jzu5yuh48aar4pvkt00.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%2F7jzu5yuh48aar4pvkt00.png" alt="linkedin image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;By doing that we improve the user experience by not blocking entirely his access to the screen and show which data we're fetching and what we already fetched.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://uxdesign.cc/what-you-should-know-about-skeleton-screens-a820c45a571a" rel="noopener noreferrer"&gt;Here's an article&lt;/a&gt; explaining why you should use &lt;code&gt;loading skeletons&lt;/code&gt; in your interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Downsides
&lt;/h4&gt;

&lt;p&gt;The first problem is related with the tree architecture where a component depends on another one to render. As an example, let's get just the structure responsible for showing us the video:&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%2Fimr7hc9ilui793zycxao.jpg" 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%2Fimr7hc9ilui793zycxao.jpg" alt="VideoPlayer architecture"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here, we're only going to get the data with the &lt;code&gt;videoSrc&lt;/code&gt; when the component &lt;code&gt;VideoPlayer&lt;/code&gt; is fully rendered. And if for some reason any of these components above the &lt;code&gt;VideoPlayer&lt;/code&gt; take a long time to load, we would need to wait that time until we can call the server and start loading the video.&lt;/p&gt;

&lt;p&gt;With that we would have two times to load the video:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rendering the components above the &lt;code&gt;VideoPlayer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;recieving the &lt;code&gt;response&lt;/code&gt; from the &lt;code&gt;server&lt;/code&gt; with the &lt;code&gt;videoSrc&lt;/code&gt; data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another problem is that we would end up with a lot of requests to the server, where each one of them will ask &lt;strong&gt;just for a single part of the data&lt;/strong&gt;. And it makes sense that once the connection is open with the server, we ask for all the data that we need.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ok, so we have these two problems. What it's the alternative?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. The recommended solution
&lt;/h3&gt;

&lt;p&gt;Instead of fetching the data on each component, we fetch them once on the page load, in other words, &lt;strong&gt;every page is a query&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But didn't you said that Relay is built on colocating the data dependencies with the components that need them?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes, I did. When I said &lt;code&gt;data dependencies&lt;/code&gt; I didn't mean the fetch function, I mean the declaration of the data that it's needed. We only fetch once, at the page render. It would look like this&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%2Ftxhrxv5xzz8w30crmc19.jpg" 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%2Ftxhrxv5xzz8w30crmc19.jpg" alt="second-architecture"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Benefits
&lt;/h4&gt;

&lt;p&gt;With that, we ask for all the data that we need to render the page on the page load. This comes with the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We decrease the amount of request made to the server.&lt;/li&gt;
&lt;li&gt;Since we're not waiting for some component to load, we decrease the load time to show relevant data to the user.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Colocating the data dependencies with Fragments
&lt;/h4&gt;

&lt;p&gt;In order to colocate the data dependencies of a component close to it, we can use &lt;code&gt;Relay&lt;/code&gt; &lt;code&gt;Fragments&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Fragment&lt;/code&gt;, in &lt;code&gt;Relay&lt;/code&gt;, it's a declaration of the data that a specific component need. &lt;/p&gt;

&lt;p&gt;It's like what we had with every component making a fetch, but instead of a fetch, we're only declaring the data that we need, and the fetch only occurs once. The implementation would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;page&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;component&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;graphql`&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;PageQuery&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="n"&gt;NavbarFragment_user&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;navbar&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;component&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;graphql`&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="k"&gt;fragment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;NavbarFragment_user&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;UserType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;profileImg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, the &lt;code&gt;Navbar&lt;/code&gt; declares exactly what it needs and if something change, we will only change on the fragment, not on the page query.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Even though this is nice, we have a problem :(&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Downsides
&lt;/h4&gt;

&lt;p&gt;At the version &lt;code&gt;10&lt;/code&gt; of &lt;code&gt;Relay&lt;/code&gt; we can't have a &lt;code&gt;loading indicator&lt;/code&gt; per component, we need to show a &lt;code&gt;loading indicator&lt;/code&gt; on the entire page before showing some data to the user, something like this:&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%2Fs9p6541nyv60ernabe23.jpg" 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%2Fs9p6541nyv60ernabe23.jpg" alt="full-loading-screen"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You have two ways to solve this problem.&lt;/p&gt;

&lt;p&gt;You could use the first approach to solve this problem, where each component calls a &lt;code&gt;fetch&lt;/code&gt; and while this &lt;code&gt;fetch&lt;/code&gt; don't return a response, you show a &lt;code&gt;loading indicator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The other way and the one that I recommend, is to &lt;strong&gt;upgrade your Relay to version 11&lt;/strong&gt; and start to use the &lt;code&gt;@defer&lt;/code&gt; directive from &lt;code&gt;GraphQL&lt;/code&gt; alongside &lt;code&gt;Suspense&lt;/code&gt; component from &lt;code&gt;React&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;@defer&lt;/code&gt; directive you could say that a specific piece of your &lt;code&gt;query&lt;/code&gt;, like a &lt;code&gt;fragment&lt;/code&gt;, should be loaded asynchronously and while the response of that piece doesn't return from the server, you show a &lt;code&gt;loading indicator&lt;/code&gt; passed to your &lt;code&gt;Suspense&lt;/code&gt; component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://relay.dev/docs/guided-tour/reusing-cached-data/rendering-partially-cached-data/" rel="noopener noreferrer"&gt;Here's the Relay documentation&lt;/a&gt; where they explain in more details how to do that and in &lt;a href="https://youtu.be/Tl0S7QkxFE4" rel="noopener noreferrer"&gt;this video&lt;/a&gt; how they're using that on Facebook.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Just as &lt;code&gt;React&lt;/code&gt; once was, &lt;code&gt;Relay&lt;/code&gt; still a not much used library and because of that there's not much articles and tutorials explaining how it works.&lt;br&gt;
I do hope that this article increased your understanding about how to use &lt;code&gt;Relay&lt;/code&gt; on your application or how its main idea works.&lt;/p&gt;

&lt;p&gt;If you missed a more technical explanation, or if you still have some questions that I didn't cover, feel free to &lt;a href="https://twitter.com/samuelmpinho" rel="noopener noreferrer"&gt;send me a tweet or a DM&lt;/a&gt; 🤙&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>graphql</category>
      <category>relay</category>
    </item>
    <item>
      <title>Como implementar Type guards no seu código</title>
      <dc:creator>Samuel Monteiro</dc:creator>
      <pubDate>Thu, 20 May 2021 00:21:19 +0000</pubDate>
      <link>https://dev.to/samuelmpinho/como-implementar-type-guards-no-seu-codigo-2040</link>
      <guid>https://dev.to/samuelmpinho/como-implementar-type-guards-no-seu-codigo-2040</guid>
      <description>&lt;p&gt;Imagine que você tem a seguinte situação:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Você tem uma aplicação que busca por músicas no Spotify e nessa aplicação você mostra o &lt;code&gt;email&lt;/code&gt; do usuário logado e a música que ele está escutando naquele momento.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Você então cria 2 funções para acessar a API do Spotify&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;getUser&lt;/code&gt; para chamar a API que irá buscar os dados do usuário&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getSong&lt;/code&gt; que irá retornar uma lista de músicas do Spotify&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Então você teria o seguinte código:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;APIResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Song&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;APIResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getSong&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;APIResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Song&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&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;Depois de implementar as 2 funções, você percebe que elas são muito parecidas. As 2 recebem uma &lt;code&gt;url&lt;/code&gt; que é passada para dentro do método &lt;code&gt;fetch&lt;/code&gt;. Esse método faz a chamada para a &lt;code&gt;API&lt;/code&gt; e depois retorna o resultado.&lt;/p&gt;

&lt;p&gt;E aí, você pode pensar&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;poxa, já que elas são tão parecidas, acho que vou criar uma função que reduz a quantidade de código repetido&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchApi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;APIResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Song&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userResppnse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchApi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://apiUser...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;songResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchApi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://apiSong...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Já parece que ficou melhor. Agora temos menos código duplicado e menor possibilidade de erro.&lt;/p&gt;

&lt;p&gt;Mas você vai ter um problema de &lt;code&gt;Typescript&lt;/code&gt; 😟&lt;/p&gt;

&lt;p&gt;O tipo da variável &lt;code&gt;userResponse&lt;/code&gt; está como &lt;code&gt;APIResponse&amp;lt;User | Song&amp;gt;&lt;/code&gt; então se você tentar fazer isso:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchApi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Você vai tomar o seguinte erro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Property&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;does&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;exist&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User | Songs[]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
  &lt;span class="nx"&gt;Property&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;does&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;exist&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Songs[]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;O Typescript não consegue dizer se a propriedade &lt;code&gt;data&lt;/code&gt; da variável &lt;code&gt;userResponse&lt;/code&gt; é um &lt;code&gt;User&lt;/code&gt; ou um &lt;code&gt;Song&lt;/code&gt; e por isso ele previne que você chame a propriedade &lt;code&gt;email&lt;/code&gt; que é exclusiva do &lt;code&gt;User&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Isso garante que se por exemplo isso acontecer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://apiSong...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Você não vai ter problemas mais pra frente.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type guards
&lt;/h3&gt;

&lt;p&gt;Os &lt;code&gt;type guards&lt;/code&gt; são uma forma de dizer para o &lt;code&gt;Typescript&lt;/code&gt; qual tipo nós esperamos.&lt;/p&gt;

&lt;p&gt;Podemos criar um &lt;code&gt;type guard&lt;/code&gt; de usuário que garante o tipo &lt;code&gt;User&lt;/code&gt; da seguinte forma:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Song&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Estamos criando a função &lt;code&gt;isUser&lt;/code&gt; que aceita o tipo &lt;code&gt;data: User | Song&lt;/code&gt; e que retorna um &lt;code&gt;data is User&lt;/code&gt;. A expressão &lt;code&gt;is User&lt;/code&gt; diz que eu, como desenvolvedor, garanto que o retorno da minha função é do tipo &lt;code&gt;User&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;O que falta é a implementação dessa verificação. Já que o tipo &lt;code&gt;Song&lt;/code&gt; não tem o campo &lt;code&gt;email&lt;/code&gt; podemos verificar se a variável &lt;code&gt;data&lt;/code&gt; possuí ele. &lt;/p&gt;

&lt;p&gt;Se possuir, podemos dizer que &lt;code&gt;data&lt;/code&gt; é do tipo &lt;code&gt;User&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;E com isso, podemos executar o seguinte código sem problemas de &lt;code&gt;Typescript&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&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;Assim, o &lt;code&gt;Typescript&lt;/code&gt; não vai mostrar um erro porque você disse que a função &lt;code&gt;isUser&lt;/code&gt; retorna &lt;code&gt;User&lt;/code&gt; e nada mais.&lt;/p&gt;

&lt;p&gt;Não tem possibilidade da variável &lt;code&gt;data&lt;/code&gt; ser do tipo &lt;code&gt;Song&lt;/code&gt; por exemplo.&lt;/p&gt;

&lt;p&gt;E para finalizar, o type guard do tipo &lt;code&gt;Song&lt;/code&gt; seria assim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isSong&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Song&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;Song&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Song&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isSong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;songResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;songResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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;Através dos &lt;code&gt;type guards&lt;/code&gt; você consegue ter maior controle sobre a tipagem do seu código. Tudo isso em tempo de &lt;code&gt;runtime&lt;/code&gt;, o que é bem legal 🤘&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Apesar de ser uma boa adição na sua base de código, o &lt;code&gt;type guards&lt;/code&gt; é propenso a falhas já que quem define a função para validar o tipo é o próprio desenvolvedor. &lt;/p&gt;

&lt;p&gt;Para resolver essas falhas e realmente trabalhar com um &lt;code&gt;typchecking&lt;/code&gt; em &lt;code&gt;runtime&lt;/code&gt;, sugiro dar uma lida &lt;a href="https://unsplash.com/blog/user-defined-type-guards-not-safe/"&gt;nesse artigo do pessoal do Unsplash&lt;/a&gt;. O artigo destaca o porquê de &lt;code&gt;type guards&lt;/code&gt; definidos pelo usuário não serem tão seguros e o que fazer nesses casos.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Galera, é isso por hoje. Fiz esse artigo porque acabei me deparando com algumas situações na minha aplicação que funcionariam melhor se tivesse um &lt;code&gt;type guard&lt;/code&gt; implementado. E apesar de serem muito úteis, não vejo muita gente usando no dia a dia, então fica aqui minha contribuição. Espero que consigam aproveitar de alguma forma 😉&lt;/p&gt;

&lt;p&gt;E se vocês sentiram falta de alguma explicação, ficaram com alguma dúvida ou só querem trocar uma ideia, podem me marcar ou mandar DM no &lt;a href="https://twitter.com/samuelmpinho"&gt;twitter&lt;/a&gt; 🤙&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Create git-graphs easily</title>
      <dc:creator>Samuel Monteiro</dc:creator>
      <pubDate>Wed, 23 Sep 2020 19:04:26 +0000</pubDate>
      <link>https://dev.to/samuelmpinho/create-git-graphs-easily-1f63</link>
      <guid>https://dev.to/samuelmpinho/create-git-graphs-easily-1f63</guid>
      <description>&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;p&gt;Have you ever struggled while trying to explain how the git flow on your squad,team or company works to one of you coworkers?&lt;/p&gt;

&lt;p&gt;I've already struggled with this situation and back in the time I've created a new folder, started a new git with &lt;code&gt;git init&lt;/code&gt; and simulated all the flow.&lt;br&gt;
The problem with this approach is that it doesn't have any visualization of the git graph that's been created. And we all know that in the beginning, git is hard and with experience and time, git continues being hard 🙃&lt;/p&gt;

&lt;h2&gt;
  
  
  How?
&lt;/h2&gt;

&lt;p&gt;So with that in mind, I found a lib that creates &lt;a href="https://github.com/nicoespeon/gitgraph.js"&gt;git graphs in svg&lt;/a&gt; and they already had a &lt;a href="https://github.com/nicoespeon/gitgraph.js/tree/master/packages/gitgraph-react"&gt;port for React&lt;/a&gt;. &lt;br&gt;
Then, I just created a website that facilitates the creation of the graph svg by manipulating the gitgraphjs api and here it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here it is
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://git-graph-creator.netlify.app/"&gt;https://git-graph-creator.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's under development and there's some improvements that could be made in order to be more complete and make use of full gitgraphjs api. It's also an open source project and I'm open to any feedback that you may give 🤙&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/SamuelPinho/gitgraph-creator"&gt;repo link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>react</category>
      <category>github</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
