T
tRPC

Would you recommend tRPC’s usage in this case?

Would you recommend tRPC’s usage in this case?

Bbenjaminreid11/1/2023
So we have two applications, one existing, one about to be started. These two applications will talk to an external API in the exact same way. The current application uses React Query to do so. I’m thinking about creating some shared library/SDK/service (whatever you want to call it) so these two applications can consume the API in the same way. I guess tRPC solves this quite well in some respects, have the tRPC client be it’s own package, have the two applications consume it. The procedures in the tRPC client are going to to be making fecth requests to this external API though, maybe transforming the data so it’s more suitable/consistent for the consumer applications. Anyone done anything like this? Or does anyone see this as being a good or really terrible idea?
Solution:
I think there's nothing wrong with creating a reusable piece of logic, but you say make the client its own package. Why the client? Personally I'd create a private package with only the common logic in it as an async function. No reference to even trpc. Then I'd install this package on both applications, and invoke the library function in a procedure somewhere in a router. This way you can easily: - Transform the results further. - Have completely unique trpc configurations. ...
Jump to solution
EEndgame101311/2/2023
I’ve personally never implemented something like this with tRPC, but it does sound possible. Personally, I’ve found monorepos and shared packages to be a pretty major headache, especially as projects begin to scale (handling different typescript configs, cjs/esm, build steps, etc.). If you want to keep things tightly coupled and are confident you’re gonna be dealing with only typescript clients for the foreseeable future, then I’d stick with tRPC for this for sure. If not, GraphQL would probably fit this quite nicely.
Speaking from experience, tRPC is perfect for BFF (backend for frontend), but trying to extend it out to non-typescript clients (via openapi or something of the like) can really be a pain. GraphQL, on the other hand, solves this problem a bit more elegantly, since it’s mostly language-agnostic. But it does come with its fair share of tradeoffs, too. Pick your poison.
EEndgame101311/2/2023
P.s. I refer back to this graphic from Theo Browne whenever I’m picking backend infra for a project:
No description
Solution
TSTamás Soós11/2/2023
I think there's nothing wrong with creating a reusable piece of logic, but you say make the client its own package. Why the client? Personally I'd create a private package with only the common logic in it as an async function. No reference to even trpc. Then I'd install this package on both applications, and invoke the library function in a procedure somewhere in a router. This way you can easily: - Transform the results further. - Have completely unique trpc configurations. - Have different trpc clients / links - Use the api call anywhere, even without trpc. - Extend the library with other API integrations in the future. - Have independent repos for the two apps and the library without any issues.
Bbenjaminreid11/2/2023
Thanks both for your input, that’s really helpful 👍🏻 Much appreciated 🙏🏻

Looking for more? Join the community!

T
tRPC

Would you recommend tRPC’s usage in this case?

Join Server
Recommended Posts
How to limit payload size?Is there a way to limit the payload size? E.g., if a particularly large JSON payload is sent to the Next.js app directoryHi. I have been able to set up tRPC with the app directory in Next.js. It was a frustrating experientrpc errorTrpc not working in next js 14 app router. Please help me to resolveFeature or happy accident: error message from `cause` being used as TRPC's error messageWhen a procedure throws a TRPC error with no message, it turns out that if the thrown error has a caWhen error in tRPC route, the error message is vaugeThere are no line numbers when a runtime error happens in a tRPC route. Surely I am missing somethinHow to have 2 separate trpc clients for http and websockets?I'm trying to have a setup where I can use trpc with 2 separate clients, one for normal HTTP requestHow to configure context for a standalone server?Are there any examples out there on how to set up context for a standalone server? The docs do incluDocs hard to follow with so much `next` codeNot having a great experience with the docs. There are many examples that use `next`, making it hardadding information to the QueryKey that is not part of the procedure inputwe have the situation that we have our trpc routes read some specific information from the browsesr How to make typed error responses in the context of a specific query?Each query has a typed (success) response, although it seems all queries have a shared type for the What's the negative code returned in error responses?What's the negative error code returned in errors responses and how is it meant to be used by the clTRPC File as InputHi can someone pls help me: https://stackoverflow.com/questions/77381516/nextjs-trpc-file-as-input`refetchOnWindowFocus` with RSC?Hi. Is there any way to have `refetchOnWindowFocus` work with server components? Seems like it onlyMulti useSubscription with trpc react -query integration failed to find second subusing trpc in nx monorepo, node version is 20 with yarn. backend is using trpc with express integrat