Every GraphQL API is defined by a schema written in SDL. Learn object types, scalar fields, and the special Query type that is the entry point for every read.
Why: a GraphQL server is defined by its schema, written in the Schema Definition Language (SDL). The schema lists every type and field that exists — so it is the single source of truth and the contract between client and server. Tools read it to give you autocomplete and validate queries before they run.
# An object type: a thing with fields
type Book {
id: ID!
title: String!
pages: Int
published: Boolean!
}Why: fields have types. The built-in scalars are Int, Float, String, Boolean, and ID (a unique identifier serialized as a string). A trailing ! means non-null — the field is guaranteed present. No ! means it may be null. Getting nullability right is part of designing a good schema.
type User {
id: ID! # required, never null
name: String! # required
age: Int # optional — may be null
rating: Float
isActive: Boolean!
}Why: Query is a special root type — its fields are the queries clients are allowed to start from. Everything readable hangs off Query. A field can take arguments (book takes an id) and returns a type. This is what made "query { books }" work in the first lesson.
type Query {
books: [Book!]! # a non-null list of non-null Books
book(id: ID!): Book # one book by id, or null if not found
}
type Book {
id: ID!
title: String!
}Note: the brackets and bangs compose, and reading them precisely matters. [Book] is a nullable list of nullable books. [Book!] is a nullable list whose items are non-null. [Book!]! is a non-null list of non-null books — the list always exists and never contains null. Learn to read these left-to-right.
Book ─ a book, or null
Book! ─ a book, never null
[Book] ─ a list (or null) that may contain nulls
[Book!] ─ a list (or null) of non-null books
[Book!]! ─ a list that always exists, of non-null books