Mutating with Scalar Type3:52 with Isaac Lee Morris
In this video, we will briefly talk over the createMovie endpoint, showing that title is required but tagline is not. We will then return the Movie object with the newly added fields.
(Apollo's Launchpad has been deprecated, so your screen will look different)
Now that we've got the anatomy down, it's time to write our first GraphQL mutation. 0:00 We'll be using the create movie endpoint from the last video, 0:05 which creates a new movie, and saves it with the fields that we specify. 0:09 As we've seen in previous stages, each movie has the following fields, 0:14 id, title, tagline, revenue and studio. 0:18 With the exception of the studio field, 0:23 each of these is what's known as a scalar field. 0:25 Recall that scalar fields are simple types like strings and numbers. 0:28 In this lesson, we're gonna create a movie by calling the create movie end point. 0:33 To see which arguments can be passed in create movie, go ahead and 0:37 load up the launch pad for this video, which is listed in the teacher's notes. 0:42 Scroll down in the back end editor to take a look at typeDefs again. 0:47 Under createMovie, we can see that a title can be passed in with a type of string. 0:55 The exclamation point here indicates that title must be passed in or 1:01 else GraphQL will through an error. 1:06 The other arguments tagline and 1:09 revenue can be passed in optionally, but aren't required. 1:11 For now, we'll just be assigning the title argument, which is a string. 1:15 So, in our query editor, first we we'll declare the mutation, 1:20 then we'll go ahead and follow that with curly braces. 1:26 Inside the curly braces, specify the createMovie endpoint. 1:30 Since we're passing information up to the createMovie endpoint, 1:35 we'll need to use a new GraphQL concept, arguments. 1:39 Arguments in GraphQL function much like arguments in any other language. 1:43 You pass them into the query or mutation that's being called. 1:48 In GraphQL's case, the arguments are named. 1:52 So specify the name first, then the argument value. 1:54 We'll specify arguments by adding a pair of parenthesis 1:58 after the name of the endpoint. 2:02 Like we saw in the schema for the createMovie inpoint, 2:05 a title argument is required, but tagline and revenue are not. 2:08 Let's try calling createMovie with only the tagline argument. 2:13 So inside of those parenthesis, go ahead and 2:17 type in tagline followed by whatever value you want to assign. 2:20 This time around we're just gonna ask for 2:27 id and the response, then run it by clicking play. 2:30 As you can see, we get an error telling us that we're missing the title argument 2:38 because that's required for creating a movie. 2:42 To pass in the title, we'll type in the name of the argument, 2:45 title, followed by the value. 2:49 I'm gonna add Casablanca. 2:54 But feel free to use any movie title you'd like. 2:56 In front of the curly braces, we'll go ahead and 3:02 choose the id, title, and tagline fields. 3:04 Then go ahead and click play, to run this mutation. 3:10 As you can see, the movie was successfully created. 3:15 The id field was automatically generated and the movie was assigned the title and 3:18 the tagline that we passed in. 3:23 Let's run one more mutation, this time without a tagline. 3:24 I'm gonna make Cleopatra this time, I'll click, play, to run the mutation. 3:35 And there we go. 3:43 This time since we didn't pass in a tagline, 3:45 we can see that the tag line field is null, which means that it's empty. 3:47
You need to sign up for Treehouse in order to download course files.Sign up