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

    Query an Enumeration Type with GraphQL

    Eve PorcelloEve Porcello
    graphqlGraphQL

    In the GraphQL Query Language, an enum or enumeration type is a restricted list of values for a particular field. We'll query an enum field, category, in this lesson to find out the different pet categories. This lesson also takes a look at the GraphQL schema for the API.

    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
    Transcript

    Transcript

    Instructor: We've sent the allPets query, so we have some information about the pet, their name and their weight. Now, I know that Biscuit and Jungle are cats, because I know that cats, but I might not have all of them memorized.

    We want to be able to find out what category the pet belongs to. When we're using GraphQL Playground, we can hit control-space. This will surface all of the different fields that are available on this query.

    Let's go ahead and add category to our query. When I click play, we should see category being returned. Now, categories look like strings, but they all seem pretty similar. Cat, dog, stingray, and rabbit. GraphQL is a query language for your API, but it's also a type system for your API.

    The GraphQL spec describes a schema definition language, which we'll use to define all of the different queries and all of the different types that are available on this API. If you click the schema tab, you can take a look at this schema.

    allPets returns a list of pets, and I can access all of the different fields on this pet type by scrolling down. Here, it says that category returns an option called petCategory. petCategory is an enumeration type that represents a restricted list of options for this field. Cat, dog, rabbit, and stingray.

    Here's another tip you can use when exploring APIs with GraphQL Playground. You can hover over one of these field names and press command. This will allow you to click on that field, and it'll take you directly to that field definition in the schema.

    We can do that for weight, command-click, command-click for name, but we can also do it for allPets and for totalPets.

    Discuss

    Discuss