Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Filter a GraphQL Query Result Using Arguments

    Eve PorcelloEve Porcello
    graphqlGraphQL

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 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.

    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.

    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.