3. GraphQL arguments
2m

Overview

We've added track and module to the schema, but we still need a way to fetch one track by ID. let us pass that ID into the .

In this lesson, we will:

  • Add a track entry point on the Query type
  • Learn how are defined in a schema
  • Accept a required id on the track

Querying for a specific track

We've updated our schema with new and the Module type, but we still need a way to for a specific track by its ID. To do that, we'll need to add another entry point to our schema.

In our schema.graphql file in the server/src folder, we can see that we only have a single entry point to our API so far: the tracksForHome that returns a list of Track objects. Let's add another one called track, which will return a single Track.

Add the following to our schema's Query type:

track: Track

We'll be able to specify which unique track we're for by giving this an argument.

🤔 How to use GraphQL arguments

An is a value you provide for a particular in your . The schema defines the arguments that each of your fields accepts.

Your can then use a 's provided to help determine how to populate the data for that field. Arguments can help you retrieve specific objects, filter through a set of objects, or even transform the field's returned value. A that performs a search usually provides the user's search term as an argument.

Doodle of resolver function retrieving a specific object from data-land using an argument

To define an for a in our schema, we add parentheses after the field name. Inside, we write the name of the argument followed by a colon, then the type of that argument, like String or Int. If we have more than one , we can separate them with commas.

Illustration showing the syntax breakdown of using GraphQL arguments

🙌 Using arguments

In our case, we want to use the track's ID as our . Following the syntax rules, we'll add the parentheses, then the name id, with the type ID which is required.

Inside the Query type in schema.graphql, update the track to:

"Fetch a specific track by ID"
track(id: ID!): Track

Code review

Practice

Where can we add entry points to our schema?
Which of these are reasons to use arguments in a query?
Code Challenge!

Update the schema below to add two new entry points: 1) galaxy to query for a specific galaxy, which takes an argument id of type non-nullable ID and returns a non-nullable Galaxy. 2) planets, which takes two arguments: galaxy of non-nullable type String (to specify the galaxy name the planet belongs to), and minPopulation of nullable type Int (to filter for planets with a minimum population). This field returns a nullable array of nullable Planet types.

Loading...
Loading progress

Up next

That's it for the definition. We now have our schema up-to-date for the feature we're implementing. Onwards to the !

Previous

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.