Help - Is it possible to use RouterInputs on server side calls without circular dependency? #5490
-
My app is getting rather large and I want to put my routers and functions in separate files since the router files are getting very large. It would be really awesome If I could use the RouterInputs on the server instead of having to name all my Zod schemas in a separate package. My Ideal type for Props below is a circular import without way around. the documentation for server side calls just has you enter the inputs twice but that doesn't seem like good practice. Ideal Solution:
Current solution:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Using https://trpc.io/docs/server/procedures#inferProcedureBuilderResolverOptions |
Beta Was this translation helpful? Give feedback.
-
I do exactly like you are doing. I also have to explicitly use inferProcedureBuilderResolverOptions which was added recently to get the context of a specific procedure. E.G: interface GetConfigOptions {
ctx: TProtectedProcedureContext; //This is just inferProcedureBuilderResolverOptions<typeof protectedProcedure>["ctx"];
input: TGetConfigInput;
}
export const getConfigHandler = async ({ ctx, input }: GetConfigOptions) => { ... } This works... okay. Until we have to do something like adding a middleware to the router: Then, We will not get strong typesafety for the context. export const appRouter = createTRPCRouter({
getConfig: protectedProcedure
.use((opts) =>
opts.next({
ctx: {
...opts.ctx,
somethingNewFromTheMiddleware: "I am hidden from the handler now",
},
}),
)
.input(ZGetConfigInput)
.query(async (opts) => await getConfigHandler(opts)),
}); I wonder, can it ever be possible to have a type helper that would allow for us to get the router options type without causing a circular dependency? Or is that really really not possible? This would allow us to have correct and fully typesafe handlers which would be huge. Not sure if it even is possible |
Beta Was this translation helpful? Give feedback.
Using
inferProcedureBuilderResolverOptions
in v11 you can kinda do ithttps://trpc.io/docs/server/procedures#inferProcedureBuilderResolverOptions