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 Connected Data with the GraphQL Query Language

    Eve PorcelloEve Porcello
    graphqlGraphQL

    One of the most useful features of a GraphQL query is that you can collect data about multiple types in one request. In this video, we'll look at how to use nested fields to gather data about the Customer type and the Pet type.

    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 sent queries for the pet type, and if we search the schema for pet, we should see all the available fields. Now, there's another main type that's part of this API, and that's called customer.

    The real power of GraphQL starts to show up when we start to talk about connecting data points. Let's write some queries that connect the pet type with the customer type. The query that we'll send is petByID.

    This is going to take in ID as an argument. This is going to return Biscuit. There's another field on pet called inCareOf. inCareOf is going to return the customer who has checked out this pet. Biscuit has been checked out by this customer.

    To draw the line from customer to pet, we're going to use the allCustomers query. allCustomers is going to return a list of customers, so we can ask for their name, their username, and we also are going to get their current pets.

    currentPets is going to return a list of any pets that they currently have checked out. That connection is made on the currentPets field. allCustomers returns a list of customers for each of those customer objects that are going to have a list of current pets that are checked out.

    This could be an empty array, or this could return a list of custom objects. To go back the other way, inCareOf is going to connect a pet with a customer. On the inCareOf field, we're going to return the customer who has checked out this pet.