Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rpc] An object for accessing procedures using typescript #4032

Open
MrOxMasTer opened this issue Nov 30, 2024 · 4 comments
Open

[rpc] An object for accessing procedures using typescript #4032

MrOxMasTer opened this issue Nov 30, 2024 · 4 comments
Labels
enhancement New feature or request rpc

Comments

@MrOxMasTer
Copy link

MrOxMasTer commented Nov 30, 2024

What is the problem this feature would solve?

This issue will be able to solve the problem

  • lack of hints when using @effect/rpc
  • implicit requests that you can't even guess about
  • To know which queries exist and which do not, so as not to accidentally make unnecessary ones or not to memorize each query which exists and which does not

Maybe a query to create using a class is fine, but how to maintain it remains a mystery to me

What is the feature you are proposing to solve the problem?

An approximate implementation:

class UserByEmail extends S.TaggedRequest<UserByEmail>()('UserByEmail', {
  failure: S.Union(
    S.instanceOf(UnknownDatabaseError),
    S.instanceOf(UserNotFoundError),
  ),
  success: selectUserSchema,
  payload: { email: userEmailSchema },
}) {}

export const userRouter = RpcRouter.annotations({ name: "userRouter", as: "user"  } | "user").make(
  Rpc.effect(UserByEmail, ({ email }) => Effect.<...>,
);

export type UserRouter = typeof userRouter;

const appRouter = RpcRouter.annotations({ name: "appRouter" }).make(userRouter, ...)

const handler = RpcRouter.toHandler(appRouter);
const resolver = RpcResolver.make(handler)<typeof appRouter>();

const client = RpcResolver.toClient(resolver);

// new function:
const caller = createCaller(client);

caller.users.userByEmail({ email })
caller.posts.postById({ id })
caller.fail()

What alternatives have you considered?

trpc:
image

@MrOxMasTer MrOxMasTer added the enhancement New feature or request label Nov 30, 2024
@patroza
Copy link
Member

patroza commented Dec 3, 2024

The current client already gives you the type hints to show what procedures you can call:
Screenshot 2024-12-03 at 08 43 48
I do however agree that named methods are nicer, and a missed opportunity, given that we already have unique tags.
(see below for my approach, which uses named methods)

I think having a single client for all rpc is not good, especially considering tree shaking.
If that's however not important in your use case, I do think It could be a neat opt-in feature.

My approach to this is slightly different:

One could easily build a unified client with this. const caller = { blog: clientFor(BlogRsc), account: clientFor(AccountRsc) }
It uses @effect/rpc under the hood, is layer based like HttpApi, and is imo an example of the flexibility of the current RPC implementation. I think you could consider the current RPC implementation low level.
Looking forward to have RPC grow to something higher level and better integrated

@gcanti gcanti added the rpc label Dec 5, 2024
@mikearnaldi
Copy link
Member

you might want to use the HttpApi module from platform instead, that gives you a client in the style you want

@MrOxMasTer
Copy link
Author

you might want to use the HttpApi module from platform instead, that gives you a client in the style you want

Question - is it possible to define HttpApi without url? Because in documentation I see only with url or the question - why I need to run a separate server, when I need only a convenient tool for working with requests, in the form of structure by business entities and middleware

@patroza
Copy link
Member

patroza commented Dec 8, 2024

@MrOxMasTer theres an example inside the tests.
But maybe you just want a RequestResolver or more what @FedericoBiccheddu does https://discord.com/channels/795981131316985866/1306739649875808349/1306747962537545759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rpc
Projects
None yet
Development

No branches or pull requests

4 participants