Check out a free preview of the full Introduction to GraphQL course:
The "Info Resolver Argument" Lesson is part of the full, Introduction to GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott shows the audience what the info argument looks like to give context to what is meant by an AST.

Get Unlimited Access Now

Transcript from the "Info Resolver Argument" Lesson

[00:00:00]
>> Scott Moss: Guess the only last thing I wanna show you, context is not that nice. I mean it's just an objects, whatever objects you pass to it. But I did tell you about info, so I'm just gonna log that for you so you can actually see what that looks like.

[00:00:11] So you know what the info object looks like when I say like it's an ast, what does that actually mean if you've never really done any lower language level stuff, that might sound weird. So we'll log that and this is it. This is the ast of the operation.

[00:00:27]
>> Scott Moss: So basically, takes the query that I sent up and it turns it into this. This thing with the field nodes on it, and arguments, and the return type, and the entire GraphQL schema, like literally everything that you need to figure out what was sent and how it was validated.

[00:00:44] So this is like, if we go to this one, really crazy tooling. It tells you the location in the schema where this thing was defined, what type it was using, what's expected, any directives, all types of stuff. So it's the ast, so again, this is really good for tooling.

[00:00:59] The only time I've ever had to use this was optimizing database calls, where I only wanted to create a database for the exact fields that they asked for, and nothing else. That was it. So only query a cat with the name and age, cuz that's the only thing they asked for, and nothing else.

[00:01:14] And that way, I didn't have to get everything from the database, and only send back the name and the cat.
>> Speaker 2: I was hoping it would automatically do that.
>> Scott Moss: Yeah, so that's a good question. It does, so for instance if I send back.
>> Scott Moss: Actually, let's check it out.

[00:01:36] So let's say I don't ask for an age here. Let's get rid of this. If I don't ask for an age here.
>> Scott Moss: But this resolver has sent back an object with an age property on it. It just won't include it. But that's free, cuz it's in memory.

[00:01:56] But if you query the database on age property, the damage is already done. You already queried the database for the age property, even though GraphQL won't include it because the query didn't ask for it, you still already queried the database for it. So that's where the is gonna be, is querying the database for fields you don't need.

[00:02:10] So yeah, you can send back whatever object you want, but it's only gonna match the names based on the query that was sent. Those names have to match up to the fields in the object that you send back. If those names don't match up, then it's gotta say that, that's just not real, that's not a real type, that's not a real field on this type, that's not a real thing.

[00:02:29] The only other way it'll resolve it is if you do this, if you write the resolvers for the names yourself.
>> Scott Moss: So yeah, resolvers is a doozy. It took me a while to actually truly understand how to use it. I mean, I think to this day, I'm still learning.

[00:02:46] Like there's just so much stuff you can do with them, it's a lot of stuff you can do with them. But, yeah these are the basics. This is the basis of understanding resolvers and how they work.