Become a member
to unlock all features

Level Up!

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


    Create a GraphQL Object Type and Query a List of Objects

    Eve PorcelloEve Porcello

    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.



    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




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

    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.

    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.

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