-
Notifications
You must be signed in to change notification settings - Fork 27k
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
with-apollo SSR example added. #21956
Conversation
}) | ||
|
||
return addApolloState(apolloClient, { | ||
props: {}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't you want to render the posts and use the data you fetched?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we are returning apolloClient
, which contains fetched data, and it adds all the posts to the apollo cache, we have at a client.
Then, inside the <PostList />
component, that data is queried and the apollo client provides it from the cache.
We are using the same technique in the SSG example to use fetched data.
return addApollState(apolloClient, {
props: {},
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I know that's not your responsibility, because you're just following the pattern already set, but that seems confusing. All other usage of getStaticProps
/ getServerSideProps
show forwarded the data to the component via props (hence the names). I wonder how many people are getting stuck on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I know that's not your responsibility, because you're just following the pattern already set, but that seems confusing. All other usage of
getStaticProps
/getServerSideProps
show forwarded the data to the component via props (hence the names). I wonder how many people are getting stuck on this.
I've adopted the same convention as above following the examples available, but if there are some better practice / more straightforward I'd love to hear about them!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We ran into this issue/decision on our project. Why isn't the best practice to just create an ApolloClient
inside gSSP
, fetch whatever and pass it as normal props to the page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the issue: you can't because functions are not JSONable, and what Next abstracts from us is basically a REST call (or something). But you don't have to right? fetchMore
is only sugar for passing just a cursor (and Apollo will fill the rest of the vars with whatever was there before). If you hydrate on the first render on the client side, you should still be perfectly able to fetch the cursor from your fetched data and pass it to fetchMore
. Really the only difference is that the first render will have cached Apollo Client data. This is the only connecting between Apollo and Next (with SSR). You fill the cache with whatever arbitrary data you want (most of the times, the first data the client will need). From the pages side, none of this matters, they just do whatever queries they need, and Apollo will have them in cache, they code stays the same. The only issue here (I think) is how to hydrate this state, and IMO it can be done with useApolloClient
(get the client, hydrate data, etc etc), right before you useQuery
. Of course you can abstract this away with a custom hook to reuse between pages. Something like useHydratedQuery
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that makes sense. You are right, making a custom hook for this purpose would be really beneficial. But adding such complex logic inside with-apollo
, wouldn't that be overkill for people who are just getting started. I think the current implementation is good, from the perspective of the example. We ain't creating a full-fledged app in here. Haha.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's also true. I mean if it works, it can't be that bad. But note that this current example (and the ones that existed before) doesn't point (IMO) in the best direction, which basically would be align with Next's way of thinking. With this said, it's not that complex, and I think anyone who is serious about Next will ask the same question we are asking and decide for themselves.
As for the hook, idk, it's as complex as what we currently have. Tbh I don't have a strong opinion to leave or remove, I'm just curious to see what core thinks (@leerob ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep same. Let's see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that both approaches work. I just worry about beginners seeing it and getting confused.
Maybe it just needs a comment to explain why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #21984 to improve the with-apollo
example. Thank you!
Hi! Not sure if this is the correct place, but is this PR deployed onto the demo link? There doesn't seem to exist the |
It's not deployed yet. Do we have the rights to deploy? I think only owners can do that. |
Got it! I guess I can just clone the repo and play around with it locally. Do we know who can deploy it? |
@leerob Can you deploy the latest version? |
I'm not sure who owns that URL. Best bet would likely be to either remove it from the README or a deploy a new one and update it 👍 |
with-apollo example shows how to use Apollo with Next.js using:
There was a lot of discussion over adding SSR example too Here
So, I have added ssr.js, which uses getServerSideProps, and also added a link in the header component to access that page.