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

    Create a GraphQL Object Type and Query a List of Objects

    Eve PorcelloEve Porcello
    apolloApollo
    graphqlGraphQL

    Using the GraphQL Schema Definition Language, we can define types that reflect all of the objects available on our API. In this lesson, we will create a SkiDay type and return a list of SkiDay objects with an allDays query. We will also create an enum, which describes a restricted list of allowed values for a particular field.

    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: 00:00 We'll start by replacing the current value of skiDays, , with an array of objects. Each of these skiDay objects has a key for id, date, mountain, and conditions. Next we'll adjust the totalDays resolver to return the length of the array, and we'll add an allDays resolver to return skiDays. Now in the schema, allDays should return a list, so we're going to return a non-nullable list of skiDays.

    00:28 Next, we'll need to create the skiDay type. This is a custom object with keys that match our data, id, date, mountain, and conditions. Now conditions is going to return the conditions type.

    00:41 Conditions is an enum, an enumeration type in GraphQL returns one of a specific list of allowed values. Typically enum values are in all caps. Now in the playground, we can query totalDays. Then we'll query allDays, and we can query any of the fields on the allDays type.

    01:02 To recap, in GraphQL we can create custom types and we can return those types singularly or as lists.

    Discuss

    Discuss