[Proposal] tRPC as communication between front and back #140
JoaquinTrinanes
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
(I'm opening this as an issue because we don't have discussions enabled)
The rant
There are a LOT of things that can go wrong when communicating between front and back through an API. One of the most commons issue I've encountered was the typing system and schema validation. We can send whatever we want to whatever endpoint, and it's a responsibility of both the front and back team to coordinate and determine this.
There are many popular solutions for this:
During the past weeks, I had to do a HUGE change in one of my Next.js personal projects. What was originally a static site that read data from a CMS turned into an application that also provided CRUD access to a database, authorization, magic link sign-in, user roles...
I felt a little hopeless at first, as I don't really enjoy creating a file per Next endpoint, and then, per each file, add the following:
unknown
) was correct. That validation can be different depending on the HTTP method.Current situation
We don't use Next.js endpoint's at Vizz that I know of (except for specific cases like authentication), but the problems described above exist consuming an external API too. The backend team has to create a DTO to validate the data, the frontend team has to cast whatever arrives to something usable. And there's a Swagger somewhere (that being said, in comparison to my other jobs, you do a great work being faithful to the specification, even though I found some documentation without response type in the wild that I had to check by hand).
When the specification changes, the backend has to change both the spec and the DTOs. The front has to modify the interfaces they created to do their castings (usually from the spec) This is not ideal because
[object Object]
.There's got to be a better way, right? RIGHT?
Enter tRPC
The thing I like about tRPC is the first thing that appears when you open the page
![image](https://camo.githubusercontent.com/41819c2d3fbc23af06fbce2110383d8a90abf9aa8af9cd317652556c572743c7/68747470733a2f2f73746f726167652e676f6f676c65617069732e636f6d2f747270632f747270636769662e676966)
In a nutshell, tRPC provides inferred, type safe Remote Procedure Calls.
This is the workflow when creating a tRPC method (I'll use v9 syntax, v10 is on the way but still in beta):
This validation works with complex nested objects too (objects, arrays, sets, enums...)
Note how we never import server code in the client, we just import the
AppRouter
type. The created hooks are totally type safe now!There are a few things to note about the code above:
react-query
is used internallyhello
string has type hinting. In v10 of tRPC, objects instead of strings are used. This allows to clickgo to definition
and somehow being redirected to the backend code (with black magic or something)The proposal itself
Thanks for reading so far! Given our current project structure (monorepo) I think this can improve the quality and stability of our code, save time and make us work more efficiently. I have two proposals:
AppRouter
type, the only type we need in the client (adding an include path in thetsconfig
?) (edit: is this legal? it's much easier than I thought 😅)I think the advantages of this approach are huge and measurable, let me know what you think! :)
FAQ
Beta Was this translation helpful? Give feedback.
All reactions