71: Joe Savona on Relay and Data Fetching with Suspense
In the immortal words of TLC, "Don't go chasing waterfalls".
Turns out they're really easy to find in React today.
Joe Savona joins us to talk about avoiding waterfalls in your React code.
He's here for our 2nd of 6 React Conf 2019 interviews to talk about data-fetching strategies with Suspense.
Joe has spent the last year working with the React team in developing a relay-suspense integration for the new facebook.com.
He shares the lessons and patterns they've learned and how they're baking great user experience into Relay.
Fetch-on-render, fetch-then-render, and Render-as-you-fetch are patterns you'll be hearing more often as Suspense picks up steam.
Thankfully, in today's episode Joe helps us navigate that verbiage and determine what our first steps with Suspense should be.
Featuring
Links
- Data Fetching With Suspense In Relay | Joe Savona — Joe's React Conf 2019 talk on Relay Hooks and Suspense at Facebook
- Building The New Facebook With React and Relay | Ashley Watkins — Ashley's talk on Relay at Facebook
- Selective hydration
- progressive hydration
@defer@stream@module
- Relay — A JavaScript framework for building data-driven React applications
- Relay Hooks — Experimental Concurrent Mode APIs
- Relay Hooks: A Step By Step Guide
- Concurrent Mode API Reference
SuspenseSuspenseListuseTransitionuseDeferredValue
- Data fetching patterns
- Relay Runtime
- Relay Compiler
- Tagged template literals
- GraphQL Fragments
- Sophie Alpert — React team manager emeritus
- Yuzhi Zheng — Manager of React and Relay teams
- Hack — Programming Productivity Without Breaking Things
Sponsors
Linode
Instantly deploy and manage an SSD server in the Linode Cloud, with your choice of Linux distro, resources and location. linode.com/react
Clubhouse
Clubhouse (now Shortcut) is project management that brings everyone, on every team, together. shortcut.com
Transcript
[00:00:00] Michael Chan: This is React Podcast. I'm Chantastic. In the immortal words of TLC, don't go chasing waterfalls. Turns out, they're really easy to find in React today. Joe Savona joins us to talk about avoiding waterfalls in our React Suspense code. He's here for our second of 6 React Conf 2019 interviews to talk about data fetching strategies in Suspense. He and the rest of the Relay team have worked closely with the React team in developing a Relay Suspense integration for the new facebook.com. He shares the lessons and patterns that they've learned and how they're baking great user experience into Relay. Fetch on render, fetch then render, and render as you fetch are patterns you'll be hearing more and more often as Suspense picks up steam. Thankfully, in today's episode, Joe helps us navigate that verbiage and determine what our first steps with Suspense should be. Thanks to Linode and Clubhouse for making this episode possible. Deploy a server in Linode Cloud in minutes. Offering cloud computing plans for every workload, from simple web hosting to CPU-intensive needs like video encoding and machine learning, Linode offers a balance of power and price for every customer. Linode has 10 data centers worldwide. When you're in the business of delivering content to users, you know how important it is to have that content on a server as close to the user as possible. Linode helps you do that. They just added 2 new data centers, one in Toronto and one in Mumbai. Combine that with native SSD storage, a 40 gigabit network, and the industry's fastest processors, They can help you serve your customers as fast as possible. Visit linode.com/react to get a $20 credit. That's linode.com/react, and the promo code 2019 will automatically be applied. Now let's talk about Clubhouse. If you work on a team like mine, chances are your project management pipeline is confusing. Developers prefer tracking progress in GitHub. Managers have discussions in Basecamp and third-party services like Zapier. send notifications to Trello and Slack to help keep other teams abreast of change. Not totally integrated. This is where Clubhouse comes in and really shines. Clubhouse is project management that brings everyone on every team together. They know design, front-end, back-end, marketing, and management all have different project pipelines, and they give you the tools to manage all of them. Tools for both structured and unstructured workflows that are friendly, fast, have great user experience, and come with all of the integrations you expect. Clubhouse has recently made all core features completely free for teams up to 10 users. And right now, React Podcast listeners like yourself get 2 months free on any plan. I've been thoroughly enjoying my time with the app. Give it a try at clubhouse.io/react. All right. Joe Savona, welcome to React Podcast.
[00:03:29] Joe Savona: Hey, nice to be here. Thanks for having me, Michael.
[00:03:30] Michael Chan: You gave an awesome, awesome talk this morning. Uh, can you give us the high level on like what it was that you were trying to communicate to the React Conf audience today?
[00:03:39] Joe Savona: Yeah, sure. So for the new facebook.com, we've been building out features that use uh concurrent mode and Suspense and Relay. Um, and so we wanted to talk about the patterns that we've found that work for us in using those features. So I think a lot of people have different approaches to data fetching. The sort of common pattern is that you start rendering your application, you initially render a loading state, and then kick off a data fetch when the component mounts.
[00:04:09] Michael Chan: Even even that was like kind of the pattern for a long time with class components of like component did mount, fetch my data, and then re-render when we get it.
[00:04:18] Joe Savona: Right, right, exactly. And that works. Um, but it can lead to waterfalls where uh a parent, a kind of a component at the root of your tree fetches some data, renders loading spinner, then it finally gets its data back, renders, and then a child goes off and fetches its data. And you just see water uh you know waterfalls, spinner after spinner after spinner. And you know, if your app is small, maybe you don't end up with that. Maybe you have just one fetch and that's actually kind of okay, right? Yeah.
[00:04:45] Michael Chan: Or like you can customize certain endpoints where you get like a whole view all in once or something like that.
[00:04:50] Joe Savona: Exactly. Right. So, so. It's it's not necessarily a problem for for everyone. With Relay, we wanted to be to kind of be able to avoid that, right? So uh with with Relay, we use GraphQL so that we can say, what are the dependencies of each component? And that that's just defined in the component. Um we can then aggregate those. So GraphQL lets us aggregate the data dependencies up into a single query. Um and so with one or small number of queries, we can go get the data for an entire UI. So, so that helps us avoid the waterfalls, but it's still this experience of show a loading indicator, go fetch a bunch of a large amount of data, and then come back and render everything. So we've avoided the waterfall, um but we still have this long period or longer period uh where we're maybe showing a loading spinner.
[00:05:36] Michael Chan: So you've reduced the number of spinners. You have a view that like is waiting on all of the data, but like now it's just like all now you just you still have this problem where you're waiting for a lot of data now.
[00:05:46] Joe Savona: Yeah, exactly. So it's still kind of not necessarily the ideal experience. Um, and you can you can do some of this manually. So for example, imagine you have uh kind of sort of above-fold content, like maybe it's a profile page and you want to show the person's photo and name, and then you have a timeline of their posts, for example. Um, and you might just use 2 queries to fetch that. Like if you're doing that today without Suspense, without concurrent mode, what you'd probably do is fetch 2 queries, and you just basically wait to make sure that you don't show the timeline on the at the bottom of the page until the first the the you know the that that header content has been uh has returned. You can do that manually. Like that's totally possible today.
[00:06:29] Michael Chan: Yeah.
[00:06:29] Joe Savona: But what you don't get is things like um actually being able to pre-render the header, right? You're sort of saying, oh, well, I can't render this yet because like I'm still waiting. Or or sorry, like pre-render the feed, right? Um, whereas in concurrent mode and with Suspense, React knows that like maybe I can't show this content yet, but I can start pre-rendering it. I can warm up those contents, make sure the JavaScript is all ready and parsed and loaded so that when we do go to render something, it's faster.
[00:06:59] Michael Chan: Fascinating.
[00:07:00] Joe Savona: Um, and also, if we do it ourselves, we're just kind of waiting, um, and like doing this like this timing orchestration ourselves. With Suspense, the the framework is basically incorporating uh research from uh you know like HCI and like uh user research studies that kind of look at how people perceive performance. Where, okay, if we just have lots of content coming in the screen all over and making things shift around, that feels not like a good experience. Sure. And that and that I think the it's kind of the the number of separate paints that a user sees affects their their perception that it's um fast or slow. So there's kind of absolute performance. How long did it take you to get to the final state? And then there's the perceived performance. And so um Suspense and Concurrent Mode let us improve the perceived performance, even if we're not changing the fundamental absolute performance of our application.
[00:07:54] Michael Chan: So on one side, like you don't want to like wait for like a whole- like waiting for a whole second before you can show the the entire view rendered feels bad. But then also on the other side, like just showing the small pieces as you get those little pieces of data back and then having it actually take 10 separate steps to render that view also feels bad.
[00:08:16] Joe Savona: Right. And so, what we want to do is think about, okay, what are the states that I'm happy- that, you know, we want to show to the user. Like, okay, yeah, we're okay, we want to show the user the profile, uh, profile photo and name. With a loading with a loading you know placeholder for the content. And maybe we're okay with showing just the first story in the person's timeline without waiting for the rest of the content to be available. And maybe you know there's some little side content there that's like uh maybe the mutual our mutual friends, right. And maybe we don't need that. We're okay showing a placeholder there because the user's attention is not maybe gonna immediately go there. So there might be specific things where we're okay showing the application in- This state, this state, this state, but we don't want any other states to appear. And Suspense allows us to um like you know carefully specify that. Um and the framework just- anything kind of anything that we do to our application will sort of just work. Like once we put a Suspense boundary at some point, even if we add a new resource within that, it'll just correctly bubble up and suspend at that boundary. So we don't have to have this, um oh, we added a new thing. And now think about how that what the global implications are. We just have our our like there's a boundary at this point, there's a boundary at this point, and everything just kind of basically just works.
[00:09:34] Michael Chan: Interesting. I I hadn't thought about this until you were saying it just now, but like Suspense is is almost like composing a view in like another dimension, right? Like composing it in like in in time.
[00:09:46] Joe Savona: Right.
[00:09:46] Michael Chan: Right. So you have you know you have your your your comp, your layout or whatever, and like that's fine. But But the thing that that you're like the work that you're after with Relay and Suspense is to orchestrate the composition of that view through time and how how the user experiences every part of that. And like kind of making adjusting the view to their uh expectation of what should show up um first and and what they want to see and what they want to interact with right away.
[00:10:16] Joe Savona: Right. Yep. Um and concurrent mode also has some some other features that kind of relate to this. Um, so we have uh so concurrent mode allows us to do things like selective hydration. So if you're doing server-side rendering, um and you have various— um so you know with server-side rendering, what what happens is the browser gets HTML and can render it, but we haven't we're that that uh that precedes the point where the JavaScript and other assets have been downloaded. So there can there can be a period where you've rendered HTML to the screen, but you aren't able to actually have the components be fully interactive yet.
[00:10:50] Michael Chan: Gotcha. Yeah.
[00:10:51] Joe Savona: So with the new uh server-side kind of concurrent um uh uh server-side rendering and selective hydration, we can actually understand that there are different pieces of your UI. And um maybe we've gotten all the data, all the all the code to to to kind of populate all those different parts of the screen. So an example would be, um let's say that we have a navigation bar and then the rest of our content.
[00:11:17] Michael Chan: Yeah.
[00:11:17] Joe Savona: Well, each of those might take a while to actually get those entire sections to be interactable. But maybe the user doesn't need to interact with the whole- with the main content. And if they hover over the navigation bar, we'd like to be able to respond to that as quickly as possible. And so concurrent mode lets us say, oh, the person- the user hovered over the navigation bar, so let's prioritize getting that content interactable earlier than the main content, and it's actually based on what the user is doing. So it's not that we're never going to um make the main content uh ready for interaction.
[00:11:52] Michael Chan: Yeah.
[00:11:53] Joe Savona: But we can prioritize what the user needs. And again, there's different users have different expectations. So um we we often have a little bit of time to actually respond to an interaction. So if the the size of each chunk is reasonably small, you can actually You know, obviously depends. Sometimes like maybe the con- we haven't even gotten the JavaScript ready. But hopefully, we can actually respond to that in a reasonable amount of time. Certainly, faster than we could have if we had to wait for a long blocking process to handle all of the uh all of the code for the entire page.
[00:12:23] Michael Chan: Fascinating. This is something that I'm still- and I think a lot of us still are like wrapping our head around is like the idea of like scheduling work and and the the consequences or like uh effects of that. And this seems like a really powerful one that you can then kind of observe the user and load and preload things that are gonna be very important to to them, like just by observing them and like making sure that that work gets done first. Sure. And uh and kind of like letting the rest be where it fits in.
[00:12:55] Joe Savona: Yeah, and I think there's still um room for more integration with that. So yeah, selective hydration is something the React team is built and it's more recent. So Relay doesn't yet fully integrate with that. So for example, you might want to have- if Relay is getting data from the server to populate its cache, maybe we want to- we might want to prioritize getting data for that navigation section into the cache so that we can make it ready for interaction. And so I think there's opportunity to have, you know, even deeper integration with, with, you know, frameworks like Relay with those React features. Yeah. And so that's something we can, I think, as a community still have to explore, right? This is still- this is still new, so we're still figuring some of this stuff out.
[00:13:35] Michael Chan: Yeah, so I guess to to know where we're at, like, as a community, this is all kind of like new stuff for us. We have experimental uh releases of React now, and then the same for Relay, correct?
[00:13:47] Joe Savona: Correct. Yep.
[00:13:47] Michael Chan: Awesome. So we can use those to kind of like figure out how to use— uh like put this together on our app. But those are experimental now for the community, but you all have been working with these tools for a really long time. Uh could you tell me about some of the process of developing these as you've been working on the next version of Facebook?
[00:14:07] Joe Savona: Sure. So the React team has been working on concurrent mode and Suspense for a couple of years.
[00:14:14] Michael Chan: I think publicly like a year and a half. Right.
[00:14:16] Joe Savona: Yeah, probably even longer than that. In that range, yeah. Um, I am not familiar with the exact date when they first started, but uh I remember, yeah, we remember conversations going back. Yeah, so so I think what happened is the React team kind of designed concurrent mode And then Suspense, I think, in a way, was something that just sort of sort of became possible with concurrent mode.
[00:14:38] Michael Chan: Yeah.
[00:14:39] Joe Savona: Right. It was something that, oh, wait a minute, we have- given the features that we have, we actually have the ability to implement Suspense at all. Or or at least in a way that works a lot better. Like so we can make concurrent- we can make Suspense work in legacy mode, but it does things like kind of immediately falling back and you don't get some of the nice uh behaviors that that you'd kind of want with Suspense. So, um, so as we were building out the new facebook.com, um, we were kind of at the same point rewriting our uh Relay APIs. So we have the the Relay Core hasn't really uh changed dramatically over the last couple of years since Relay Modern. Um, but when with the announcement of Hooks last year, um, we really wanted to take that opportunity to really refine our the the the APIs that actual that product developers interact with.
[00:15:26] Michael Chan: Sure.
[00:15:27] Joe Savona: So that's uh kind of like the React Relay integration layer, basically. Um and while while doing that, it was also a perfect opportunity to integrate with Concurrent Mode and Suspense and kind of do that that um rewrite that whole integration piece, just get this right. Um and so that led us to basically iterate on that design in collaboration with the React team, in collaboration with product teams and our web infrastructure teams. Um and just really like see what worked.
[00:15:53] Michael Chan: Yeah.
[00:15:54] Joe Savona: And so, that our kind of initial ideas, I think when you see hooks and you think about GraphQL, pretty natural thing is to say, hey, why don't we have a useQuery hook? Right? Like, I've got a query, I want to use the results, so let's define a useQuery hook. Makes a lot of sense. And I think it's where it's it's a pretty natural place to start, to be honest. Um.
[00:16:15] Michael Chan: Well, you I think we saw a lot, like right after the hooks announcement, a lot of people took what they were traditionally doing in render props and making um making hooks for that. So like useFetch or use whatever it was they were trying to do, um kind of fit that into the hooks API. Right. Um and so yeah, I think that that is absolutely like a very at the forefront of our mind is like how do we use these cool things for uh for for data.
[00:16:42] Joe Savona: Right. Uh I think the one— so but for us, we weren't just migrating to a hooks-based API. It was we're gonna do hooks and concurrent mode and Suspense kind of all together. Sure. To make to get it kind of get one cohesive experience. And that was because we really wanted a a a an app-like feel for the new facebook.com. So we really wanted to be able to exploit concurrent mode and Suspense to get staged, really carefully crafted staged loading experiences. Um we had been working on um implementing support for feature uh GraphQL features like defer and stream internally. Which let us define the data dependencies for an entire UI and compose them. But then um using these declarative annotations, @defer and @stream, annotate that we actually don't need, for example, all of the items in your newsfeed to be ready to before we want to get anything back. Sure. So we're actually okay getting an initial result that just has one story and then getting the the subsequent stories in separate follow-up payloads, which lets us put again, just using Suspense, put some content on the screen earlier.
[00:17:45] Michael Chan: Yeah.
[00:17:46] Joe Savona: So we really wanted to be able to do things like that. Or um if we had data in the cache and we navigated to a page, um try to be able to reuse content that's already in the cache to make that transition faster. So if I've just been to a page and I come back, wouldn't it be nice if we could use the data that's in the Relay cache to render that and make that experience faster? Um so that led us to really try to understand, like, you know, work with the React team, understand these features and figure out how to make them, how to use them. So when we approached useQuery, it wasn't just, okay, let's make the same behavior that we've always how to work with a hook, it was how do we make useQuery work with Suspense.
[00:18:18] Michael Chan: Yes. So now you mentioned uh wanting to work with Suspense and concurrent mode and hooks uh simultaneously. Um does does the new work that you're doing exclude legacy mode?
[00:18:31] Joe Savona: Uh so the work that we're doing will work- so there are a few APIs that do not work in legacy mode.
[00:18:40] Michael Chan: Gotcha.
[00:18:41] Joe Savona: And I would like, you know, try to document that. Um, and that's definitely actually something that I think we have to figure out a little bit as a community, um, and in in the React and and and really uh in the React docs, um, especially to for understanding what features will work in legacy mode and what will work in um concurrent mode.
[00:18:57] Michael Chan: Well, it feels like a really big shift. I know that at one point there was a uh a what is it concurrent mode component where you could like just do part of your tree.
[00:19:06] Joe Savona: It
[00:19:06] Michael Chan: seems like that was just
[00:19:07] Joe Savona: Yeah, I'm not sure what the status of that is.
[00:19:10] Michael Chan: I think it's gone.
[00:19:11] Joe Savona: It might be. I don't want to like answer just because I'm not 100% sure. Uh, I try to keep up on the React APIs fairly well, but I also focus on specific aspects of them. Um, so definitely check the docs. Cool.
[00:19:24] Michael Chan: Um, but yeah, I think that it it sounds to me like that that kind of idea of maybe running them in tandem, right, uh, is is not only like extremely difficult, but then also doesn't really give a clear boundary for um kind of what will or will not work in in concurrent mode, right, in relationship to APIs, you know, like in in in Relay.
[00:19:46] Joe Savona: Sure, yeah. Yeah, so I think that's I think that's part of why these APIs that we're releasing are still experimental, is that they work, but there's remaining things that, you know, still need to be polished or figured out um in order to make them really like um you know robust enough to be ready for everyone to use. So that's why Still experimental. Um, so again, uh, you know, try them out.
[00:20:08] Michael Chan: Yeah.
[00:20:09] Joe Savona: But
[00:20:10] Michael Chan: we're in that fun future territory where we get to explore and there's some some rocky roads.
[00:20:14] Joe Savona: Right. Try it try it for a side project or for an experiment. Uh, not recommended for production yet.
[00:20:19] Michael Chan: Yeah. Yeah. So, so one thing I'd like to like tease out is you mentioned two um APIs, which is the the defer and stream.
[00:20:27] Joe Savona: Sure.
[00:20:28] Michael Chan: I'd love to know what the difference um of those are. Which one would you use in this new kind of like Suspense.List API?
[00:20:35] Joe Savona: Yeah, so Stream is a pretty natural fit for Suspense.List.
[00:20:39] Michael Chan: Okay.
[00:20:39] Joe Savona: So, Stream will- so the basic idea of Stream is that you can specify how many items you want as part of the initial result. For example, maybe you don't- maybe you're okay showing an empty list, right? So imagine like the profile page. Maybe you're okay showing no posts from the from that person initially. Well, in that case, maybe you'd say stream the results, but initially just give me not give me zero items. Yeah. And then just stream back items. Or um whereas for newsfeed, we might want to um show a post as you know as quickly as possible. Okay. Just as an example. Um and so maybe there we'd say, okay, we want to stream and we want to get some number of items, but we only do one in the initial payload. So stream uh stream after the first one, for example.
[00:21:21] Michael Chan: Interesting. So you you you had kind of alluded to this earlier. Like this is great for like above the fold kind of like this stuff is critical, the rest can be deferred. So I guess you named it.
[00:21:32] Joe Savona: Right. So stream stream is basically— uh yeah, stream is kind of um incrementally resolve items in a list. Sure. Defer is basically that, but for just a single uh section of the page. So um imagine that you have— uh like I think I gave the example of um mutual friends on the side. And that's just like a block of components. So you don't need to actually stream in the individual friends. We just want to show all of the mutual friends when they're ready. And that's just one component. And so we can defer its fragment, uh and that way we don't have to block. And so you and so a pretty typical thing would be pair SuspenseList with items that are being streamed, uh and then pair a deferred component with a Suspense boundary so that we don't accidentally um bubble up, right? Because that data is not gonna be there, and that component will find that its data is missing and suspend. So if So you kind of pair defer with a Suspense boundary in general.
[00:22:28] Michael Chan: Interesting. Now, I know that there was one more that um that Ashley uh Watkins had talked about, um which was this module. What are those those tags called again?
[00:22:39] Joe Savona: Yeah, so it's uh the the directive is @module. It's a really specific thing. Um yeah. So we refer to that as data-driven dependencies. Okay. Or kind of you can think of it as data-driven code splitting.
[00:22:50] Michael Chan: Okay.
[00:22:50] Joe Savona: So yeah, the basic idea is imagine you have uh some some content that can be rendered in some number of ways. Let's say like a newsfeed story can be rendered in, let's say, 100 different styles, just to pick a big round number.
[00:23:02] Michael Chan: Like photo, video, GIF, a poll, or whatever, a bunch of stuff.
[00:23:07] Joe Savona: Right, lots of different, like text post, right? Uh in a traditional application, what would you do? Well, you'd um you don't know what data you're gonna get. So you'd down in your code, you'd have a probably a switch statement or something that looks at the type of content and decides, oh, I'll render a text story or this story or something. Or you'd make all of them just lazy. But that has implications as well. So what we wanted was a way to say, let's only download the code to render the data that we actually receive. So we're gonna fetch, let's say, 2 stories. Well, let's download the code for just the 2 story types that we actually get from the server.
[00:23:46] Michael Chan: Cool.
[00:23:46] Joe Savona: So if I don't if I don't see a video story, I don't download the video rendering logic. Um, we also wanted, though, um, to make that choice. So, one way to one way to do this is to actually download all the data about, you know, or lots of data about about the the content, and then on the client decide how to render it. So, that might mean you actually, even though, um, you know, you might download a bunch of stuff that would be relevant if you were going to display it as a video.
[00:24:15] Michael Chan: Right.
[00:24:16] Joe Savona: Only to then realize, oh, it's not a video, so I don't need any of that data.
[00:24:19] Michael Chan: Yeah.
[00:24:19] Joe Savona: And then in Relay, and and I'm I imagine other um GraphQL clients kind of um work similarly, um when you're processing GraphQL data, you often need to have an understanding of what the query itself was. So imagine that you fetch friends, first 10. Well, the result will be friends. Okay. Then I fetch friends, first 10 after some number, you know, after item 10, and I get back friends. Well, what does friends mean? I have to know, I have to look at the query to understand that this friends value means um it's a pagination result that has to get merged somewhere as opposed to just like the first 10 friends. So you often need to look at the query and the code. So-
[00:24:57] Michael Chan: Interesting.
[00:24:59] Joe Savona: If we have a query that queries for 100 different types, our query is gonna have con- you know, a description of the query for 100 different types. So our query is gonna get bigger too for these. You know, for for this. Um, so what we wanted was download only the data we need for the actual component, make the choice on the server, download the code for the component we need, and then download the code to process the data only for the types that we need. Um, and so that's what data-driven dependencies is. So we can uh use GraphQL. So GraphQL supports the idea of unions, a type that can return one of a distinct set of possibilities. So if you have a field that returns a union, so for example- Interesting. We can have a storyRenderer field that returns a storyRenderer type. And that's a union of text story, photo story, video story, et cetera, et cetera. And now we can just exploit GraphQL's kind of built-in sort of pattern matching in a sense to say, if it's a uh photo story, then download this module and um this data, right?
[00:25:57] Michael Chan: Wow.
[00:25:58] Joe Savona: Yeah. Uh and so, this is partly where Relay Compiler comes in. So we actually have a full uh ahead-of-time compiler that understands all of the GraphQL in our application can do things like checking and integrate with our static resources pipeline to understand that, oh, this you know this fragment will query for this module, make sure that's a valid module at all.
[00:26:19] Michael Chan: Interesting.
[00:26:20] Joe Savona: Yeah.
[00:26:20] Michael Chan: So so I guess that kind of like leads into— like I obviously this is where you're going with Facebook. What are some of the things that we'll need to figure out as a community in order to bring these uh ideas, which sound amazing, into into other applications?
[00:26:38] Joe Savona: Sure, yeah. So, I think the first step is— uh in the talk, I talked about kind of the alternative to useQuery, uh which is this idea of um render as you fetch, is is like roughly roughly what we're calling it. Uh it's kind of informal term. Uh there's I think We kind of we kind of distinguish between fetch on render, fetch then render, which is fetch everything and then do the rendering. Fetch on render is like start rendering, oops, we can't render yet, suspend or you know stop for a while as we as we go and fetch and get waterfalls. Um and the thing is like these terms, it's probably very easy to mix them up. We mix them up. You're like, wait, was it render as you fetch or fetch as you render? Which one was it? Um and I think it's it's these are very subtle distinctions. Um but they actually do have implications for the user experience. Yeah. So the render as you fetch pattern is start preloading in an imperative um event handler. So, for example, I'm about to open a modal. I click on a on a you know on a UI element, and I'm that that event handler triggers showing the modal. That is the perfect place to start preloading the data for the modal while you also load the code. And then start rendering it. And if the preload is ready, great, you render. If it isn't quite ready yet, then you suspend. But that pattern makes it very clear when a fetch will happen. And it also lets us fetch the data in parallel with loading code. And so I think probably the next step is uh the concurrent mode and Suspense documentation has, you know, talks about this pattern. Um and we've implemented that pattern in Relay. I think probably the next step is to kind of generalize that pattern into something that's usable for things like REST, uh other, you know, other data, other kind of data sources that people are using. And kind of get more awareness of that pattern and more support for that pattern. It's something that you would likely want to integrate with things like a modal or a tooltip component or a router, for example, so that your router is aware of what the data dependencies of your UI are. And if I navigate routes, that the router can start kicking off the preload for me, for example. That's what that's what we are doing internally.
[00:28:40] Michael Chan: Interesting. Now, in terms of the uh things like the modal directive, is is that something that we could recreate in like bundlers that uh like like Parcel and Webpack and all that kind of stuff?
[00:28:52] Joe Savona: Yeah. So um the it's it's it's definitely possible. Possible. Um and Relay Compiler and and Relay Runtime are actually agnostic about what data you return. Basically, uh app module desugars into a call to a JS field. You just have to have a JS field in your in your schema. And it can return whatever data you want. It's actually you tell Relay runtime how to load that value. So it's actually we're actually agnostic what the actual content is. And that was intentional to make it easier for people to plug in a system later. Um so again, even though this is something that we're trying to deeply integrate with Facebook, we try to be really cognizant about not making Relay too tightly coupled. You know, obviously we need to we need to have some way to know what the code is. So we need to have something in the schema that tells us that. Um but the actual- Sure. Type of that data can be whatever whatever you need. So so that's I think something we can integrate with Webpack. Um just have to figure out the exact form that it takes.
[00:29:48] Michael Chan: Gotcha. So so Relay Runtime would be this this layer where you can kind of customize it to your specific needs as as an application.
[00:29:54] Joe Savona: Yeah. So um with Relay Runtime, when you create an instance of a Relay environment, you would pass in a um uh basically an object that knows how to given the results of that field, go and actually download the component.
[00:30:07] Michael Chan: Okay. Cool, cool. Now, tell me a little bit about the Relay compiler, because it sounds like that that's where all any static analysis about certain queries in components and whatnot would happen.
[00:30:18] Joe Savona: Yeah. So, with Relay, we we users define their queries with a GraphQL tag. Uh, and that's uh it's a tagged template literal. So, GraphQL backtick some query and backtick. Just for if you're not familiar with the the the exact syntax node, which I always like, wait, how was it again? Like tagged what? Um, so, uh, and those are completely static. You can't interpolate. It's a tagged template literal. We're kind of uh abusing using the the the the the JavaScript syntax.
[00:30:49] Michael Chan: I mean, everything on the web is an abuse, like from the beginning.
[00:30:53] Joe Savona: Right. It's like, well, okay. Um, you know, we don't, we, yeah, it's just the easiest way to kind of kind of get the get this working. Um, and so what what then Relay Compiler does is looks at your codebase, defines all these GraphQL tags. Because the way that GraphQL works is you can have a query and then do dot dot dot fragment name, and that means spread this fragment in. Kind of like a macro or a function call. Um, and so, if you have a query that does that, dot dot dot fragment, well, what is the definition of that fragment? To know what the query, the actual full query text is, we have to know.
[00:31:23] Michael Chan: And like, and just to like flesh it out a little bit, like a fragment might be like a user fragment where you have like, you know, first name and uh avatar or something like that. And then that could be part of a bigger like profile, whatever.
[00:31:36] Joe Savona: Right. Yeah, exactly. So, um, yeah, you're you're imagine like a story. A story component might be fragment story, you know, fragment my story on story. And then dot dot dot story header, dot dot dot story body, dot dot dot story footer. And each one spreads more fragments but also fetches a little bit of information, whatever it needs to actually display.
[00:31:54] Michael Chan: So, fragments are a way of encapsulating that little piece that might be associated with a specific component in a way that you don't have to then rewrite that every single time for your queries.
[00:32:07] Joe Savona: Yeah, exactly. And so, this lets us tell kind of co-locate what data each component needs with that component. But the key is that a fragment isn't specifying kind of how to get that. It's saying, given that I have I already have a story, these are the fields I need about it. But that means that it's just like a React component. You can kind of reuse it anywhere. It's not we're not coupling that to any particular like method of getting a story. Anywhere that I've already got a story, I can kind of fetch this fragment and render this component. So it makes it a lot a lot more uh kind of more reusable. So I think I think there's probably people don't understand like what that really means. So what what that means is I can open up thenewfacebook.com as a developer at Facebook because we're using Relay. And I can see, oh, I want to change, you know, some component on the screen. I want to like— maybe I see a user, the name of a user, and like, let me just put, I don't know, the mutual— our mutual friend count uh next to that user, um or you know something like that. Sure. Some piece of information that we're not already showing. I can use React developer tools to figure out what component it is, open it up in my editor, go to its fragment, add the field, use that field in the in the rendering logic, hit save, refresh the page. And I don't have to go figure out which of a dozen REST endpoints might be supplying the data to that component. I don't have to look at any of the parent components to like make sure they're passing that data in. I just hit save. I just add the field, use the field, hit save. Relay compiler has a watch mode. So it's watching our our our fragments, recompiling our queries for us. I hit refresh. And the new query executes. The new data is available. Relay automatically extracts, has, you know, fetched that new data because it's part of the new query, and my component receives the data and it works.
[00:33:47] Michael Chan: Unbelievable.
[00:33:48] Joe Savona: So that's the experience that using Relay gets you. Um, and, you know, I think that's and that's also just true of kind of GraphQL. So if you're using most GraphQL clients, you'll get that. Um, the thing that like Relay kind of goes further with, with Relay Compiler, is because the queries are fully static, and because we have, because, you know, well, basically because of that, um, we can do a lot more work ahead of time. So instead of having to print out a query string at runtime, we can figure out what the entire query string is gonna be ahead of time, save it to the server, get back an ID, and just upload a small ID to the server instead of the whole query text. We can look at that query and say, it looks like you're fetching ID and then fetching dot dot dot fragment, and that fragment is also fetching ID. Well, you're fetching ID twice on the same object. We'll just remove the duplicate. Oh, and that makes the fragment empty, so we'll just remove the fragment for you.
[00:34:39] Michael Chan: Interesting.
[00:34:39] Joe Savona: Or, oh, you have a conditional field selection, but you're passing an argument into the fragment that makes the condition known false. So we'll just prune that entire subtree and not need to fetch it, not need to process its data, things like that.
[00:34:54] Michael Chan: Wild.
[00:34:54] Joe Savona: Yeah. So this lets us do, you know, a bunch of optimizations that help. And so another thing is when we're putting data into our store or pulling data out of our store, We have to look at, okay, you're you're fetching photo size, uh, you know, what, like 10, whatever. Sure. Um, well, okay, we have to compute a key for where to put that data in the store. But if we know all the fields and their arguments at compile time, we can just pre-compute the key so we don't have to compute it at runtime and save ourselves work.
[00:35:24] Michael Chan: Wow.
[00:35:25] Joe Savona: Um, so there's a lot of things like this that we're doing to move as much work as we can to compile time. So, yeah, in the end, uh I think it really kind of adds up to a pretty powerful developer experience.
[00:35:37] Michael Chan: Yeah, it's it's unbelievable because I know that GraphQL itself saves you from from a lot of overfetching, but it sounds like this takes it even further and allows you to save you from places that you didn't even realize that you were inadvertently overfetching.
[00:35:52] Joe Savona: Yeah, right. So a common question that we get is, oh, you know, is it is it bad to co-locate this data because I'm fetching almost the same fields that this other component is fetching? And our answer is generally no, it's not bad. You should definitely co-locate the data dependencies because we have this compiler that will say, oh yeah, these two these two things almost overlap and we will you know deduplicate that and reduce you know reduce the cost. So I think this is part of where um uh Frank Yan in his talk um yesterday talked about our our CSS, um this like CSS in JavaScript. And he kind of talked about bending the curve of like we're not just gonna get more and more CSS, but that in fact, a lot of our rules have the same properties. Yeah. And so as we add a little bit more CSS, often most of that is already duplicated. And I think it's kind of the same thing here where sure, you're adding a new co-located fragment, but most of that actually deduplicates is, you know, is is just duplicated um with other things that we're already fetching and you're not actually introducing that much uh additional cost, right? It looks like a lot because you maybe have 20 lines, a 20-line fragment fetching a bunch of things, but actually it's a lot of overlap.
[00:36:56] Michael Chan: Interesting. Well, I know that there was a lot of questions uh kind of like going into this event. Like I think a year ago we saw um everything demonstrated um using React cache. And I think a lot of people were, I think, not necessarily knowing what to latch onto in Suspense and concurrent mode. We're really focused on like whatever React cache meant. And um this year that seems to be— like I mean there there are no releases of it, so I'm assuming that it's kind of like uh off off people's minds, you know, especially given that I think some of the documentation even has like a a bare-bones, like 15 to 20 line implementation of what a resource effectively was. Yep. And um and so it kind of feels like the the the story right now is that there's that if you want to play around with, you know, just any type of data layer. But that really the the work in that realm of of caching queries and and and how that works with Suspense and uh you know error boundaries and whatnot, all of that work is really on your team in in Relay and that that's where the uh I guess data layer React Suspense work is happening. Is that kind of an accurate depiction of maybe where we're at?
[00:38:12] Joe Savona: Yeah, I think that's an accurate depiction of where we've like been at. And now that we're able to kind of release the experimental versions and documentation, um I think it'll be really interesting to see the community begin to kind of understand these patterns, interpret them, and uh kind of explore the the space. So, yeah, so as far as React Cache, yeah, we actually, uh I mean, like I think we still have the package in the repository. We intentionally didn't release that because we wanted to kind of make it clear that that is a pattern that can work. Yeah. That that approach can work. And there are situations where it's okay. Um But um React Cache is kind of great for things like images. Um so the so the React Cache has a a a fixed, you know, you're gonna use like a fixed size cache. Um if the image happens to get evicted from the cache, well, the browser's got a cache too. So it's not sort of, um it's not catastrophic. Yeah. That we that we're like, oops, we have to fetch the image again, except we're not actually fetching it, right? So all we're really doing in that case is kind of creating a new promise for it. Not the end of the world. Um, with things that are not already cached, and often with things like a REST endpoint or GraphQL endpoint, because of um privacy, because of the rate at which the data changes, we don't actually like we don't necessarily want to put HTTP caching or something on that. Sure. Um, that is can be more problematic if that actually gets evicted from a cache. Um, and so for we found that that model, um, we've kind of implemented it in a sense, uh, In the talk, I talked about uh use lazy load query, which is kind of that actually you know fetch as as you as you render. We actually do have a cache that is somewhat like React cache. Okay. It's like a little bit more nuanced in in terms of like how it maintains entries, again, because of that cost of like trying to avoid um having an entry that we might need get evicted. Um so I think that's where the I think like the next round of innovation will be with the community. Is, um, do we- there will be use cases, I think, for that still lazy loading case. But also, in a lot of cases, you don't need that. You can actually know ahead of time, like, yeah, I just need this REST endpoint for this page, needs the data from this REST endpoint. We can preload that and get that really nice experience that I demonstrated in the talk. And so, what is what is a sort of a what is a generic version of preload query? What is a generic version of relay entry points? That we demonstrated where we have code and its data dependencies co-located together that integrates with routing. I think that's where the next round will be. And that's like, you know, hoping to- I'm very excited to see what the community comes up with.
[00:40:39] Michael Chan: Yeah, yeah, absolutely. I think all this stuff is super interesting and I I I really enjoy seeing the collaboration of of your teams because it it really does give us an idea of like what this looks like for React. And I mean, especially I feel like y'all did a really good job of communicating this story of we are consuming these APIs and they have been developed through this amazing effort to like rebuild the, you know, facebook.com using these tools. And the reason it's taken so long and the benefit that all of us are getting as a community is is that you have actually like thought deeply about these together and like work through a lot of really tough edge cases. Um, could you tell me a little bit more about um those teams um working together? Because I know that uh I know that Sophie uh left Facebook last year and um there was kind of like a a manager shift up. Tell me how closely the teams are actually working together.
[00:41:41] Joe Savona: Sure. Yeah. Uh so at the moment, uh so Yuzhi Zhang is our manager and uh when Sophie Alpert left, uh Uh, Yuzi took over as manager of the React team. And we- so I think for the most part, we we collaborate a lot, but also we have, you know, the actual work of implementing something like concurrent mode and Suspense is sort of, you know, pure React work. And a lot of the the work of implementing something like app module or defer or, um, you know, like the compiler work, for example, is is completely just, you know, very Relay specific and and doesn't doesn't doesn't require collaboration. So I think there is a lot of work that is really separate between the teams. And also the React team is very engaged with the community and you know paying attention to issues and things like that. Um we also have a Relay community, but it's it's definitely smaller, um and we focus um more of our effort internally at the moment. So our collaboration has really been um in the context of building out the new facebook.com. Yeah. And kind of Seeing new use cases come up, right, again, and over the course of the last year or so, and then kind of looking at, okay, well, how do we solve this? Is that a React change? Is it a Relay change? Or is it a product change? Um, it's often conversations with multiple people from both teams, plus product engineers, plus other people from the server side, etc., in order to understand like what the appropriate solution is. Um, again, like we're always trying to figure out, okay, this is the the product use case that we have. How do we solve it? Um, but then often that kind of ends up being, oh, okay, that gives us an idea for how we can make Relay better or make React better or or both or, um, yeah, just just things like that. So it's a lot of um ad hoc collaboration, a lot of just discussions, you know, um, around the halls, things like that too. Um, discussion groups. So we have a like a React design discussion type group internally, and we all kind of like post ideas and stuff in there and then discuss them. So. Yeah.
[00:43:39] Michael Chan: I think that answers the question. Yeah, absolutely. And I I like you you had alluded in your talk um to there even being— um that there's not— you don't have some kind of like back channel into React. Like you're I think you're requesting a cancelable- Yeah,
[00:43:55] Joe Savona: yeah. What was it? Right, yeah, yeah, yeah. We were um requesting- so in in the case that a component gets rendered but not committed, okay, um for something like useQuery, if if for example where you're you're um kicking off a fetch during render, well if that component doesn't end up getting committed, you might want to cancel that work, for example. Sure. And so we were kind of wondering, okay, well, how do we do that? We don't- yeah, it's it's it's tricky, right? Um, and so we we asked the React team, like, hey, so can we have cancellation? And it's like, no. Uh, we're like, but like, can we have cancellation? Like, really? And they're like, no. It's like, but like, please? No. Um, and of course, it's it's not just no. It's it's like, here's why we need it and here's why it's problematic and working to understand. And I think what's what's interesting is that But over time, uh, new use cases came along.
[00:44:43] Michael Chan: Yeah.
[00:44:44] Joe Savona: And we see, okay, actually, what we really want is to download the code and data in parallel. And oh, wait a minute, once we do that, actually, the preload query and usePreloadedQuery API doesn't just work for these transitions, it works really well in all these other cases, too. And actually, that completely bypasses the need for cancellation. So it's not a problem of- it's not that we needed cancellation, it's that there's just an even better thing that we got to together. Uh, so yeah.
[00:45:12] Michael Chan: Um, that's one of my favorite types of things is like when, when you have two things that you're working on in tandem, right? Like these, these things need to move together in a way. Um, but each have constraints that are kind of immovable objects, right? Like, so it's like you weren't gonna get cancelable, but because of that, you actually made a better solution that, that works in a in a totally agnostic way where you don't have to like touch touch that part. You don't have to touch React. Um but now everything's like still great and you like kind of had this like epiphany and things move forward and they're they're great.
[00:45:46] Joe Savona: Yeah. And and it's it's interesting that the that approach kind of ended up having benefits that, again, like we didn't yeah we didn't necessarily anticipate. Uh so for example, with the useQuery style API, you start rendering and like okay, you might get rendered again, so you have to cache. Well, what happens if you're caching a request? It means that you might do a mutation somewhere and then come back to that UI, and if it happens to still be cached, well, you're not refetching it and there's a mutation.
[00:46:15] Michael Chan: Yeah.
[00:46:16] Joe Savona: And so, this is, you know, sort of, uh, you know, the uh cache invalidation is a is a known, like, hard problem, right? Um, and so, it you know, it it turns out that um, when we have an API that's more explicit, imperative, you know, call, like, preload the query now, it kind of bypasses that, or it allows you to bypass. So actually, if you look at the implementation, it's a little more complicated than this. But, you know, in in in concept, the idea being that you could have a preload query function that just did no caching whatsoever and kind of guaranteed, we'll just fetch it right now.
[00:46:48] Michael Chan: Yeah.
[00:46:49] Joe Savona: Very predictable behavior, right? Um, and and that just makes, you know, developers don't have to ask, like, well, when will it actually, when will when will when will be cached, when not? It seems really subtle and I can't, you know, it's race conditions that kind of, Oh, no, just event handler. And so it's a very predictable and explainable behavior.
[00:47:06] Michael Chan: Awesome. Awesome. I love it. So for people who are are following along with this event, they see um you know they see React, they see um your work on Relay, uh how it's tying you know GraphQL and React together in all these awesome ways that Facebook's able to use. They want to follow along. They want to like give this a try, maybe get involved. Like what's the best way to do that? And I don't know when this will go out, so you know, may have a little bit of runway, but like how do people get involved in uh really figuring out what Relay might mean for them?
[00:47:38] Joe Savona: Sure. Yeah. So we released um some uh a bunch of documentation along with the experimental release of Relay. Um I think, you know, for for understanding the and and kind of trying out the Suspense features, I really encourage just looking at the Concurrent and Suspense docs um from on the React side. There's, you know, CodeSandbox links there already just just today. So even if this even if this goes out tomorrow, like, you know, it'll it's already there. Um, and those are runnable examples and you can kind of play with and tweak. Um, we don't have a runnable kind of uh kind of full example app with Relay that uses these features yet. Um, we're actually hacking yesterday and got pretty far. Uh, so hopefully by the time this podcast goes out, we'll have at least more progress there. Awesome. And I think that'll be kind of a good a good place to kind of go and look at how how we're actually you can actually use these features um in a Relay app. And I think it could be surprising to people who've maybe looked at Relay in the past, if you haven't kind of given it a look in a in a in a year or two, um I think seeing the new hooks-based APIs uh and and how how kind of simple that makes data fetching while also still being scalable over the long run, I think that could be surprising to people. So I definitely hope that people do give it a look.
[00:48:48] Michael Chan: Cool. And to that point, you'd mentioned something in your talk, how Relay really started its like life as much more of like a routing solution and has transitioned to really exclusively like data, solving the data layer problem. Um, could you talk a little bit about uh to that transition?
[00:49:05] Joe Savona: Sure. Yeah. Uh, so that transition actually happened before I joined. Um, so I'm like, I'm aware of the history. I was did not like live that full transition. Um, but I can, I can share kind of what I know of it. Um, so, uh, basically the the kind of idea was to start out building We had an internal, and and still have an internal, you know, routing system for our PHP code. The the Facebook um web codebase is is um written in Hack, which is uh like a kind of gradually typed PHP that we developed at Facebook. And so we had a kind of a a routing solution in PHP, and we wanted— um we're looking at, you know, how do we do routing client-side? This was still, I think, when the web community was still really beginning to figure out, um, you know, this is like more than 5 years ago. I joined 5 years ago. Uh, so this is like before, before that, right? When the community was still really figuring all this stuff out.
[00:49:57] Michael Chan: Um, like client-side routing was a wild problem.
[00:50:01] Joe Savona: Yeah, right. Um, and it's gotten- there's like really great libraries for that now, but, you know, 6 years, 6 or something years ago, right, it was still, um, kind of, uh, an open area of exploration. Um, and So we were looking at, okay, we need we want to do routing. And then I think the thing is that you pretty quickly when you're doing routing, you come up with the the situation of, okay, so we're gonna go to this page. Uh, what code do we get for that page? Yep. Oh, we render it. And oh, how do we get the data for that page? Um, and we had this thing that we were using to fetch our data in our native applications called GraphQL. And it let you get all your data at once.
[00:50:39] Michael Chan: Yeah.
[00:50:40] Joe Savona: And that was like, well, what if we just combine GraphQL and routing and use the GraphQL to say what data we need for this route and I can go to the route. So what's interesting is that we actually had uh a lot of the ideas we presented today working internally with the early version of Relay, you know, in the ballpark of like 5 years ago. But not exactly in the in the current form, right. Um, this was um before we had Relay compiler. We were doing much more stuff dynamically at runtime. And and that was without Suspense. So this is kind of— um so I think that that idea has been like floating around, right? Uh and it's just interesting to kind of see it come back again and kind of revisit it and get it like an even better iteration of it with Suspense and current mode. And that like you know not just these like one or two loading spinners, but just like I'm I'm you know much more incremental staged experience. But yeah, it's interesting.
[00:51:29] Michael Chan: Awesome, awesome. Well, so with with some of that routing kind of like removed from the library. Uh, what would you recommend to someone who's maybe only familiar with interacting with React and GraphQL through um something like Apollo or another, like, urql or some other type of library that does that uh that layer for them now?
[00:51:49] Joe Savona: I would look at the React docs for whatever we recommend for routing. Um, so, yeah, this is the the thing that happens when you work on a framework is that you don't actually build prod- like, I build products, but I I work on like Facebook product examples. So it's not that I don't use React, but it's that I don't use React like in open source. So, you know, React Router, I almost hesitate to mention any one name because I don't I don't know if there's like some other library that I should be mentioning that's that's equally great. So I apologize if there's some great router out there that I'm not aware of. Um, but yeah, I would recommend looking at the the like the React docs.
[00:52:25] Michael Chan: Okay. Well, but I specifically like Relay versus like um like, I guess the Relay versus Apollo, like, discussion. I don't know if you're trying to get around me on that one. If you are-
[00:52:35] Joe Savona: Oh, no, sorry. I thought you were asking about routing. So if I misunderstood you, then I apologize. But, uh, yeah, so-
[00:52:41] Michael Chan: I thought you were just trying to be like a little bit weaselly. Oh, no, no, no. We can cut it out if you-
[00:52:45] Joe Savona: No, no, that's like, that's totally fine. Um, so yeah, I think there are a lot of, a wide variety of different types of applications and they all require different trade-offs. So, if you're just getting started and you have a very simple app, like use fetch and hit your GraphQL endpoint.
[00:53:03] Michael Chan: Yeah.
[00:53:04] Joe Savona: And do that for a little while. And then when that doesn't work for you, do something more sophisticated. I think it's, you know, people are doing REST endpoints, um, and that like that works, right? You build build very large applications with REST endpoints and just fetching REST endpoints. And GraphQL can be a little bit more, you know, flexible than that, and that you don't have to maintain these separate endpoints. You can edit the query and get new information. So I definitely would encourage starting really simple and kind of expanding as you go. I think there can be a tendency to, um, oh, I need- everyone's talking about framework X or library Y, so I need to use it. Yeah. This is just a general purpose, like, use React state. You know, you don't need to jump to uh to Redux or or or similar. Start off with useState or a useReducer at the top of your tree. Update. When that when that begins to have problems, then evaluate, you know, what the solution should be. Use fetch to get your GraphQL, that's fine. When that becomes a problem, evaluate libraries. Um and at that point, then you'll also have a better understanding of like what trade-offs you have. Your application will have grown in size at that point, um and then you can evaluate which approach makes sense. Apollo has a really great uh getting started experience, for example. They really focused on that. Providing great documentation, making it really easy to get started. With Relay, we focused much more on the kind of longer-term application development where as your application gets bigger, as the number of people working on your your project gets bigger, how do you make that scalable in in terms of both keeping the performance of your application scalable, but also like the development itself.
[00:54:43] Michael Chan: Yes. Right. Um,
[00:54:44] Joe Savona: and so
[00:54:45] Michael Chan: you see that through things where like you have this compiler now that will like remove extraneous things that exactly that that it can tend to get lost in the cracks when you have a big team working on a big application. Sure.
[00:54:56] Joe Savona: Yeah. And there's I think there's some really subtle things that people- some some subtle architectural differences that actually have some interesting implications. So, for example, you know, I I I as far as I'm aware, basically all the the GraphQL libraries and frameworks, they they support fragments, for example. I think Relay is kind of unique in really um appreciating the value of fragments.
[00:55:18] Michael Chan: Okay.
[00:55:18] Joe Savona: In the sense that- yeah, this is like a total GraphQL nerd conversation. But uh so just just to acknowledge that. I'm here for it. Yeah. Uh so in the sense that um a fragment is is saying what what data our our component needs. But that is giving the framework a lot of information that it can then use to help the developer. So some frameworks will just let you define a fragment and then use that. With Relay, you actually use the fragment to read data from the store. So our API is, for example, in the in the new experimental uh release, is useFragment. Mm-hmm. So given a reference to this fragment, like my parent fetched my fragment, they gave me the results. Um I pass that, I take that result and my and my fragment and I call useFragment. So I've got this like reference to a story and I've got a story fragment. I go read the results. Relay can then subscribe for updates just to that one fragment. Hmm. Right? So, you know, the the story changes. There's something in the in the in the store that changes on that story. Like maybe somebody changed field XYZ. I'm fetching title. That isn't XYZ. So the story has changed. We'll have to reevaluate that fragment internally to see. Maybe because we don't know. We know that the story changed. We're not sure which field. So we'll have to re-read that internally. But if we detect that actually title didn't change, the fields you care about didn't change.
[00:56:37] Michael Chan: Yeah.
[00:56:38] Joe Savona: We won't update you. Um, and so what this means is we're doing a little bit of extra work in the cache, right? Where like you might, if you were like were to inspect what's happening, you'd see, wait a minute, why did that fragment, you know, why did we reevaluate that work? Well, it's because we want to know, do we need to even bother re-rendering this component at all?
[00:56:54] Michael Chan: Interesting.
[00:56:54] Joe Savona: Rereading fragments is relatively, uh, you know, relatively, um, relatively cheap. It doesn't, you know, it's not that much time. Whereas rendering, um, our components could take longer, especially if we're, you know, missing like a memoization boundary or something. Um, so we've kind of thought about the whole cycle, right?
[00:57:12] Michael Chan: Interesting.
[00:57:13] Joe Savona: So what we- so when you're developing at scale, you want to have problems of, you know, you want to avoid situations where somebody makes a change and now there's a performance regression at some point and you're wondering why. And it's because, oh, well, this very large amount of UI is getting re-rendered. And so kind of that's something that by really taking advantage of fragments, we can just do a lot of things for the developer. Kind of help keep performance more scalable and more predictable just across the board.
[00:57:42] Michael Chan: Interesting. So you're circumventing- I mean, you're you're getting in there before even React would have a chance to make a decision about that.
[00:57:48] Joe Savona: Right, right.
[00:57:49] Michael Chan: Interesting.
[00:57:50] Joe Savona: Yeah. So rather than, for example, well, XYZ changed, so we'll give you a new title, let React render, only to figure out that, oh, look, the DOM you rendered is identical and have have React have to diff the font, like the the resulting UI, which might have additional, you know, wrapping components and styles and all this other stuff, saying, well, the title didn't change. We can just check the strings and then stop, right? Um, and then we we go actually even further. So if you have a component, let's say one of its fields changed, but all the rest of the data didn't change, we will give you triple equal data for the portions that didn't change. So that when you pass them to child components, it will be triple equal and the should your shouldComponentUpdate or React.memo will kick in and say, oh, I don't need to redo this work. So, you know, again, maybe it's not worth the effort uh of like putting a React memo. It doesn't always make sense to do it. But if you identify that, oh, this component's re-rendering and you go to use React memo, the data we give you will be triple equal if it hasn't changed. And so that will just work, right? So it's it's little things like that that I think we've like really kind of seen, um not just like how should Relay internals work, but at a pretty deep level, how should it integrate with the UI library to really give developers a great experience.
[00:59:00] Michael Chan: Interesting. Well, I am super excited about your work. I'm super fascinated by the um the the partnership and how each library is getting better um by figuring out what piece they need to care about and then kind of like delegating the rest to the other team. Um so it's been really fun to hear that story uh kind of progress and evolve at this conference. And um I was just really delighted to to hear you're part of it. And I'm glad you came on to kind of give us a little bit more backstory, flesh out some of the the bones of the talk and whatnot.
[00:59:32] Joe Savona: Yeah. Well, thank you so much for having me, Michael. It's it's really great to catch up.
[00:59:35] Michael Chan: Yeah. Um one last thing. Sure. Um if people want to follow along with you and your work, um is is there a place that they could do that?
[00:59:41] Joe Savona: Sure. Uh I am @ian_js on Twitter. It's a long story.
[00:59:48] Michael Chan: Awesome. Well, maybe we can leave that for next time. Next time. Awesome. Well, thanks for being here.
[00:59:55] Joe Savona: Yeah, thank you so much.
[00:59:56] Michael Chan: That's it for today's episode. Find links and show notes for everything mentioned at reactpodcast.com/71. Thanks, Joe, for your deep knowledge on these topics of building great user experiences and React Suspense. Your experience helps all of us make faster, friendlier applications. If you're looking to squeeze one last React conference in before the end of the year, Check out reactday.berlin. Get 15% off the last wave of available tickets with the promo SPECFM. Or just visit our show notes and click the link and the discount code will automatically be applied. Thanks to our sponsors, Clubhouse and Linode. Clubhouse has recently made all core features completely free for teams with up to 10 users. And right now, React Podcast listeners get 2 months free on any plan. Get your projects back on track at clubhouse.io/react. Instantly deploy and manage an SSD server in the Linode cloud. Get a server running in seconds with your choice of Linux distro, resources, and node location. Get a $20 credit when you visit linode.com/react or use the promo code Use code REACT2019 at checkout. This episode of React Podcast was edited by Mikael Delport. It was produced by Mikael Delport and Sarah Jackson. You can find React Podcast on Speck, a network to help designers and developers level up. Visit speck.fm to find other shows that will take you further in your career. Help us out by reviewing this show on iTunes. Your reviews help the show grow and help help us ensure great guests and awesome content week to week. To join the discussion, visit reactpodcast.com/chat or follow us on Twitter @reactpodcast. I'm @Chantastic. To stay out of the discussion but get updates, visit reactpodcast.com/news and sign up for emails. Thanks so much for giving us your attention. We'll be in your ears again next week. Thanks for listening.
chantastic.fm