-
Notifications
You must be signed in to change notification settings - Fork 36
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
Static fragments on Client Components #27
Comments
Hi there, I love the experimentation and the PR :) Fragment colocation is definitely something we want to give some more of a platform.
|
@amannn first of call, I love this pattern and highly recommend it so I'm happy to see you using it. In fact, its a pattern I use all over our Apollo Spotify demo. While we recommend the separation between client and server components, I think fragments make a ton of sense for client components to declare their data needs so that server components know what to include in their queries. This provides a nice data handoff between server and client components. I was surprised that static members don't resolve properly, but I guess that would make sense if those are lazy. I'm curious, do you know how non-default exports are resolved? My first intuition was to just Regardless, I would love to explore this a bit more and allow this pattern if its something Apollo itself can provide. This pattern is super powerful and I encourage it as much as we can (in fact, I'd love to make sure our docs emphasize this pattern much more than they do). I don't know all the limitations we run into with bundlers and whatnot though so this could be difficult. Thanks for raising this concern! |
Thank you @phryneas and @jerelmiller for your quick replies! Really encouraging to hear that you're putting more focus on colocated fragments, I think especially with TypeScript being pretty much an industry standard by now, this is a really good call!
Unfortunately, that causes the same problem! Everything imported from a client boundary is available as
Hmm, I read this part in the docs. I'm wondering from which perspective this is desirable? Is this seen as a necessity to work within the technical constraints we have or does it solve a problem for users or developers? Here's a practical example for a component that can be used for updating a user profile: 'use client';
import CountrySelector from './CountrySelector';
function HighlyDynamicUserProfile({countries, user}) {
const [country, setCountry] = useState(user.country);
// ...
return <>
{/* ... */}
<CountrySelector countries={countries} value={country} onChange={setCountry} />
</>
}
HighlyDynamicUserProfile.fragments = {
countries: CountrySelector.fragments.countries
} Here I think it would be reasonable to fetch
Interesting point. That's true, however as soon as you add interactive React features to a "shared" component like
That might be true, yes. I was kind of hoping that this pattern could be considered at this stage, as RSC bundlers are being created currently and Next.js has the only one that is known to be stable AFAIK. |
Yeah, I fear that might not be the best angle going forward - the React team is pretty set on having that a "file-level" switch, and I can understand it as everything else will make things a lot more complicated.
[...]
Yeah, this would be the rare example where it makes sense - but also I have to admit that this is a pattern I still have to wrap my head around a bit. |
@amannn I just had an alternative thought - you wouldn't even need to move the fragment out of the Client Component file if you used the |
I'm doing some housekeeping so I'm closing some older issues that haven't seen activity in a while. |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better. |
Hey! Really exciting to see Apollo experimenting with RSC support in Next.js! 👏
I'm using a Relay-esque approach with Apollo Client, where components define fragments so that data requirements can be colocated with the code that consumes them and to reliably orchestrate data dependencies upwards a component hierarchy.
I noticed this works as long as the components that define fragments are Server Components, but since imports from Server Components to Client Components create a lazy reference, the static fragments are no longer resolvable.
Based on one of the examples in this repo, I've added this commit for demonstration: amannn@fa8c9d3
This is not really a limitation of Apollo, but of the RSC model I guess. I was wondering if this is something that you're interested in supporting and if there's something that can be done about this? The only thing that comes to my mind is a compiler that resolves the static parts at build time, but I guess ideally there would be 1st-class support from the RSC bundler for this.
Many thanks! 🙌
The text was updated successfully, but these errors were encountered: