from the internet’s favorite T3 Stack
tRPC.io: Move Fast and Break Nothing.
tRPC is an interesting open-source project created by Alex based on a Proof of concept by Colin McDonnell (creator of Zod). tRPC has 22.4k+ stars on their GitHub repo and is considered to replace Rest APIs and GraphQL (Provided that you are using TypeScript on your back-end and front-end), but is it though? that’s what I will try to explain to you.
tRPC stands for TypeScript-Remote Procedure Call.
How it’s going to replace Rest APIs or GraphQL?
I don’t think it’s going to, since Rest APIs and GraphQL have their own advantages.
Rest APIs are simple like pages serving JSON data instead of HTML content.
GraphQL is an elegant Query Language for requesting data from the server, simplicity at its best, first define the Data Structure, and then simply ask for what your need.
tRPC on the other hand is not API per se, instead, it’s (as the name suggests) Remote Procedure Call where you have a router object on the server containing all the Procedures and their inputs tightly defined by Zod. The client then imports that router object and calls the procedures defined by the router object.
All the routes (procedures) are executed on the server, the client requests are wrapped in form of an API call by tRPC, and the response serialization and deserialization are implicitly handled by tRPC. Since these are TypeScript objects and there is another layer of Zod schema definitions, any changes on the server router object or the changes to the input schema will cause a build failure at the client end helping us to identify the errors at TypeScript build-time and not at the actual run-time when a user is using the application helping us to reduce runtime errors.
Why should you use tRPC?
- Type safety at the server and client end.
- Early error detection, reducing run-time errors.
- No need to share API document or any schema since tRPC and Zod takes care of it.
- Great Developer Experience (DX).
Wait, but What’s Zod?
Oops, my bad, I have used the word Zod many times before assuming you might be knowing it, and if you don’t, don’t worry I got you.
Zod is a schema-definition and validation library allowing you to tightly define your object schema using primitives given by Zod and handle invalid schema/objects gracefully using the validation syntax provided by Zod.
How Zod is different from the Type/Interface of TypeScript?
- Type/Interface does not allow you to add additional rules over types. E.g. TypeScript you can use
stringto define a string but Zod allows you to have something like this:
z.string().max(10).min(3);which will define a required string with a minimum of 3 characters and a maximum of 10 characters.
- Zod (since it’s a validation library) will also allow us to have custom exception messages if the object does not obey the defined schema.
- tRPC is a Remote Procedure Call from your Client Application to your Server Application where you can define the schema i.e. your router object with its procedures and required inputs.
- tRPC is great for avoiding runtime errors due to missing fields in the response or missing/updated routes on the server.
- tRPC is great and can be used alternatively instead of Rest API or GraphQL provided that you are using TypeScript at the Client and the Server.
- The Schema Definition in tRPC is handled by Zod, A schema definition and validation library.
I hope you enjoyed reading this article, if you do then please give me some claps, follow me, and share this article with your friends on social media.