6. SDL Crash Course
2m

SDL Crash Course

Scalar types

  • Int
  • Float
  • String
  • Boolean
  • ID

Nullability

An ! indicates that a field is non-nullable.

For example:

title: String! means the title field is non-nullable. It must return a String title. yearReleased: Int means the yearReleased field is nullable. It can return null. cast: [Actor!] means the cast field can return null. It can also return a list, where the elements of the list must be Actor types, the elements cannot be null.

Descriptions

"""
A movie available for
streaming
"""
type Movie {
"The name of the movie"
title: String!
yearReleased: Int
cast: [Actor!]
}

Add a type or field description using (") double quotes just above the type or field. You can also use triple double quotes (""") for multi-line descriptions.

Root types

Client entry points into the API

Queries: Read operations

type Query {
}

Mutations: Write operations

type Mutation {
}

Field arguments

movie(id: ID!): Movie

Arguments are used by resolvers to determine how to populate the data for that field.

  • Know which specific object to retrieve
  • Filter through a set of objects
  • Transform the field's returned value

Code-along: Add types and fields to enable querying for all track titles

  1. Open up schema.graphql.

  2. Add the Track type. It needs an id field and a title.

    "A track is a group of Modules that teaches about a specific topic"
    type Track {
    id: ID!
    "The track's title"
    title: String!
    }
  3. Now how do we query that? We need to add an entry point, through the Query type. Let's remove this example field, the deleteMe field.

    type Query {
    - """
    - This is just a placeholder for the starter code.
    - Delete it once you're able to run a test query in Sandbox!
    - """
    - deleteMe: String
    }
  4. Since we removed the deleteMe field, we also want to remove it from our mocks file, so head over to mocks.js and remove the deleteMe field from the Query type.

    mocks.js
    Query: () => ({
    - deleteMe: "Hello I am an example to be deleted!"
    }),
  5. Back to schema.graphql. Add a field to the Query type called tracksForHome, this will return all the tracks we need for our homepage.

    This field returns an array of Track objects. We'll also make the list non-nullable, and the Track elements inside non-nullable. So the client can rest assured that they'll always be getting something back to show on the homepage.

    type Query {
    "Query to get tracks array for the homepage grid"
    tracksForHome: [Track!]!
    }

Check your work

Save your changes and let's check our API!

Head over to Sandbox again, where we should still be connected to localhost:4000.

Run this query:

query GetTracksForHome {
tracksForHome {
id
title
}
}

We should get back:

{
"data": {
"tracksForHome": [
{
"id": "c_0",
"title": "Astro Kitty, Space Explorer"
},
{
"id": "c_0",
"title": "Astro Kitty, Space Explorer"
}
]
}
}
Previous
Next