Specifying Default Variables3:42 with Isaac Lee Morris
How do you handle mutations where some of your arguments are missing? The answer is default variables.
(Apollo's Launchpad has been deprecated, so your screen will look different)
When you’re ready to try GraphQL in a real project, check out the Apollo GraphQL client at https://www.apollographql.com/. It’s made by the creators of Launchpad, and provides easy-to-use client frameworks for React, Angular, and Vanilla JS.
In our final video I'm gonna show you one last trick 0:00 to make your GraphQL more reusable. 0:04 In the last lesson you learned how to extract 0:07 arguments into variables that could be passed in with a variables object. 0:10 But what about when you've only got some of the arguments you need? 0:14 GraphQL helps out here as well, with a feature called default variables. 0:18 Let's hop on over to Launchpad and check it out. 0:23 In this example, we'll be calling a new endpoint, random movie by year. 0:26 This endpoint doesn't take a movie ID, like the movie by ID endpoint, 0:32 instead this movie takes an integer named year. 0:37 And returns a random movie that was released that year. 0:40 Before we write the query go ahead and take a peak at the schema. 0:44 First you'll see that we've added another field to the movie type. 0:49 This new field is called releaseYear and has a type of Int for integer. 0:53 Like I mentioned before, 1:00 the randomMovieByYear endpoint takes just a single argument with a type of Int. 1:02 Which is required and returns a Movie object. 1:07 Back to writing our query. 1:10 This query is gonna look very similar to the query from the last video. 1:12 We'll start with the declaration, Followed by declaring the variable, 1:17 which will be named $year, and we'll have a type of Int. 1:24 Inside the curly brackets for our query we'll call the randomMovieByYear 1:32 endpoint using that $year variable for the year argument. 1:37 In our response, let's return the movie Iid the title, and the releaseYear. 1:45 Going back to our variable declaration, 1:53 you might have noticed that we didn't require the year variable this time. 1:55 So, what happens if we run this query without passing in any variable values? 1:59 Go ahead and click the play button, and there you'll see GraphiQL throws an error. 2:04 This is because, even though our query doesn't require the year, 2:10 the randomMovieByYear, endpoint inside of it does. 2:14 So, if our query isn't given the variable it needs, 2:18 we need to specify a default value to be used. 2:21 This is where default variables come in. 2:25 To define a default variable after the variable 2:28 type add an = followed by the value. 2:33 In this case we'll say 2000, now run the query again. 2:37 There we go, we've got one movie. 2:46 If we click it again, we get another movie. 2:49 What if we want to pass in a variable using our query variables pane? 2:52 Well, let's go ahead and try doing that. 2:56 I'll go ahead and pass in a variable for year, this time we'll use 2001. 3:02 Go ahead and run that query. 3:08 And there we go, we've got a movie from 2001. 3:11 As you can see, since we specified a variable value, GraphiQL used that one 3:14 instead of the default, and returned a random movie from 2001. 3:19 So that wraps up our introduction to GraphQL, 3:25 I encourage you to keep playing around in Launchpad. 3:28 Try defining some of your own queries, mutations, and fragments. 3:31 As always, don't forget to check out the teacher's notes for 3:35 helpful resources on GraphQL, see you. 3:38
You need to sign up for Treehouse in order to download course files.Sign up