Create a GraphQL Object Type and Query a List of Objects

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet

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.

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.