1 00:00:00,570 --> 00:00:05,308 Now that we've got the anatomy down, it's time to write our first GraphQL mutation. 2 00:00:05,308 --> 00:00:09,270 We'll be using the create movie endpoint from the last video, 3 00:00:09,270 --> 00:00:12,850 which creates a new movie, and saves it with the fields that we specify. 4 00:00:14,000 --> 00:00:18,780 As we've seen in previous stages, each movie has the following fields, 5 00:00:18,780 --> 00:00:23,230 id, title, tagline, revenue and studio. 6 00:00:23,230 --> 00:00:25,600 With the exception of the studio field, 7 00:00:25,600 --> 00:00:28,860 each of these is what's known as a scalar field. 8 00:00:28,860 --> 00:00:33,370 Recall that scalar fields are simple types like strings and numbers. 9 00:00:33,370 --> 00:00:37,860 In this lesson, we're gonna create a movie by calling the create movie end point. 10 00:00:37,860 --> 00:00:42,440 To see which arguments can be passed in create movie, go ahead and 11 00:00:42,440 --> 00:00:47,170 load up the launch pad for this video, which is listed in the teacher's notes. 12 00:00:47,170 --> 00:00:51,820 Scroll down in the back end editor to take a look at typeDefs again. 13 00:00:55,103 --> 00:01:00,570 Under createMovie, we can see that a title can be passed in with a type of string. 14 00:01:01,630 --> 00:01:06,270 The exclamation point here indicates that title must be passed in or 15 00:01:06,270 --> 00:01:09,010 else GraphQL will through an error. 16 00:01:09,010 --> 00:01:11,140 The other arguments tagline and 17 00:01:11,140 --> 00:01:15,640 revenue can be passed in optionally, but aren't required. 18 00:01:15,640 --> 00:01:20,310 For now, we'll just be assigning the title argument, which is a string. 19 00:01:20,310 --> 00:01:24,320 So, in our query editor, first we we'll declare the mutation, 20 00:01:26,900 --> 00:01:30,220 then we'll go ahead and follow that with curly braces. 21 00:01:30,220 --> 00:01:34,560 Inside the curly braces, specify the createMovie endpoint. 22 00:01:35,850 --> 00:01:39,570 Since we're passing information up to the createMovie endpoint, 23 00:01:39,570 --> 00:01:43,409 we'll need to use a new GraphQL concept, arguments. 24 00:01:43,409 --> 00:01:48,360 Arguments in GraphQL function much like arguments in any other language. 25 00:01:48,360 --> 00:01:52,000 You pass them into the query or mutation that's being called. 26 00:01:52,000 --> 00:01:54,990 In GraphQL's case, the arguments are named. 27 00:01:54,990 --> 00:01:58,720 So specify the name first, then the argument value. 28 00:01:58,720 --> 00:02:02,640 We'll specify arguments by adding a pair of parenthesis 29 00:02:02,640 --> 00:02:05,040 after the name of the endpoint. 30 00:02:05,040 --> 00:02:08,580 Like we saw in the schema for the createMovie inpoint, 31 00:02:08,580 --> 00:02:13,460 a title argument is required, but tagline and revenue are not. 32 00:02:13,460 --> 00:02:17,230 Let's try calling createMovie with only the tagline argument. 33 00:02:17,230 --> 00:02:20,030 So inside of those parenthesis, go ahead and 34 00:02:20,030 --> 00:02:25,070 type in tagline followed by whatever value you want to assign. 35 00:02:27,420 --> 00:02:30,937 This time around we're just gonna ask for 36 00:02:30,937 --> 00:02:35,267 id and the response, then run it by clicking play. 37 00:02:38,093 --> 00:02:42,052 As you can see, we get an error telling us that we're missing the title argument 38 00:02:42,052 --> 00:02:45,410 because that's required for creating a movie. 39 00:02:45,410 --> 00:02:49,581 To pass in the title, we'll type in the name of the argument, 40 00:02:49,581 --> 00:02:51,757 title, followed by the value. 41 00:02:54,111 --> 00:02:56,910 I'm gonna add Casablanca. 42 00:02:56,910 --> 00:02:59,530 But feel free to use any movie title you'd like. 43 00:03:02,250 --> 00:03:04,490 In front of the curly braces, we'll go ahead and 44 00:03:04,490 --> 00:03:10,570 choose the id, title, and tagline fields. 45 00:03:10,570 --> 00:03:12,940 Then go ahead and click play, to run this mutation. 46 00:03:15,070 --> 00:03:18,580 As you can see, the movie was successfully created. 47 00:03:18,580 --> 00:03:23,045 The id field was automatically generated and the movie was assigned the title and 48 00:03:23,045 --> 00:03:24,762 the tagline that we passed in. 49 00:03:24,762 --> 00:03:28,523 Let's run one more mutation, this time without a tagline. 50 00:03:35,091 --> 00:03:42,000 I'm gonna make Cleopatra this time, I'll click, play, to run the mutation. 51 00:03:43,983 --> 00:03:45,181 And there we go. 52 00:03:45,181 --> 00:03:47,735 This time since we didn't pass in a tagline, 53 00:03:47,735 --> 00:03:51,850 we can see that the tag line field is null, which means that it's empty.