Advanced GraphQL Data Loaders
This course has been updated! We now recommend you take the Advanced GraphQL, v2 course.
Transcript from the "Data Loaders" Lesson
>> Scott Moss: So again the point is I already got this I don't want to have to get it again. I already got this, I don't want to have to get it again. So that's what we are going to look at. So first things first is what is a DataLoader?
[00:00:17] How do we make one and how do we use it? So it's actually a library called DataLoader.. If you look at loaders.js, you'll see that I already have some stuff here. I have this createProjectLoader, so all that stuff you have to do on the exercise. But let's just walk through what this DataLoader.r is and try to understand the dynamics of it, and why the way it's written, and how to apply it.
[00:00:43] So the DataLoader, basically the way it works is given, cuz it does batching, that's the whole purpose. That it wants to batch the similar operations on any given level in GraphQL to resolve at a later time and then cache it. So what that means is for this project loader, its purpose is to resolve an array of project IDs and then pass them back in the same order with the same number that they were given.
[00:01:14] So what that means is if I make multiple calls to the database to fetch projects on the same query. This DataLoader is going to batch them all. Wait until the end of the request, then it's going to pass all those IDs, so this going to be an array of object IDs.
[00:01:34] Then it's our job to write a database query given these project ideas over turn all the matching projects with that ID. And then DataLoader is expecting as a return type in array of promises that will resolve to the objects that represent those IDs. So here basically what I'm doing is I'm gonna create the database, I want to find all projects that have any of these IDs, then I'm gonna group them by their ID.
[00:02:12] That's gonna allow me to map over the given IDs and replace them with the actual projects that they represented from the database. So this keeps the order of the IDs I've got, because order's important. DataLoader looks at the order of the ID that you gave it to know where to resolve it to, so order's important.
[00:02:33] And then for as many IDs that I got in this array, there had better be as many results in this array as well. They have to match, even if it's null. There has to be something there. Otherwise I'm gonna freak out. Those are the only requirements. This sounds very complicated as I'm explaining it.
[00:02:47] As I'm writing it, you're gonna see it's not actually that bad. It's just like a caching thing. So again, DataLoader. batches and then it caches. Batches and caches. Kind of rhymes. This may be my debut album, batches and caches. All right, any questions on the DataLoader?
>> Speaker 2: One, essence of version one, all the IDs being unique.
[00:03:11] I guess I'm trying to figure out why 13, 14.
>> Scott Moss: 13, 14, this right here?
>> Speaker 2: Yeah.
>> Scott Moss: So all the IDs, they might always be unique. So check it out.
>> Speaker 2: [INAUDIBLE]
>> Scott Moss: I might ask for the same projects in the same request. So this actually might be an array of all the same IDs.
>> Speaker 2: Got it.
>> Scott Moss: Yeah, right? If the ID's 1, this might be an array of 111111111.
>> Speaker 2: So if you uniqued that before you passed it in, then you could
>> Scott Moss: Well, you wouldn't want to because if you do, if you unique it, you're going to, you're gonna mess DataLoader.
[00:03:44] Cuz DataLoader's like I gave you an array of ten things. You uniqued it and you made it one thing.
>> Speaker 2: It wants ten things back.
>> Scott Moss: It wants ten things back. Yeah, it's gotta be the exact same thing.
>> Speaker 2: Got it.
>> Scott Moss: Otherwise it won't know where to pass the resolved data.
[00:03:56] It's like I don't know what you want me to do with this. I gave you 10 things, you gave me one thing back.
>> Scott Moss: Exactly, yeah, you got to keep order, order is important. Otherwise it won't know where to resolve to, so you got to keep order in the keyBy is, so I can just look it up by the ID and I loop over the given IDs.
[00:04:20] I don't look over the results. Cuz this is gonna be in any order, the DataLoader's gonna give it back to me in whatever order it wants. The only thing I know that's guaranteed the right order is the IDs that DataLoader gave me. So I'll loop over that and I associate that ID with the project from the database.
[00:04:36] Which guarantees the order and the amount. And if for whatever reason there wasn't. If one of these IDs didn't actually represent a project that was found in the database, then it would be our duty to replace that value with null. Which is basically what this does. So this, like, or this is undefined, I think.
[00:04:56] This goes undefined. There at least needs to be something there.