NazCodeland
NazCodeland2y ago

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?
1 Reply
NazCodeland
NazCodelandOP2y 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