leptos icon indicating copy to clipboard operation
leptos copied to clipboard

Generic Server Fn

Open sjud opened this issue 10 months ago • 4 comments

Hello,

I know there is another generic SeverFn PR open https://github.com/leptos-rs/leptos/pull/3008 but I decided to try my hand at it anyways since it's been something I've wanted for a while. I had needs that were not covered by the other PR. I hope I am not stepping on anyones toes @rakshith-ravi .

This PR covers implementing generic server functions without touching any non-macro code. Specifically I've added an attribute for registering the types of a particular generic function and extending the macro path logic. Instead of impl<T> ServerFn for SomeStruct<T>, for each registered type set the macro generates a specific impl ServerFn for SometStruct<SpecificT>.

My implementation is generic over server function arguments (the focus of the other PR) as well as, generic over non input types. For any type that isn't in the input of the server function, we add a _marker type with PhantomData<T,..,U> with the set of non input types.

It's also generic over the T in Result<T,ServerFnError<E>>, although not E (it'd be very easy to add that though.)

Something I also wanted which is kind of weird is a way to specify a generic server functions in the front end over a type that can only exist on the backend. This will let us (when generic components are expanded), build a component that is generic over a backend implementation. So you could have a GraphComponent which is generic over it's data source, and it uses the same server function to compile data from different data sources which are specified in the client code.

I've introduced some macros to build shims between generated front end types and the SSR only backend types, and logic within the server macro to map those client side types into their actualized server types.

I also refactored the server function macro, which I hope was OK. I wasn't able to perform any edits on it without doing a big (and yet painless) refactor.

I've added some examples to show off the the new code. This PR is based on my RFC (which got no comments lol) https://github.com/sjud/server-fn-generic-rfc , which is out of date since I wrote it before the implementation of it but covers what I was going for in more detail.

Best, Sam

sjud avatar Dec 21 '24 20:12 sjud

Overall these seem like positive comments and I'm glad that the PR has passed a "vibe check" which was something I needed to know before I put more work into it. Thanks!

sjud avatar Dec 27 '24 21:12 sjud

I have not yet looked at the implementation changes in server_fn_macro/src/lib.rs. As it's quite a big diff, I thought I'd take a look at the API (thanks for the extensive examples!) under the assumption that the implementation will probably change a bit, and I can review that once I'm ready.

It's been long enough since I've taken a look at the internals of the #[server] macro that it will probably take me a while to understand and keep track of the refactor relative to how it works.

I'll note that the CI is failing because of the unused variables etc., which also surfaces a number of todo!()s in the macro code, which should be resolved before this is ready as well.

For what it's worth, the refactor mostly just took the logic out of the main macro and put it in a bunch of functions but I don't think it changed the overall control flow or the logic (that much). There were some places that needed to be extended for generics. This is definitely a first pass but I basically just pulled the logic into seperate functions and then turned the macro into an if statement "if this is a generic do the new functionality" if not "do the old server function macro".

sjud avatar Dec 27 '24 21:12 sjud

Hey Greg! I'd love to push this to completion but I think I need a little more feedback before I do so. I appreciated your initial thoughts but I need some more concrete things that I can itemize into a TODO list before moving going forward. There are a couple things you mentioned already, was I suppose to do those things and then continue this process? Thanks

sjud avatar Feb 03 '25 17:02 sjud

Hey, sorry! I think I've responded to your questions on the particular comments.

I'd suggest -- resolving those with any changes (mostly removing the default generic I think), and then cleaning up the linting issues so CI will run.

Then I'll try to read through the implementation all at once and let you know :smile:

gbj avatar Feb 06 '25 00:02 gbj

@sjud I tried my stab at rebasing my PR but gave up halfway through. Will come back to this another day with a clear head. If you're able to figure this out, I'd be happy to close my PR instead.

If you're up for it, I'm happy to work with you on this PR as well

rakshith-ravi avatar Apr 18 '25 20:04 rakshith-ravi

Oops sorry I haven't driven this forward to completion! I suddenly became very busy. I'm planning on working on this more but not sure when I'll get to it. @rakshith-ravi Thanks! I'll take a look and see what I can do, I appreciate your support.

sjud avatar Apr 24 '25 13:04 sjud

No worries :D

Let me know if I can help in any way at all

rakshith-ravi avatar Apr 24 '25 14:04 rakshith-ravi