So let's take a look at what GraphQL might be able to do to help us here. So first, we're going to dig into fragments a little bit and talk about what those are. So for those that aren't familiar with GraphQL, fragments, fragments are essentially reusable units and they're a subset of fields that let us split our complicated application data into smaller chunks. This can be really useful if you want to combine lots of UI components together into a single data fetch where each of those fragment defines data requirements for a component.
And here's what it looks like syntactically. So on the bottom, you see our fragment definition, which includes the fields that we want to include as part of that fragment. So in this case, it's that reviews field that you see there. We can include that fragment in our query using that spread like syntax that you see above. So this just makes sure that the fields in that fragment are fetched as a part of our query.
And here is how that query and the fragment that it contains can map onto the components that we're rendering. So here we can see the title and media URL where those are rendered and and the reviews which are rendered beneath. And again, to to the point that Gerald made around how fragments can allow you to scope those data needs of a specific component. This is where we'll see our product reviews component that are rendering the stars. Just declaring that that data requirement through the use of a fragment and Apollo clients use fragment hook.
So, you know, in this case, all products are rendered beneath and all reviews are rendered beneath. So, you know, in this case, all products is fetching all of the data that this UI needs. But product reviews is rendering the reviews data using that use fragment hook. And it's worth noting that fragments aren't enough on their own to improve the user experience here, because while they give us a way to include the product reviews, the product reviews components data in our query, we're still waiting for all of the fields to resolve before we can render our page. So let's turn to something else that GraphQL gives us here to solve this problem, and that is the defer directive.
What is the defer directive? So defer allows us to mark a set of fields as deferred within our query. We still get the benefit of a single network request, but defer tells our GraphQL server to stream those deferred fields in later via a multi-part response. And this gives us that ability to stream in data incrementally. This can really provide a great experience for situations where some fields take much longer to resolve than others. And this is what it looks like in our query. It's essentially allowing us to annotate that product fields fragment with a defer directive to indicate that all of the fields within product fields can be streamed in at a later time.
So we've learned about these utilities, but how do we actually use this in our application? So let's actually do this and we're going to walk through a demo of this in action. I just want to point out before we go too far that what we're about to show you here is highly experimental. It's not yet released in Apollo Client, but we are working on this functionality and we want to add this to a future release. So we just wanted to be able to get excited about the future with you on the possibilities that using GraphQL in the React server component architecture provides. So let's dig in.
Comments