Filter a GraphQL Query Result Using Arguments

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet

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.