trpc

I am new to trpc and I was wondering if I should use it in case I have a request to another backend and I don't use NextJS or prisma as my server side. would it be good idea to use the fetch() function in the trpc procedure or it is over engineering?
7 Replies
Sturlen
Sturlen17mo ago
tRPC is for connecting your frontend and backend. if you need to get something from another backend inside of a procedure, then use fetch
Tom
Tom17mo ago
ill quickly add using tanstack-query (which trpc is based on) might be a good idea (in combination with fetch). it adds caching, refetching logic, and loading states to the request so its. anice pattern (most of the time)
bol__glowy
bol__glowyOP17mo ago
so people do that? to use a fetch() function inside the query in tRPC procedure? In this case, we will do 2 requests: first to NextJS backed (trpc) and the second is trpc to my other backend. while if I use fetch in my component I have only one request. so I was wondering that trpc might not be the ideal solution for this case and I need some opinions.
Ramsay
Ramsay17mo ago
I personally call external apis from trpc procedures too. That way I can get the react query hooks for free. I also still get typesafety as I parse the response with Zod.
Tom
Tom17mo ago
yeah but that means your request is going to your server which might not make sense in every situation
Ramsay
Ramsay17mo ago
True. For all my use cases so far I end up needing an API key for the external apis, so having it go through my server also allows me to protect those keys.
Tom
Tom17mo ago
yeah definitely that makes sense a lot of the time
Want results from more Discord servers?
Add your server