Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The Schema  ›  Lesson

The Type System

Type System 12 minA GraphQL schema is built from six named kinds of type (scalar, enum, object, interface, union and input object), the list and non-null wrappers, and the Query, Mutation and Subscription root types, and the server validates every request against it.

The Type System

Every query in this course has leaned on a schema you have only seen in pieces. Now it is time to build it deliberately. The schema is not documentation that might drift from the truth: it is the rulebook the server checks every request against, before a single resolver runs.

The rule

Six named kinds, two wrappers, three roots.

A schema is written in the schema definition language (SDL). Every named type is one of six kinds, and the keyword tells you which:

Keyword Kind Used for
scalar Scalar Single values: Int, String, a custom Date
enum Enum One of a fixed set of values: FANTASY, MYSTERY
type Object Output data with fields: Book, Author
interface Interface Fields that several object types share
union Union "One of these object types"
input Input object Structured arguments: AddReviewInput

Two wrappers modify any type where it is used:

  • [Book] is a list of books.
  • Book! is non-null: the value can never be null.

Three root types are the entry points. Query is required; Mutation and Subscription are optional:

type Query {
  book(id: ID!): Book
  books(genre: Genre, first: Int = 10): [Book!]!
}

type Mutation {
  addReview(input: AddReviewInput!): Review!
}

type Subscription {
  reviewAdded(bookId: ID!): Review!
}

Because every field and argument has a known type, the server can validate a request completely before running it: an unknown field, a missing required argument or a string where an Int belongs is rejected with an error, and no resolver is called.

The schema also describes itself. Special introspection fields such as __schema and __type(name: "Book") let tools ask the server which types and fields exist. That is how editors autocomplete queries and how documentation explorers are generated.

The shortcut

Faced with any type in a schema, read the keyword first, then the wrappers. The keyword gives the kind; the brackets and ! tell you how many values and whether null is possible.

Reading `books(genre: Genre, first: Int = 10): [Book!]!`Step 1 of 4
  1. Argument types

    Genre is an enum, optional (no !). first is an Int with a default of 10.

Drills

Answer fast — trust the rule.

quick drill

Which keyword declares a type meant only for passing structured data as an argument?

quick drill

Which root operation type must every schema have?

quick drill

A query asks for book(id: "b1") { isbn99 }, a field that does not exist. When is this caught?

quick drill

What lets an editor autocomplete fields from a live GraphQL server?

Remember

  • Six named kinds, two wrappers, three roots (only Query required).
  • Read the keyword first for the kind, then the brackets and ! for shape and nullability.
  • Trap: the schema is enforced, not advisory; invalid requests fail before execution.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead a field signature

Explain everything the schema line addReview(input: AddReviewInput!): Review! on the Mutation type tells you: which kinds of type are involved, what the wrappers mean, what a client must send and what it gets back. Then say what happens if a client sends addReview with no input.