-
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
Minimal apollo example #387
Comments
Some observations: this approach doesn't go deep into the components to load all graphql queries it encounters (something you can enable server-side with react-apollo). I believe this is a bit problematic with next.js: you aren't really meant to load data deep in the component hierarchy - if you want it to happen both on the client and on the server-side. There is just one point to load the data: in getInitialProps in the root component. I don't know if this is going to change in the future, but if not, then we will have to
In both cases, the approach above should be fine for the data being loaded in getInitialProps. |
Aand if some core developer likes this, I can create a pull request with the example. |
About getInitialProps only called at root, see #192. Would love to have your ideas there. |
@sedubois what problems were you running into with |
@nmaro your https://github.com/nmaro/apollo-next-example is empty. @amccloud better to ask @nmaro about that (I still need to get back into the code). |
Thanks @sedubois it's now online (always forget to run |
Oops, I mentioned to the wrong person. @nmaro what issue did you have with react-apollo? |
The data was loaded in the server, then as soon as the client started loading the page was empty again. I then looked at @sedubois's implementation (https://github.com/RelateMind/relate), and thought it's already quite complex for a quick proof-of-concept, so I finally tried with the lower-level api. |
@stubailo since you were wondering why it's so hard to integrate apollo with next.js - it looks like the only place you can fetch data both on the client and on the server is at the page's root component inside an async function called getInitialProps. I think the usual way to integrate react-apollo would only be useful on the client side. |
Interesting - are there any other data integrations with Next.js? It looks like using Redux is also pretty hard based on the examples I've seen. Most modern data systems have some sort of global cache (Redux, Apollo, Relay) so I feel like there needs to be some sort of facility in Next to enable this. |
Absolutely. We have a Redux example on the wiki, we need to create more like those :) |
It's not something we have to do on a release basis btw. We can just write a wiki tutorial at any time. |
Btw @nmaro that example looks really neat, thanks for contributing. We can take that as a base and expand it |
Oh, odd - I didn't realize the issues involved. @nmaro what is it about react-apollo that makes things hard? seems like you should be able to follow the redux example almost exactly but do I'd love to work with someone to make a minimal example. This is our "hello world" example for React, it would be great to have a port for Next.js: https://github.com/apollostack/frontpage-react-app |
@stubailo I'd love to work with you on a minimal example. I've been using the universal apollo microframework, Saturn, for a couple projects and would love to port them over to Next.js + Apollo ultimately :) |
Nice - yeah just making minimal modifications to the frontpage app to make it run on next.js instead of create-react-app would be my preference. then we can list it on our home page as well! |
A small issue was that data was loaded and rendered on the server, only to be replaced with nothing when loading on the client - I guess I just don't know apollo and next enough to get it right. Using apollo-client directly I didn't have this issue. What's more tricky for server-rendering is if you have queries deeper in the hierarchy. React doesn't have a way to render things asynchronously, i.e. wait for each component to be ready before rendering it. Which means a ssr framework either has to
Now the question is whether apollo has a way to detect all the data calls that will be needed to render a component tree, and do this all in one function call that can be provided to getInitialProps. |
@stubailo Is there a solve for this? ^ |
@nmaro @ads1018 have you seen |
BTW I wonder if things can be simplified now that #301 is merged. Haven't looked into that yet. |
@sedubois I checked that out thanks for sharing! Yeah, I imagine your example using react-apollo can be simplified with the new programmatic API (#301) that was just merged into Master so that you don't have to wrap all page components with your own HOC. If you make any progress on that, please let me know! Would be cool to get a next.js example on the apollo homepage :) |
Has anyone had any luck getting react-apollo working with the new 2.0.0-beta.2 release? |
@sedubois @stubailo I pushed up my attempt at next + react-apollo if you want to take a look. You can find it here: https://github.com/ads1018/frontpage-next-app One issue I'm facing right now is that components are only getting rendered client side and not server side. Perhaps we can use react-apollo's Would love to eventually include this hello world example inside the Next examples folder and the Apollo home page. |
The only pre-requisite for server-rendering the data is that it's returned as an object in |
Gotcha. I think this is a bit difficult with react-apollo because as @nmaro pointed out:
|
I've got a working example of React Apollo and Next 😄 🚀 I hope many of you find it useful. You can check it out here: https://github.com/ads1018/next-apollo-example (I've also deployed a demo using Now.) I ended up using a HOC inside my page called Thanks @bs1180 and @rauchg for pointing me in the right direction. If you'd like to add a |
Thanks @ads1018 😊 Compared to my example https://Relate.now.sh, does this example solve the issue of using Apollo in deeply nested components (avoiding the cascade of getInitialProps)? Maybe the example should showcase that as it's the main pain point. And I'm sure adding this to the examples folder would be very appreciated. |
Thanks @ads1018, things work great with the fixes in adamsoffer/next-apollo-example#2 🎉. I updated my example as well: https://github.com/RelateNow/relate |
Nice work, @ads1018 @sedubois! I've been following along on this and #192, I've also been investigating prefetching/async views using Apollo and vanilla React. Have you noticed or do you anticipate any performance issues with running Real nice solution 👍 I think rendering twice is the only option to ensure all child data is cached, just curious what you guys think about performance. |
Hey @estrattonbailey - I haven't noticed any performance issues and I don't anticipate any. In fact, it's super snappy! As for running |
@ads1018 some ideas for your example:
|
@ads1018 Great to hear! Nice little demo. What I meant to ask is: how well will this scale? Though I haven't used Next yet, as I understand it, Next calls The project I'm working on is a large scale editorial site, so I hope to do some benchmarking of different approaches, including yours. Would love to discuss more on twitter if you'd like. Thanks for your work! |
@estrattonbailey Gotcha. I imagine it will scale very well. For the initial page load, Side note - if you're concerned about initial page load time due to lots of components and data being requested on a page you can always tell apollo to intentionally skip specific queries during SSR and offload them to the client by passing I'll connect with you on twitter if you want to discuss further :) |
Important to keep in mind |
@ads1018 @estrattonbailey AFAIK there are still indeed 2 renders server-side on first page load: getDataFromTree gets executed and renders the whole tree internally, then the render is called again to construct the HTML response. Don't think if there's any way to avoid that, but I guess is still quite performant thanks to the network round-trips avoided by SSR. I guess the performance is maximum when the GraphQL server is hosted on the same machine as the Next.js server, so you could always try that if you'd be concerned with performance (at this point, I prototype my app with Graphcool for the backend, while the Next.js is deployed with Now/Zeit World). |
@sedubois @estrattonbailey Correct me if I'm wrong, but we are still only rendering once. |
@ads1018 AFAIK and looking at the Apollo code, it does render the tree recursively (just to trigger the Apollo data-fetching). So that's 2 renders server-side on first page load. |
But anyway, thanks to your demo, now we have a usable integration between Apollo and Next, and remaining questions about Apollo SSR performance don't have anything specific to Next.js anymore I think. I'd suggest to ask questions about that over there. |
@sedubois what is a render anyhow? I'd call it walking and shaking the tree. Seems to be optimized pretty well – suppressed |
We should get a blog post about Apollo + Next.js on the Apollo blog! |
@stubailo @ads1018's example is great 👏 For something bigger using same Apollo principles, can check my app: https://github.com/relatenow/relate |
Thanks @helfer. I'm thrilled with how it came out. I feel like I discovered the holy grail of app development with Next.js + Apollo. I've been meaning to follow up with a blog post in an effort to spread the gospel but just haven't gotten around to it. @stubailo I'd be happy to collaborate on a piece on the Apollo medium publication :) |
Huge shout out to @sedubois for helping out with the example and his sweet app. 😄 |
@ads1018 would love to get you on the blog. When you're ready to chat about it, ping me (thea) on Apollo Slack. :) |
@helfer You are totally right. I should do a new issue pass to see if issues can be closed 😄 @stubailo @theadactyl awesome idea ❤️ |
Anyone know of an issue/PR to watch - regarding requesting data twice server side? Just curious |
It turns out apollo integration is much easier when using apollo-client directly instead of react-apollo.
Here's the code: https://github.com/nmaro/apollo-next-example
And here's a running version (at least for as long as I keep the graphql server online): https://apollo-next-example-oslkzaynhp.now.sh
The relevant details are here:
apollo.js
then in a page
The text was updated successfully, but these errors were encountered: