Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video, we will go over how to fetch data in the shape we want it, including only the pieces that we care about.
Course Repo: Use this GitHub Repo to follow along with this course
New Terms:
- Scalar field - A field with a simple data type, such as a String, Int, or Boolean
- Object type field - A field which returns another object, which can be broken down into further scalar or object type fields
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we're familiar
with all the pieces,
0:00
let's bring it all together into a query.
0:02
In this video, we'll try adding and
0:05
removing fields to see how that
affects our query results.
0:07
Once again,
we'll be working with movie objects.
0:12
You can find the link to
the starting point for
0:15
this lesson in the Teacher's Notes
under Stage 2- Video 2.
0:17
As we saw in the previous stage,
each movie has five fields,
0:22
id, title, tagline, revenue and studio.
0:26
With the exception of studio, each of
these is what's known as a scalar field.
0:32
This means they are simple
fields like strings and
0:38
numbers that can't be broken
down into more nested objects.
0:41
Let's start by writing a small query
that selects a single scalar field id.
0:45
As we saw in the last video,
we'll start with the query
0:51
declaration followed by
the topMovieByRevenue endpoint name,
0:56
followed by the field we want to select,
id.
1:02
Go ahead and click Play.
1:06
And there we see we've got the movie
with the highest box office
1:10
revenue with only its id.
1:13
So what would happen if we ask GraphQL for
1:16
a field that doesn't exist
on the movie object?
1:18
Let's try it.
1:21
After Id, add a poster field.
1:25
Go ahead and run the query.
1:29
As you can see,
1:33
GraphiQL's thrown an error,
Cannot query field poster on type movie.
1:34
This is because movie objects don't
contain a corresponding poster field.
1:39
Now that we’ve gone over scaler fields,
let me introduce you to another type.
1:46
The object type field.
1:51
These fields are the meat of what really
makes GraphiQL such a pleasure to use.
1:54
An object type field is a field
the returns another object,
1:59
which means we can also specify which
fields we want to return from that object.
2:03
Let's try it out.
2:09
First, remove the invalid
poster field from earlier so
2:10
that we don't get an error.
2:14
And we'll go ahead and
replace it with a studio field.
2:17
This will return an object of the studio
type, which has two fields, name and
2:22
location.
2:27
Since this is an object type field,
2:28
we'll add some curly braces
after the field name.
2:30
And then we'll specify which subfields
we want returned inside of them.
2:35
For now, just enter the name field.
2:39
If this seems familiar,
that's because it is.
2:43
Object type fields are queried
just like endpoints.
2:46
You have some curly braces containing a
list of the fields that we want returned.
2:49
Go ahead and run the updated query.
2:54
There you have it!
2:59
Inside of our movie object,
3:00
we can see the name of the studio
where our movie was created.
3:01
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up