Filter a GraphQL Query Result Using Arguments

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

Some GraphQL queries will have arguments. Arguments can be used to send additional options about the data set that you wish to receive. They may be used to specify sorting preferences, limit the number of records returned, or filter data. In this lesson, we will filter a list of pets by status.

To follow along with these queries, go to the Pet Library GraphQL Playground.

Instructor: [00:00] So far, we've gotten data about all of the pets in the library. We've gotten a list of our pets. We've gotten total pets. Total pets tells us that there are 25 pets that are part of our library, but I might want to filter this list to see just the pets that are available or just the pets that are checked out.

[00:18] To do this, I'm going to add a GraphQL argument. There's an argument on the total pets query called status. This will take in either available or checked out. If we add available, we'll see that there are 20 available pets. If we change this to checked out, we'll see that the total checked out pets is five.

[00:38] If we look at the total pets query in the schema, we'll see that it has this optional status argument. The value that I need to send is for pet status. Pet status is an enum, either available or checked out. Now as we saw before, total pets will work without a filter, but if I do provide a status filter, this will filter the list based on the value that I provide for pet status.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today