tRPCttRPC
Powered by
KrangaK
tRPC•4y ago•
2 replies
Kranga

Is it possible to call a query from another query defined in the same router?

Say I have some code like the following:
t.router({
    compute_all: t.procedure
        .query(async ({ctx, input})=>{
            //Receives an array of computable objects and applies the transformation defined in compute_one
            const result = await Promise.all(input.datasets.map(x=>/*SOMETHING_SHOULD_BE_HERE*/));
            return result;
        }),
    compute_one: t.procedure
        .query(async ({ctx, input})=>{
            const result = '...';//Fetches data and does some transformation on it
            return result;
        })
})
t.router({
    compute_all: t.procedure
        .query(async ({ctx, input})=>{
            //Receives an array of computable objects and applies the transformation defined in compute_one
            const result = await Promise.all(input.datasets.map(x=>/*SOMETHING_SHOULD_BE_HERE*/));
            return result;
        }),
    compute_one: t.procedure
        .query(async ({ctx, input})=>{
            const result = '...';//Fetches data and does some transformation on it
            return result;
        })
})

Is it posible to reuse the
compute_one
compute_one
query logic within the
compute_all
compute_all
query without extracting the common logic outside the router?
I have tried doing it via instantiating a client using that same router and executing the query as usual from within the query but I get
'default' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
'default' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
due to the router=>client=>router circular reference.
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Is it possible to call one procedure in another?
TaylorFayTTaylorFay / ❓-help
4y ago
How do you call a router procedure from another router procedure?
chicoCchico / ❓-help
2y ago
Is it possible to split the router definition with the imlementation?
BarakondaBBarakonda / ❓-help
3y ago
is it possible to use trpc without the app router?
tltlTtltl / ❓-help
2y ago