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
tRPC is for connecting your frontend and backend. if you need to get something from another backend inside of a procedure, then use fetch
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)
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.
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.
yeah but that means your request is going to your server which might not make sense in every situation
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.
yeah definitely that makes sense a lot of the time