Skip to content

feat: Split procedure definition and implementation #3699

Closed as not planned
Closed as not planned

Description

@ilijaNL

Describe the feature you'd like to request

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

// definition
const appRouterDefinition = definition({
  greeting: publicProcedure.input(z.object({ name: z.string() })).output(z.object({ text: z.string() })),
});


// implementation
const appRouter = router(appRouterDefinition, {
  greeting: (req) => {
    const { input } = req;

    const input: {
      name: string;
    };

    // this should match the .output type
    return {
      text: `Hello ${input.name}` as const,
    };
  },
});

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!

Activity

  1. ilijaNL commented on Mar 12, 2023

    @ilijaNL
    Author

    Any thoughts @KATT ?

  2. Nick-Lucas commented on Mar 12, 2023

    @Nick-Lucas
    Contributor

    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"

  3. ilijaNL commented on Mar 12, 2023

    @ilijaNL
    Author

    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?

  4. Nick-Lucas commented on Mar 12, 2023

    @Nick-Lucas
    Contributor

    Yes but the API would have to consume this too, and baseProcedures are usually very tied to the API setup

  5. ilijaNL commented on Mar 12, 2023

    @ilijaNL
    Author

    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.

  6. Nick-Lucas commented on Mar 12, 2023

    @Nick-Lucas
    Contributor

    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

  7. ilijaNL commented on Mar 12, 2023

    @ilijaNL
    Author

    Okay, we will continue there :)

  8. locked as resolved and limited conversation to collaborators on Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions