37
loading...
This website collects cookies to deliver better user experience
GraphQL
. It's developed by Facebook and was conceived with the same ideia of React componentization.And how does that relates to Relay?
query
structure, just your component. (Not every case works this way, but most of them)VideoPlayer
: used to render the video that we're watching. Probably need the videoSrc
from the server.VideoDetails
: show the video details like title, description, author, number of likes and dislikes.RelatedVideos
: it's a list of videos that the YouTube algorithm believes that you would like to see.UserImg
: renders the logged user profile image.Just remembering that all of that it's just an example. For sure the real YouTube architecture it's much more complex than this.
GraphQL
query, like this one:graphql`
query NavbarQuery {
user {
profileImg {
src
}
}
}
`
Here's an article explaining why you should use loading skeletons
in your interface.
videoSrc
when the component VideoPlayer
is fully rendered. And if for some reason any of these components above the VideoPlayer
take a long time to load, we would need to wait that time until we can call the server and start loading the video.VideoPlayer
.response
from the server
with the videoSrc
data.Ok, so we have these two problems. What it's the alternative?
But didn't you said that Relay is built on colocating the data dependencies with the components that need them?
data dependencies
I didn't mean the fetch function, I mean the declaration of the data that it's needed. We only fetch once, at the page render. It would look like thisRelay
Fragments
.Fragment
, in Relay
, it's a declaration of the data that a specific component need. // page component
graphql`
query PageQuery {
user {
...NavbarFragment_user
}
}
`
// navbar component
graphql`
fragment NavbarFragment_user on UserType {
profileImg {
src
}
}
`
Navbar
declares exactly what it needs and if something change, we will only change on the fragment, not on the page query.Even though this is nice, we have a problem :(
10
of Relay
we can't have a loading indicator
per component, we need to show a loading indicator
on the entire page before showing some data to the user, something like this:fetch
and while this fetch
don't return a response, you show a loading indicator
.@defer
directive from GraphQL
alongside Suspense
component from React
. @defer
directive you could say that a specific piece of your query
, like a fragment
, should be loaded asynchronously and while the response of that piece doesn't return from the server, you show a loading indicator
passed to your Suspense
component.Here's the Relay documentation where they explain in more details how to do that and in this video how they're using that on Facebook.
React
once was, Relay
still a not much used library and because of that there's not much articles and tutorials explaining how it works.Relay
on your application or how its main idea works.