This course has been updated! We now recommend you take the Advanced GraphQL, v2 course.

Check out a free preview of the full Advanced GraphQL course:
The "Data Loaders Solution" Lesson is part of the full, Advanced GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through the solution to the Data Loaders Exercise by showing how to speed up the resolvers' efficiency.

Get Unlimited Access Now

Transcript from the "Data Loaders Solution" Lesson

>> Scott Moss: Hope everyone had fun interacting with the data loaders and understanding the complexities around batching and catching and how all that works. Did anyone observe any like, weird behavior with the data loader and things that they didn't expect, like how it batches or-
>> Student: I got thrown off by the object I need from Mongo isn't equal to itself.

>> Scott Moss: Yeah, that's true, [LAUGH].
>> Student: So, my loader was getting called multiple times, cuz I wasn't passing to a certain.
>> Scott Moss: That's right, yeah, mongo IDs are objects, but if you look at them, they look like strings, cuz they call two strings, one log. But yeah, so you gotta cast those to strings, anyone notice the behaviour of a data loader when going down to another level.

>> Scott Moss: No, alright. Nothing special there I just want to know if anyone observed like they thought something different happened like they thought there would be another batch call or do they think that it will just pull from cash or how that would work, just curious. I was gonna ask cuz when I started learning, it kind of turn me off.

[00:01:09] I expected something else and it wasn't there, really cool, let's go over this solution then, he would not get help one done
>> Scott Moss: Okay, cool, great, yeah that one was meant to, I didn't expect anybody to get that done. So I gave you no context, no whatsoever. I just wanted you to struggle a little bit after you did something before.

[00:01:36] It's okay to struggle when you know you're struggling on, so let's hop right in. We go to the task resolver, which is the one, so you were tasked with creating a loader, or replacing the model. Query with a loader instead, so the project loader was already created for you.

[00:02:03] So all you had to do was come in here and just replace it with this, did anybody use some other way other way of using the loader in this project task? Yeah it's almost too easy, it's already done for you, yeah, so that was there. Then there was an extra credit one, but we'll get to that one, the other one was basically,

>> Scott Moss: Here we go, so it was the GitHub one. So let's actually look at how that's defined, then we'll come back to how they use it, so we go to loaders, if we go look at the GitHub loader, the couple of things you gotta notice here about this GitHub loader and how it all ties together.

[00:02:45] On the task, on the dev task type there is a repo field,
>> Scott Moss: Right here, that' is the name of this disk, name, issue count, description, url, but if you notice, on the task model, none of that exists. So where is that being saved in the database? It's not, it's all virtual, which means all you really need is a repo URL and then based on that URL we have a resolver, that makes a call to GitHub to get that information and provide it to you in the shape of this repo.

[00:03:25] So all that's virtual, so it's guaranteed to be up to date as long as you can hit the API. And that's happening in the task resolver right here, this repo. So you can see it takes the repoUrl, splits it, gets the last thing on the end which is the name of the repo and then it makes a call to GitHub and tries to fetch it and then it returns it in the shape that it needs.

[00:03:49] So before this loader was here there was an actual call to GitHub that was being called. So what we're gonna do is we're gonna replace that with a loader. That looks like this, so we make a new loader. This is gonna take an array of repoed names just like the loader you created or the project loader above was an array of ID's this is gonna generate repoed names,.

[00:04:11] The reason why we know that is because, I do not know why this is not staying open We're gonna be creating, we want to be able to get all the repos from GitHub but only return the one given the name. Cuz that's the one that we're actually accessing, right?

[00:04:29] We're accessing the name, so that's why it's gonna be an array of names because that's what we're determining the difference between the repos, or the names, so it's gonna be by name and then put that over there here we go.
>> Scott Moss: So it is gonna be a array of repoed names and for each one of those names all we're really gonna do instead of making a database call, because this is not in our database it's in GitHub, we're gonna use this reposForOrg call which is inside of GitHub.js.

[00:05:01] Where it's literally just goes to the type io org and fetches all the public repos and sends them back. That's all it does, so every repo in that public there is gonna send that. But we're only are interested in the one repo that we're resolving at the time, so that's why we need the names to figure out which one is which.

[00:05:19] We've look it up by the name So that's where this comes in, so we get all the repos back from that Org. Then we group them by their name, just like we group the projects by their IDs, we group these by their name. And just like we did with the project loader, we looped over the given input, in this case repost names and then we pair them up with repos that came after from GitHub.

[00:05:42] So it's basically the same thing, except for replace a database query with this call instead.
>> Scott Moss: And then now inside the taskResolver, we can replace what we were doing with GitHub in here, which was like two lines of code, with this one line of code that loads it up, batches it, encashes it, which prevents us from going to get help every single time in a single request.

[00:06:06] Because it's a third party API call, that is gonna tremendously speed up our resolver. Because now we don't have to go to get help and come back every single time he asks for that one request. And let's just do it one time for that current batch
>> Scott Moss: And then the last one, I'm sorry there were two more.

[00:06:29] The other one that I asked you to do which is slightly a bit more practice, was inside of loaders which was to create a task loader. Who did not create a task loader?, ok. So, this one's pretty simple, but basically, it's the same thing as the project loader, right?

[00:06:46] Except for you just change tasks in with sort of projects and that's basically I think the only the only work you gotta do is maybe you have to require this if this wasn't there. I can't remember if that was there or not and that's about it, it's basically the same thing, you could've copied and pasted this.

[00:07:01] And just change everywhere a project is the task because all we're doing is, giving it a ray of task ID's, find task that are matched with these ID's and then group them and then return them.