Transcript from the "Arguments & Input Types Demo" Lesson
>> Scott Moss: So we call it shoes. Let's say a type shoes, shoes has a brands and that's a string. And they also have size and that's a integer. Sure, okay, and they will make a query for some shoes here. And what we'll do is we'll give us an argument that will make it a minute.
[00:00:20] I'm gonna say shoe that they're called a shoe and not shoes. Cool and now we're gonna make an input type. So input for shoes input and I'm gonna let you query by brand, which can be a string. I'm also gonna let you query by size which is an editor.
>> Scott Moss: So, and then I'm gonna pass this here as input and shoes input, I'm not gonna make it required. So I made a type, I made an input with some fields that I'm going to use for filter, and I made a query. Now I've just got to make a resolver for these shoes.
[00:01:01] So open here, shoes, no value here, no initial value but we do have the arguments here and that's gonna be an object with an input key on it which is the same name as this. If I change the name here then that's what gets changed here but I always me personally, I just always keep it the same it's a lot easier to deal with.
[00:01:22] So I don't need context in this example. So then what I'll do is I'll just return an array of shoes, which have a brand Nike, and a size of 12, and 15, yeah. Let's make one more, let's make one more, and we'll call this one Adidas and this one is 14 in Adidas, cool.
[00:02:00] So now we have two shoes, two brands, two sizes and we're not filtering, we're not doing anything with it right now but we'll just start this and I'll say no API source demo our source.js. Cool, 4,000
>> Scott Moss: All right, got our shoes here. Cool, so if I ask for some shoes, I should get back all those shoes with the brand on them.
[00:02:32] Cool, simple enough. Now let's actually apply our arguments and get going with that. So remember I didn't make the arguments required so the query didn't break but now if I want to use them in my query, I'll just go in here and I'll pretty much do the same thing I did on the server.
[00:02:46] I'll use parenthesis then I'll start typing in the name of the argument in this case it's always input. You can see I get a autocomplete there cuz it knows it's called input. So I just hit enter, autocomplete. We know input has a certain shape too, if I start typing right here, I'll get that shape too.
[00:03:02] So if I say I want to filter by brand, you see brands there then I can pass in a brand. The thing is with strings you always have to do double quotes in here, don't do single quotes that they go to break. So cool all you want to get the ones that have Nike, right?
[00:03:18] So I'm gonna say Nike. And obviously this isn't gonna do anything if I run it, I'm still gonna get all of them because I'm not actually doing anything with this in the resolver. Right, so now if I go inside of the resolver, I have input, I can say actually, let's filter shoe by, I'm just gonna hard code the case of Parco the case of brand right now for now.
[00:03:49] So just return of shoe.brand equals input.brand. Right, so I'm gonna filter those out. Start this thing.
>> Scott Moss: And if we run around this again, did I not spell it right?
>> Speaker 2: I think you did a return statement.
>> Scott Moss: Yeah, you're right.
>> Speaker 2: The same line.
>> Scott Moss: Yeah.
>> Speaker 2: Which displaced the devices.
[00:04:40] Or if you're hitting some third party API and you need something to pass the API, that's what these arguments are for. At least on the career level and on the field level, they're just, for me personally I use them, what I like to do on the field level is any field on a type that is an array type.
[00:04:59] For instance, if Pets did have something called Buddies here and it was an array of Pets, then I would always pass off an argument here to be able to manipulate that array. Whether I wanna sort it, filter it, cuz maybe I don't wanna get all of it's Buddies, I only want to get it's Buddies that are dogs, something like that.
[00:05:18] So I would always pass arguments to fields who return array types. And that's just a habit that I picked up because you normally want to be able to manipulate an array whether it's pagination, whether it's transforming, sorting, filtering whatever that is, so that's one thing I always learned.
[00:05:34] Other than that on fields you probably don't need arguments. Like I said, you can do things like transformations on images and things like that. But there's also another way that you can do that and those are called directives. And we're not talking about those in this course, but most people just use directives for that.