merging other routers to appRouter

Hey everyone, I am using the https://icflorescu.github.io/trpc-sveltekit package in my SvelteKit project. My question is not related to that package (at least I don't think it is) I figured I mention it since it might be related and I just don't know... I am wondering if I have this code,
// router/router.ts
import type { Context } from '$server/trpc/context';
import { initTRPC } from '@trpc/server';

export const tRPC = initTRPC.context<Context>().create();

export const publicProcedure = tRPC.procedure;
export const appRouter = tRPC.router;

export type AppRouter = typeof appRouter;
// router/router.ts
import type { Context } from '$server/trpc/context';
import { initTRPC } from '@trpc/server';

export const tRPC = initTRPC.context<Context>().create();

export const publicProcedure = tRPC.procedure;
export const appRouter = tRPC.router;

export type AppRouter = typeof appRouter;
and say I also have
// routers/user.ts
import { appRouter, publicProcedure } from '$server/trpc/router';

export const userRouter = appRouter({
list: publicProcedure.query(() => {
return [];
})
});
// routers/user.ts
import { appRouter, publicProcedure } from '$server/trpc/router';

export const userRouter = appRouter({
list: publicProcedure.query(() => {
return [];
})
});
am I able to merge the userRouter onto the appRouter directly within the /router/user.ts file?
B
Bonsaye308d ago
based on my research it seems like we can't, I've opted for this solution instead,
import type { Context } from '$server/trpc/context';
import { initTRPC } from '@trpc/server';
import { userRouter } from './routes/user';
import { postRouter } from './routes/posts';

export const tRPC = initTRPC.context<Context>().create();

export const publicProcedure = tRPC.procedure;

// whenever creating new routers, import routerFactory
export const routerFactory = tRPC.router;

// import into this file child routers and add them to appRouter
export const appRouter = routerFactory({
user: userRouter,
post: postRouter,
});

export type AppRouter = typeof appRouter;
import type { Context } from '$server/trpc/context';
import { initTRPC } from '@trpc/server';
import { userRouter } from './routes/user';
import { postRouter } from './routes/posts';

export const tRPC = initTRPC.context<Context>().create();

export const publicProcedure = tRPC.procedure;

// whenever creating new routers, import routerFactory
export const routerFactory = tRPC.router;

// import into this file child routers and add them to appRouter
export const appRouter = routerFactory({
user: userRouter,
post: postRouter,
});

export type AppRouter = typeof appRouter;
if this is incorrect in a way that I can't forsee please let me know hmm, that actually doesn't work for me, I am getting this error
9:45:45 p.m. [vite] Error when evaluating SSR module /src/server/trpc/routes/posts.ts:
|- TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

9:45:45 p.m. [vite] Error when evaluating SSR module /src/server/trpc/router.ts: failed to import "/src/server/trpc/routes/posts.ts"
|- TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

9:45:45 p.m. [vite] Error when evaluating SSR module /src/server/middleware/hooks.server.ts: failed to import "/src/server/trpc/router.ts"
|- TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
9:45:45 p.m. [vite] Error when evaluating SSR module /src/server/trpc/routes/posts.ts:
|- TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

9:45:45 p.m. [vite] Error when evaluating SSR module /src/server/trpc/router.ts: failed to import "/src/server/trpc/routes/posts.ts"
|- TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

9:45:45 p.m. [vite] Error when evaluating SSR module /src/server/middleware/hooks.server.ts: failed to import "/src/server/trpc/router.ts"
|- TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
TypeError: Cannot read properties of undefined (reading 'query')
at eval (C:/Users/o0/source/projects/mine/Barbercate/src/server/trpc/routes/posts.ts:6:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async instantiateModule (file:///C:/Users/o0/source/projects/mine/Barbercate/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.0/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
hmm inside my post.ts file
// routers/post.ts
import { routerFactory, publicProcedure } from '$server/trpc/router';

console.log('routerFactory', routerFactory);
console.log('publicProcedure', publicProcedure);
export const postRouter = routerFactory({
greeting: publicProcedure.query(async () => {
return `Hello tRPC v10 @ ${new Date().toLocaleTimeString()}`;
})
});
// routers/post.ts
import { routerFactory, publicProcedure } from '$server/trpc/router';

console.log('routerFactory', routerFactory);
console.log('publicProcedure', publicProcedure);
export const postRouter = routerFactory({
greeting: publicProcedure.query(async () => {
return `Hello tRPC v10 @ ${new Date().toLocaleTimeString()}`;
})
});
both of the console.logs returrn undefined, which is odd ahh, I think there is a circular dependency going on here ... router.ts is importing the child routers which are importing variables declared in router.ts so they haven't been initialized since router.ts code hasn't run yet so they are undefined -- just taking a guess
More Posts
Can I get non-redacted INTERNAL_SERVER_ERRORS in production?I'm trying to debug crashes in production, and having a hard time. As far as I can tell, errors are Error handling: Zod errors and manually thrown TRPCErrors have different shapeIf zod validation fails, the client-side `error.message` contains a JSON encoded array of errors, buprefetch() within getServerSideProps does not provide data upon manual refreshingWithin getServerSideProps: `await ssh.user.getDepositInfo.prefetch(userId);` `await ssh.user.getFQOHow to modify existing cache data?I have a message queue being fetched with InfiniteQuery requests. When adding a new message, I want When using createServerSideHelpers can the context be typed as GetServerSidePropsContext?I assume the reason we should recreate the context when using createServerSideHelpers is because theQuery data is undefined for a bit and is then populated. How to use with React State?I have an asychronous submit function that's called upon a button press. It refetches the query witshould we use tRPC for handling form submittion or not?Hey everyone, I've started using tRPC for my API endpoints and my project is based in Svelte/SvelteKWhat's the benefit of using the context instead of a direct import for the database connection?I've wondered why I should use the tRPC context instead of just importing my database singleton fromUsing both tRPC React client AND tRPC React Query clientHey all! We are successfully using both the React client and the React Query client. We do this becaTRPCClientErrorlike no name propertyQuick question: Why doesn't TRPCClientErrorLike have the name property? It is called ErrorLike, so IapplyWSSHandler yells at context error in expressI used the example code from docs. My applyWSSHandler code is: ``` const handler = applyWSSHandler({How to use the errorFormatter when using an adapter?The example here uses `context.create()`, which isn't something you do when using an adapter. I donOne file per function call on the server sideI'd like to structure my server with one file per function call -- is this an antipattern? If not, dOptimistic 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