2. Updating our schema
3m

👀 Reviewing our design

Let's start with the mockup of the Track page our design team provided us with. Here we'll need to display more information about a track than what's contained in a card on the homepage.

Track page mock-up design

In addition to what we had in our card, a track needs to have:

  • a description
  • the number of views
  • the list of modules included in that track

For each module, we want:

  • the title
  • the length

✍️ Updating the schema

Let's open up the schema.js file in the server/src folder.

We'll start by updating the Track type. We'll add the description (which is a String) and the numberOfViews (an Integer). Oh, and let's not forget to give our fields nice comments!

Inside the Track type in schema.js:

"The track's complete description, can be in Markdown format"
description: String
"The number of times a track has been viewed"
numberOfViews: Int

Next, what should we do about our track's modules?

Putting our business glasses on, it looks reasonable to assume that a module should be a standalone type: a single track might include any number of modules, and one module might be part of multiple tracks. So, we'll create a separate Module type.

A module has an id, which is required. It also has a title, which is a required String and a length, which is an Int. We'll add comments for this type and its fields as well.

In schema.js, adding to typeDefs:

"A Module is a single unit of teaching. Multiple Modules compose a Track"
type Module {
id: ID!
"The Module's title"
title: String!
"The Module's length in minutes"
length: Int
}

Which of these are reasons to create a separate Module type?

Now that we have our Module type, let's go back to our Track type.

We know that a Track can have multiple modules, so let's add a field to Track called modules, which returns an array of Module objects. This array can't be null so we add an exclamation point at the end, and the entries in the array can't be null either, so we add another exclamation point after Module.

Inside the Track type in schema.js:

"The track's complete array of Modules"
modules: [Module!]!

Given this schema field: missions: [Mission!], which of the following statement is true:

Perfect, we have our types updated according to our client's needs. Are we done with the schema definition? Not quite! For the moment, we only have a tracksForHome query that returns all of the tracks at once.

In the next lesson, we'll update our schema to retrieve a specific track, using arguments.

Previous
Next