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
Published 6 years ago
Updated 4 years ago

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.

egghead
egghead
~ 11 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today