React hooks RFC #80
Replies: 3 comments 1 reply
-
I think I like variant 1 more, as you don't always have to add imports for a new hook. |
Beta Was this translation helpful? Give feedback.
-
Hi, so this RFC is about code-splitting the client? I don't see an issue with generating a method per operation. It's overhead but not much for 99% of cases. But from React Developer's perspective, I like variant 2 because it is very explicit and feels like "real" code generation :) We also need an imperative way to call the API. Currently, we do it like this const {refetch} = useQuery.Weather({ lazy: true })
refetch() I'd prefer variant 1 for the base client implementation and variant 2 for the new react interface. |
Beta Was this translation helpful? Give feedback.
-
TL,DR: 👍 smaller bundles, better performance, better caching, faster compile times I really like that idea - right now the Wundergraph Hooks API feels like it was not build with medium to large applications in mind. Dead code elimination is a great tool for performance and also a great DX as you can rely on the compiler to optimize the code automatically. Large applications often use hashes in their names for example The same issue slows also down bundle during dev and production builds as a change of any query would force the bundler to rebuild all files.. There are some edge cases which disables dead code elimination once you use common chunks optimizations and lazy loaded chunks. Splitting the generated code into several would also solve those issues. import { useWeatherQuery } from 'generated/hooks/useWeatherQuery'
const { data } = useWeatherQuery({ forCity: 'Berlin' }) Furthermore we could split A generated hook file might look like this: export const useWeatherQuery = (input, options) => useInternalQuery('weather', input, options) Splitting operations into files could also allow future performance gains for the wundergraph compiler as it allows parallel and incremental code generation. |
Beta Was this translation helpful? Give feedback.
-
Right now for each operation a method is generated in the web client and in the hooks implementations for React and Next.js. This creates a lot of overhead, and the generated code can't be code-splitted. Which could be a big deal in bigger projects, because you need to load the entire client in your client bundles.
There are a couple of ways that we could potentially improve this.
Using keys as method accessors instead of actual methods.
Another solution for hooks to make them tree shakeable is generating individual functions for each operation. This could also be a wrapper around hooks.useQuery(...).
This will keep the client and hooks lightweight in the production bundle, but still have full TypeSafety in development.
Beta Was this translation helpful? Give feedback.
All reactions