T
tRPC

One file per function call on the server side

One file per function call on the server side

Nnmeng6/12/2023
I'd like to structure my server with one file per function call -- is this an antipattern? If not, does anyone have an example repo of how to structure the server code with one function/rpc call per file? Do I have to have one router per function call?
Nnlucas6/12/2023
Just export your procedure from each file and then construct your routers by importing them What’s the challenge?
Nnmeng6/14/2023
Hey, thanks @Nick Lucas -- the problem is that the function is created using the object from initTRPC.create(); (i.e. we do const t = initTRPC.create(); then const app = router({ endpoint: t.procedure.input(...).query(() => { .... }), }); I'd like to put the whole t.procedure.... into a separate file, not only the function passed to .query() so would I from the endpoint file export e.g. const endpointFactory = (procedure) => procedure.input().query(() => { ... }) and then (in the app router) do router({ endpoint: endpointFactory(t.procedure), }) which seems ... over the top
Nnlucas6/14/2023
You can import t everywhere, and it's the only way to create a procedure because it's what contains all your types But export const listThing = t.procedure.query() is totally fine
Nnmeng6/14/2023
Alright, that's what I'll do! Thanks 🙏

Looking for more? Join the community!

T
tRPC

One file per function call on the server side

Join Server
Recommended Posts
Optimistic update on infinite queryNode 18.12.1 with pnpm trpc v 10.28.1 I have the following query to render a list of posts ```tsx Creating trpc context (express server) for testing without req and res objects.I have seen a few examples of tests with next auth or t3 stack that is using next-auth under the hooDoes trpc (t3 stack) disable ssr from Nextjs?Or is the ssr just not applied to the apiGetting data as "any"For some reason, probably after update I started getting following issue. My router is no longer typWhat is error formatingI understand error handeling but what is error formating , found it in trcp docs have'nt seen that twhere does opts.path and opts.type came from ? i guess by default it only stores ctx ,next and input```ts const loggerMiddleware = middleware(async (opts) => { const start = Date.now();   const rePassing objects to the next() function of the middle ware```ts // ------------------------------------------------- // @filename: context.ts // -------------opts object passed to mutation , i can not understand where it came from```ts export const authorizedProcedure = publicProcedure .input(z.object({ townName: z.string() })How do I log thrown errors from the query/ mutation on the server side globally?I was surprised to find that the server servicing tRPC requests do not log any thrown errors from a trpc with app dir nextjsHello im wondering how to use trpc with nextjs app router now?. I have t3 app but for now its a src/if I export 'appRouter' instead of 'router' I get errorhey everyone, I have this exact setup https://icflorescu.github.io/trpc-sveltekit/getting-started I React Native/Expo: Uncaught Error: No QueryClient set, use QueryClientProvider to set oneFound this issue but it just says to use patch-package, I was wondering if there was any changes sinAll values in DecoratedProcedureRecord are of type anyI've stumbled across an issue and I'm not sure how to start diagnosing it... Basically in my IDE tDetecting batch queries/mutations on the server-sideHello all, Looking to leverage batching as a means to streamline some of our cross-service requests,VS Code becomes unresponsiveHello everyone, I've started to experience huge performance issues with my project that's based onMiddleware-ish for clientHi! I had a quick question about client-side usage: I have users with long-running sessions, and iWhy cannot I specify a mutation input with useQueryKey?It's supported with queries, but for some reason not with mutations. Why is this the case?how to connect trpc server (t3 stack) from my external nextjs app?how to connect trpc server (t3 stack) from my external nextjs app?