Advanced GraphQL Advanced GraphQL

GitHub API Data Loaders

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

In this additional data loader example, Scott shows the solution to using data loader with the GitHub API.

Get Unlimited Access Now

Transcript from the "GitHub API Data Loaders" Lesson

[00:00:00]
>> Scott Moss: Okay, and then there was the final one, which was extra credit. Which was basically, where's that one at? Project resolver. Yeah, is this one, so tasks. So this one I said, yeah, try to find a way to use a loader here if you can. Did anybody find a way to use a loader here?

[00:00:22]
>> Scott Moss: Okay, how did you do it?
>> Student: I did a query in another loader that handled the relationship. So I was actually taking the project ID as the key for the loader and looking at the tasks collection.
>> Scott Moss: Got it. So you take a project ID, okay, cool.

[00:00:38] So does anybody wanna see a solution for this? You? Okay, all right. There's a lot of ways you can do this one. So basically what you would do, and that's a good approach, and I'm probably gonna take a similar approach to that.
>> Scott Moss: Let's call this one
>> Scott Moss: Project or taskByProject loader, that's what we'll call it.

[00:01:05] So this one is going to take in,
>> Scott Moss: An array of project ID's. So we have some project ID's here.
>> Scott Moss: And the difference with this is, we're taking project ID's but we're actually trying to return task. So there's a relationship thing here. So we need to do, is we need to develop like a query for Mongo that, okay, so let me try to describe it this way.

[00:01:45] So if we have an array of project ID's, right? And for each one of these ID's there's going to be an array of tasks.
>> Scott Moss: Right, so if each one of these projects had three task each, that's gonna be four arrays with three tasks in them. Does that make sense?

[00:02:12] So what we need to design a query that will take all of these ID's, and that query is gonna return back from Mongo all the tasks. But they won't be grouped by the array or the project that they are associated with. So that's where the complexity gets there.

[00:02:35] So that's still small difference because now we're dealing with collections of things, they're not just values. So there's something to think about there. It makes it hard to plan stuff like that. Okay, so now we have that out of the way what we can do is we can just return a query here for a task.

[00:02:53] What is that? [LAUGH] Task.find. And basically we wanna find all the task that have a project that's in this array of project ID's.
>> Scott Moss: So we'll get that and then this is gonna give us all these tasks here.
>> Scott Moss: So now that we have all those tasks we can just group them by the project.

[00:03:27] So tasksBy,
>> Scott Moss: Project. And that's just going to be we're going to use key by.
>> Scott Moss: Let me get rid of this over here.
>> Scott Moss: And we're just gonna key by the task by the project property. There we go. My god.
>> Scott Moss: So now we got them grouped, we've got the objects.

[00:03:59] And now what we can do is we can return on the project Id's.
>> Scott Moss: .map over those because if we have four project ID's and we have, again, four tasks on each one of those projects, that's four times four. So that's what it looks like flat, but we've gotta form it back to where it's an array.

[00:04:21] So what we need to do now is we'll map over it and then we'll say, we'll get this project ID here, and then it's basically the same thing. So we'll just be like tasksByProject with the project ID.
>> Scott Moss: Does that make sense? All right, so now you got that you can just come out here create taskByProject or whatever you wanna call it.

[00:04:56] And now you have access to that loader inside of project.resolver, which would be loaders.taskByProject.load
>> Scott Moss: There you go. And that's basically the only way how you can do this. Is that how you did it?
>> Student: Pretty much, yeah.
>> Scott Moss: Yeah. So there's another way you can do it where you can have, we already have a project loader and a task loader, we can have them talk to each other and prime each other with data.

[00:05:38] But then when it comes time to loading it over here your keys are off. So it can get pretty complicated. And then you might think of doing a key that's an object that's a key. Again, it can get really complicated. The thing about DataLoader is that it only works when your data is simple.

[00:05:56] when data gets complicated this also gets very complicated and you could be banging your head on the wall. Any other questions on this?
>> Scott Moss: Nope. Everyone good with DataLoader? Know when to use it? Know how to use it? So use it to batch and cache database calls. Use it to batch and cache calls to other API's.

[00:06:22]
>> Scott Moss: There are some other exceptions around some of that but mostly that is true. I would say DataLoader is pretty well used. You can do things like hook it up to Redis to where it persists it in Redis. So it would be your Redis layer and then also fall back to M memory.

[00:06:38] So there's a lot of things there. If you have data that's universally used for all your users you can cache that. Or if you come up with some really good cache keys, that stuff gets really complicated. So yeah, that's DataLoader. Alright, so what we're, any other questions in general about caching in GraphQL?

[00:06:57] Because all we did is talk about DataLoader. But there's other things. I was gonna save those for the end, but if people have questions now, I'm willing to talk about them.
>> Scott Moss: Nope? Alright. Caching is harder than GraphQL, [LAUGH] really hard. There's no git request.