Tuxer
Tuxer
TtRPC
Created by Tuxer on 5/14/2023 in #❓-help
How to pass context to vanilla client?
Hi, I have a next app where I use trpc. Now I need to call some trpc functions from outside of any components. My first solution was to just call the endpoint with axios. const { data: attribute } = await axios({ method: "GET", url: getApiServerUrl() + "/trpc/db.getAttribute", params: { input: JSON.stringify(trpcInput) }, headers: { 'Cookie': cookie } }); This is working fine, but I lose typing support. My new solution is to use the vanilla client. But using the vanilla client I can't find a way to pass context to the request, for example to pass a cookie. Now my walkaround is creating a getter function that each time creates a new trpc client with the context needed. export const getClientWithCookie = ({cookie}: {cookie: string}) => createTRPCProxyClient<AppRouter>({ links: [ httpBatchLink({ url: getApiServerUrl() + /trpc, headers() { return { cookie: cookie, }; }, }), ], }); const attribute = await getClientWithCookie({cookie: cookie}).db.getAttribute.query(trpcInput); This is also working fine, but feels a bit hacky to me, because each time it creates a new client. How would be the proper solution to call a trpc function with context like a cookie using the vanilla client? (I can't use hooks here)
27 replies
TtRPC
Created by Tuxer on 12/6/2022 in #❓-help
use tRPC for RPC calls instead of gRPC
Hi, I know that tRPC and gRPC are different things - despite the similar name. Nevertheless I would like to hear your opinion on using tRPC for things you would normally use gRPC for. What are possible use cases? What are the pros and cons. use case: call a remote render server to render assets pros: - You can keep using your zod typings and don't have to recreate them for all needed gRPC calls. cons: - Both tasks need to be running on typescript or you need at least to create a typescript wrapper for that task - gRPC has much lower data usage
2 replies
TtRPC
Created by Tuxer on 11/20/2022 in #❓-help
query and mutate promise on next
I've followed the guide "Usage with Next.js" and now I only have the useQuery and use useMutation hooks exposed when I want to call a route. How do I also expose the regular query and mutate promises on these routes? Edit: Solution:
const utils = trpc.useContext();

// use it (for example in an event handler)
const someResult = await utils.client.someRouter.someProcedure.query(/* args maybe */);
const utils = trpc.useContext();

// use it (for example in an event handler)
const someResult = await utils.client.someRouter.someProcedure.query(/* args maybe */);
10 replies