You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: Split procedure definition and implementation #3699
Hello, first thanks for this library and the general idea how to write full type-safe apps.
Currently the tRPC server includes the definition of all procedures + the implementations of all of those procedures. I understand the design choice that is made however because of this, tRPC is limited to only some project structures, see #1860. However the integration, especially in existing projects, can be improved by a lot if it the trpc server definition is splitted from the implementation. It is important to note that the output should be explicitly defined, hence not relying on return type inference.
Describe the solution you'd like to see
// definitionconstappRouterDefinition=definition({greeting: publicProcedure.input(z.object({name: z.string()})).output(z.object({text: z.string()})),});// implementationconstappRouter=router(appRouterDefinition,{greeting: (req)=>{const{ input }=req;constinput: {name: string;};// this should match the .output typereturn{text: `Hello ${input.name}`asconst,};},});
appRouterDefinition can be easily shared, in form of a npm module or in monorepo. This module only has dependency on trpc and zod (or any other supported validation libraries).
Describe alternate solutions
Additional information
No response
👨👧👦 Contributing
🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Procedures carry a lot of API coupled information (think AuthMiddleware, Context, etc) so I don't think this could work because a large chunk of the implementation still has to live with the "interface"
Could you give me an example of this? As far as I know the trpc client only knows/cares about the interfaces of input and output from a procedure or am I wrong?
Yes but the API would have to consume this too, and baseProcedures are usually very tied to the API setup
Yes the consume part on the server is tricky and it is currently not possible. For more context think about graphql when doing code-first vs schema first. However instead of a schema, this could be simple a set of procedure definitions(with little bit validation logic). The server implements the defintions and the client consumes those. Additional benefit is that the validation logic could be reused on the client side.
Okay, well this is a duplicate of #3798 so I'm going to close this just to keeps things tidy. There's some more full reasoning on that thread why this is unlikely to happen, but I don't believe there's any firm decision from the core team
Activity
ilijaNL commentedon Mar 12, 2023
Any thoughts @KATT ?
Nick-Lucas commentedon Mar 12, 2023
Procedures carry a lot of API coupled information (think AuthMiddleware, Context, etc) so I don't think this could work because a large chunk of the implementation still has to live with the "interface"
ilijaNL commentedon Mar 12, 2023
Could you give me an example of this? As far as I know the trpc client only knows/cares about the interfaces of input and output from a procedure or am I wrong?
Nick-Lucas commentedon Mar 12, 2023
Yes but the API would have to consume this too, and baseProcedures are usually very tied to the API setup
ilijaNL commentedon Mar 12, 2023
Yes the consume part on the server is tricky and it is currently not possible. For more context think about graphql when doing code-first vs schema first. However instead of a schema, this could be simple a set of procedure definitions(with little bit validation logic). The server implements the defintions and the client consumes those. Additional benefit is that the validation logic could be reused on the client side.
Nick-Lucas commentedon Mar 12, 2023
Okay, well this is a duplicate of #3798 so I'm going to close this just to keeps things tidy. There's some more full reasoning on that thread why this is unlikely to happen, but I don't believe there's any firm decision from the core team
ilijaNL commentedon Mar 12, 2023
Okay, we will continue there :)