REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) Tracing Nested Resolvers
This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Tracing Nested Resolvers" Lesson
>> Scott Moss: I promised that I was gonna walk through how GraphQL resolves different levels. I forgot I was supposed to do that, so let's do that first. I think that would be very helpful. Because I know when I first saw that I was just like whooh man, that is awesome.
[00:00:12] So let's start with that.
>> Scott Moss: So many branches. Okay, I got to write all that over again.
>> Scott Moss: Let's start as a user.
>> Scott Moss: Wait, there we go, okay. So user, one more time. Getting user,
>> Scott Moss: Getting playlists,
>> Scott Moss: And then getting songs.
>> Scott Moss: And then on the song, test: String.
>> Scott Moss: And down here, Song,
>> Scott Moss: Test, return test,
>> Scott Moss: And getting test, okay. Now we can try.
>> Scott Moss: All right, so come back in GraphQL, and if we run this query again. Actually let's refresh to make sure GraphQL is up to date. We can check the query of song and we should see test also.
[00:01:30] So I'll just come down here and I'll say I also want to get the test before I run this. Everything looks good. I'll look at the logs and we can see right here. It's getting the user. Then it was getting the playlist, then it was getting the songs, then it was getting the test, right?
[00:01:47] So let's undo all these queries and see what keeps logging. So I'm gonna get rid of test, and I'm gonna run this again. Notice it does not getting test, we didn't asked for it so it didn't try to get it. Now, I'm gonna get rid of songs.
>> Scott Moss: I don't need to refresh that.
[00:02:05] I'm gonna get rid of songs. Notice it's not saying getting songs. I didn't ask for it, all right? Kill that, and then if I get rid of playlists, it's not asking for playlists because I didn't ask for it. It only resolves what you ask for. And that's the power, that's the thing that I wanted to show you.
[00:02:25] So you have to create the resolvers if someone asked for it but you don't know if they will or not. Which is why you want to have nested resolvers versus putting everything in one flat resolver that gets every possible combination of everything. You would never know what someone was asking for.
[00:02:40] And if you did, you probably have a very restrictive API that you probably don't need GraphQL. You should just use REST or something, most likely, or just your own implementation or whatever.
>> Scott Moss: Any questions on that, how that's working, or I know some people if you think like me, you might be thinking, well, how do I break that?
[00:03:03] Cuz that's how I learn, I'm like how do I break that? Anybody thinking like that, what are some questions around what are the fallacies of that, or anything like that?
>> Scott Moss: Nope, all right, cool, okay. So now back to the test. If you're on lesson 14, go ahead and write some tests.
[00:03:28] It is now 4:04, we'll come back here at 4:30.