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