Mutating with Input Object Types4:32 with Isaac Lee Morris
Return the Movie object with the nested Director object, and show the original fields.
(Apollo's Launchpad has been deprecated, so your screen will look different)
In this video, we're gonna be running another mutation, but 0:00 this time with a different set of arguments. 0:03 Instead of only specifying scalar arguments, like title and tag line, 0:06 this time were gonna add a more complex object type called an input object. 0:11 Unlike scalars, input objects have their own nested properties, 0:17 which can be scalars or more input objects. 0:21 The endpoint we're using in this video is the add director to movie endpoint. 0:26 To see which arguments the add director to movie endpoint accepts, 0:31 let's scroll down in our back end editor to check out the schema under type deps. 0:35 Looks like it takes a movie ID of type ID, which in this case just means a string. 0:43 It also takes a director object of type director input. 0:48 If we scroll up to check out the director input type, 0:53 we can see that the only field that it contains is the name field. 0:56 Going back to our addDirectorToMovie endpoint, 1:00 you can see that there's a colon after the arguments list. 1:03 This colon indicates what type of object will be returned from this mutation. 1:06 Just like the create movie endpoint, 1:12 addDirectorToMovie also returns a movie object. 1:14 To define the input object we're going to use the query variables pane in Launchpad, 1:18 right below the query editor pane. 1:24 This is pure JSON, so we just need to name the argument. 1:26 In this case, that will be directorToAdd. 1:30 Followed by a colon and curly braces. 1:36 Inside the curly braces, add the only required director property which is name. 1:40 I'm gonna go ahead and use my name for the value, but you can use your name or 1:47 any other name you see fit. 1:51 Now, let's write the mutation. 1:53 Since we are passing in a query variable, we need to let GraphQL know at the top 1:58 of our mutation declaration what type of variabless being passed in. 2:04 The way that we do this is after the mutation declaration, 2:09 we add parenthesis then we put in a dollar sign followed by the name of 2:13 the query variable we'll be using, which in this case is directorToAdd. 2:17 To let the endpoint know that this is actually the type of argument that it 2:23 requires, we need to specify that our query variable is of type director input. 2:29 And we'll go ahead and put an exclamation point to let them know that it's required. 2:37 Now for the addDirectorToMovie endpoint we can plug in the arguments. 2:44 For movieId, let's go ahead and 2:51 use movie_0, that should be the first movie in our list. 2:55 And then for director, we're just gonna go ahead and 3:00 reference that directorToAdd variable that we declared at the top. 3:04 Note that again we've included this dollar sign before the variable name. 3:10 That's how GraphQL knows that we're using these query variables. 3:14 Just like every other operation we've run, 3:19 we need to specify which field we want to appear on the response. 3:22 We know that addDirectorToMovie returns a movie, so this time around we'll 3:26 return the title, the tagline and the directors list. 3:31 This will be a list of the directors and 3:38 should include the director that we've just added. 3:41 As we know director is not a scalar type, instead it's an object type. 3:45 Therefore we need to specify which fields we'd 3:50 like to see in those nested directors. 3:53 In our case, we're going to look for the ID in the name fields. 3:55 Go ahead and click play. 4:00 There we go. 4:03 We can see our title, our tagline, and our directors list. 4:04 And as you can see, our new director Isaac Lee Morris, has been added to the list. 4:12 And that's it, you've learned how to use mutations with both scalar and 4:18 input object types, which will wrap-up this stage. 4:22 In the next stage, 4:26 I'll go over some tricks you can use to make using GraphQL even simpler. 4:27
You need to sign up for Treehouse in order to download course files.Sign up