T
tRPC

Detecting batch queries/mutations on the server-side

Detecting batch queries/mutations on the server-side

Bbetim6/8/2023
Hello all, Looking to leverage batching as a means to streamline some of our cross-service requests, both query and mutation. We have a graph of interconnected tRPC services, some of which form trees of calls where service A calls service B, and service B then calls service C. Looking to get some guidance on how to identify on the receiving side whether a particular method invocation is a part of a batch request so that I could then batch requests to a downstream service, eg. I receive 10 queries in a batch to service A, which I would then like to batch in a single call to service B, and so on. Specifically: 1. Is there a mechanism I could introspect to tell that the current method invocation (query or mutation) is part of a batch request? 2. Is there a mechanism I could use to get access to the input and context of the other requests of the same type in the batch? Thanks!
Nnlucas6/8/2023
The best/only way might be to have a singleton hosting your own batching layer, then have the batched procedures call through that and let it batch them up But batching is an optimisation which sometimes lands, not something you should expect to behave deterministically. If you want to do this I would write a procedure which receives an array of inputs and then you send your batch from the client in one call
Bbetim6/8/2023
thanks @Nick Lucas . I was hoping to avoid writing a batch-specific procedure, in addition to the single input use cases (both of which are relevant in different use-cases for us), but sounds like that isn't an option. I suppose if I'm gonna have to go down this route, one thing that would help is support for "overloading" in a TypeScript sense, in that I'd like to have a single named procedure (with two implementations) that supports a single input (producing a single output) as well as an array of inputs (producing an array of outputs). Is something like this supported in tRPC?
Nnlucas6/8/2023
Just use a different procedure name createItem createItems Overloads are nice, but JS/TS don't actually support them, it's always faked
Bbetim6/8/2023
would be helpful from a DX standpoint for us but I'll go ahead with the separate procs for each case. thanks again!
Nnlucas6/8/2023
Well you could use a Zod discriminatedUnion for the input and output, but TS wouldn't know which output matches to which input
Bbetim6/8/2023
yeah, that was one of the first approaches I was looking at but it foregoes the output type-safety I wanted ideally, something like this is the DX I was looking for https://www.typescriptlang.org/docs/handbook/functions.html#overloads for tRPC, using Zod-based schemas, instead of TS types, I'm sure something like it could be supported.. but I do realize it's a fairly niche case
Nnlucas6/8/2023
TS overloads only offer type safety for the consumer. The function itself has no idea what it’s been sent and has to check at runtime The only way to guarantee type safety on both sides is to use a different function name with its own impl

Looking for more? Join the community!

T
tRPC

Detecting batch queries/mutations on the server-side

Join Server
Recommended Posts
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?trpc.legacyRouterName.procedure "does not exist" on type CreateTRPCNextBase, runs anywayHave reached the end of a massive trpc 9 -> trpc 10 migration, executed using the official guide witNot getting legacy router ops in merged tRPC 9 routerFollowed the guide to merge my large tRPC 9 router with tRPC 10, but now I can't use useQuery or acccreateTRPCNext Type Error + useQuery Not DefinedI'm trying to use the conventions for `createTRPCNext ` from https://github.com/trpc/examples-next-pRead response headers on the clientIve been searching for a while trying to figure out how to access response headers on the client. FoIs it possible to access context in input?Hi, I have a validator that requires to get context of prisma in the `input`, Is it possible to get TRPCContextState not found in v10In our v9 app, we used TRPCContextState from internals to type out TRPC utils, importing it like `imRateLimiter for fastify tRPC routesHi, I would like to add Ratelimiter to a specific fastify trpc route, are there any solutions availaSubscriptionsare we able to send data over with subscriptions, to allow for full duplex communication ?Query tRPC the right wayIf you use graphql, you can precisely query what you exactly need and avoid over fetching, so you caHow does trpc subscription actively close/disconnect in the server?How do I actively disconnect my subscription?child router questionlet's say we have a monorepo, one server, two client apps (cat and dog). we make one appRouter, likHow to force SSL on projects using tRPC?Hello everyone, I'm working on a project that uses create-t3-app as boilerplate. My project is hosAWS Lambda / How to set Cookies inside ProceduresHow can I set and remove cookies similar to ctx.res.cookie(..) with Express when using the aws lambdMassive Type Errors on tRPC 10 Legacy Router against MergeRouterWe're trying to upgrade to tRPC 10 but I am running into a massive type clash when trying to merge rIs it possible to create 2 routers inside a single file?I am having an issue in which it is impossible to me to use a class instance within 2 routers. I triUse onError to change an application error into a TRPCError?I want to use the onError handler to change any instance of a custom application error into a TRPCEr