<?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: Marcon</title>
    <description>The latest articles on DEV Community by Marcon (@marconlp).</description>
    <link>https://dev.to/marconlp</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%2F993187%2F27908670-432b-46d8-ab31-ea737e8db593.png</url>
      <title>DEV Community: Marcon</title>
      <link>https://dev.to/marconlp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marconlp"/>
    <language>en</language>
    <item>
      <title>tRPC in 100 seconds</title>
      <dc:creator>Marcon</dc:creator>
      <pubDate>Mon, 06 Feb 2023 17:54:15 +0000</pubDate>
      <link>https://dev.to/marconlp/trpc-in-100-seconds-jac</link>
      <guid>https://dev.to/marconlp/trpc-in-100-seconds-jac</guid>
      <description>&lt;p&gt;tRPC - A way to build entirely typesafe APIs without the need of schemas or code generation.&lt;/p&gt;

&lt;p&gt;If you are building an API today, there are two main approaches: REST and GraphQL.&lt;/p&gt;

&lt;p&gt;But there is actually another less talked about approach called tRPC with features like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Autocompletion&lt;/li&gt;
&lt;li&gt;Automatic typesafety&lt;/li&gt;
&lt;li&gt;Request batching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In REST Land the most basic way to fetch data inside a React Application is to use the browser fetch api, when the browser first mounts in &lt;code&gt;useEffect&lt;/code&gt; then manage the response in a &lt;code&gt;useState&lt;/code&gt;. This approach does not have a strong contract between the frontend and the backend: for example if you decide to change the message property from greeting to text, typescript will not throw an error, but in our React Application we get a broken UI.&lt;/p&gt;

&lt;p&gt;This is where tRPC comes in. To get started create a new project with npm create &lt;code&gt;t3-app@latest&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;tRPC is setup in routes and procedures, where a route is like a folder and a procedure is like a file.&lt;/p&gt;

&lt;p&gt;If you open up the exampleRouter, you will see a hello procedure with an input object where name is a string, and an output object where greeting is a string.&lt;/p&gt;

&lt;p&gt;To call the procedure in the frontend you have to use a special api object and navigate to the hello procedure by using the example router. Under the hood tRPC uses React Query to make the request&lt;/p&gt;

&lt;p&gt;Note that changes to the backend will throw typescript errors in the frontend.&lt;/p&gt;

&lt;p&gt;And additionally when making multiple requests, tRPC is going to batch all of them into a single HTTP Request.&lt;/p&gt;

&lt;p&gt;There are a couple of instances where you shouldn't use tRPC:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When your frontend and backend code is not written in TypeScript&lt;/li&gt;
&lt;li&gt;When you have separate backend and frontend teams&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the tRPC introduction in video form:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0DyAyLdVW0I"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>announcement</category>
      <category>devto</category>
      <category>community</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
