Overview
With SDL basics in place, it's time to define the Catstronauts schema for our homepage feature. In this lesson, we will:
- Create a
schema.graphqlfile in the server - Define the
TrackandAuthorobject types - Add a
Querytype with atracksForHomefield - Document schema types and fields with descriptions
✏️ Let's define that schema
In the editor, let's navigate to the server/src/ directory. In there, we'll create a file called schema.graphql.
📂 server┗ 📂 src┗ 📄 schema.graphql
Referring back to our mockup, for each learning track, we need the following:
- Title
- Thumbnail
- Length
- Modules Count
- Author name
- Author picture
How do we organize this data into types?
Well, we could create a single type named Track, shove all those fields into it, and call it a day. But would that make sense from a business domain point of view? Not really. For starters, a single author might create multiple tracks, and that author's information would be needlessly duplicated across multiple locations.
Instead, we need to think in terms of standalone entities. We'll start with two: Tracks and Authors.
The Track type
We'll start with the type Track that represents a particular learning track. Let's define the type and add a description:
"Educational modules teaching a specific topic"type Track {# Fields go here}
Now for the track's fields, we'll have:
idof typeID!titleof typeString!authorof typeAuthor!(we'll define theAuthortype when we're done withTrack)thumbnailof typeString(a URL to the image for the track's card)lengthof typeIntmodulesCountof typeInt
Here's our complete Track type:
"Educational modules teaching a specific topic"type Track {id: ID!title: String!author: Author!thumbnail: Stringlength: IntmodulesCount: Int}
Not seeing the nice GraphQL syntax highlighting you want? Check out the GraphQL: Syntax Highlighting extension for VS Code.
How do we determine which of these fields should be allowed to be null? One approach is to make the schema reflect our "business" domain rules. In our case, a track could exist without a thumbnail, but a track without a title or author doesn't make any sense from our "business" point of view.
Add some nice descriptions for each of these fields, then let's move on to the Author type.
The Author type
"An author of a track or module"type Author {# Fields go here}
The Author type contains only three fields:
idof typeID!nameof typeString!photoof typeString
Here's the complete type:
"An author of a track or module"type Author {id: ID!name: String!photo: String}
Excellent, our first feature is now fully represented in our schema. These are the data types we'll be able to retrieve.
We're still missing one piece though: how do we return these Track and Author types? We'll need to define a Query type.
The Query type
The Query type is defined like any other object type:
type Query {# Fields go here}
The fields of this type are entry points into the rest of our schema. These are the top-level fields that our client can query for.
For now, we're only interested in fetching the track list for our homepage. Let's name that specific query tracksForHome to make it as descriptive as possible. We want this query to return a non-null list of non-null Tracks. We'll also add a nice description:
type Query {"Fetch a list of tracks for the homepage grid"tracksForHome: [Track!]!}
Our schema is now fully defined to support our first feature!
Code review
Practice
Query type?Define a Query type with a field, popularPlanets, that returns a non-null List of non-null Planet types. Remember to give your field a description!
Up next
Now that our base schema is ready, we can start working on our GraphQL server.
Share your questions and comments about this lesson
Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.