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 benull.
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.
Argument types
Genreis an enum, optional (no!).firstis anIntwith a default of 10.
Drills
Answer fast — trust the rule.
Which keyword declares a type meant only for passing structured data as an argument?
Which root operation type must every schema have?
A query asks for book(id: "b1") { isbn99 }, a field that does not exist. When is this caught?
What lets an editor autocomplete fields from a live GraphQL server?
Remember
- Six named kinds, two wrappers, three roots (only
Queryrequired). - 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.
