In the first lesson, the query asking for isbn was rejected straight away, before the server even looked for the book. How did the server know isbn doesn't exist? From the schema: the full list of what can be asked from a GraphQL API. It holds which types exist, which fields each type has, and what kind of value each field may hold. The server rejects every query that doesn't fit the schema, before a single resolver runs.
It's like an official form at an office: every box has a kind of entry (a number, text, a date), boxes marked with a star must be filled in, and some boxes can only be ticked from the choices printed there. In a GraphQL schema, the star is !.
The Bookshop schema
A schema is written in SDL (Schema Definition Language). This is the Bookshop schema used throughout the GraphQL track:
type Query {
book(id: ID!): Book
books(status: StockStatus): [Book!]!
author(id: ID!): Author
authors: [Author!]!
}
type Mutation {
createBook(input: CreateBookInput!): Book!
updateStock(id: ID!, stock: Int!): Book
}
enum StockStatus {
IN_STOCK
SOLD_OUT
}
type Book {
id: ID!
title: String!
price: Int!
stock: Int!
status: StockStatus!
author: Author!
}
type Author {
id: ID!
name: String!
books: [Book!]!
}
input CreateBookInput {
title: String!
authorId: ID!
price: Int!
stock: Int!
}
type Queryis the entry point for reading data. Each of its fields can be written at the outermost level of a query, such asbook(id: 1).type Mutationis the entry point for changing data. It's covered in the "Mutations" lesson.type Bookandtype Authorare object types: groups of fields.author: Author!links a book to its author, andbooks: [Book!]!goes the other way.enumandinputare covered below.
Built-in scalars
The fields at the very ends, the ones with no fields inside, hold a scalar (or an enum). GraphQL has exactly five built-in scalars:
| Scalar | Holds | Example in the Bookshop |
|---|---|---|
Int |
a 32-bit whole number, from -2,147,483,648 to 2,147,483,647 | price: 89000 |
Float |
a decimal number | (not used yet) |
String |
text | title: "The Rainbow Troops" |
Boolean |
true or false |
(not used yet) |
ID |
a unique identifier, sent as text | id: "2" |
An ID is always sent as a string in the response. The Bookshop data stores the id as the number 2, yet book(id: 2) { id } answers "id": "2". As an argument, both id: 2 and id: "2" are accepted.
Date is not a built-in scalar. A schema that uses publishedAt: Date without declaring it is rejected with Unknown type "Date".. If you need one, declare a custom scalar with scalar Date, and the server decides how it is read and sent (usually a string such as "2026-10-11").
The exclamation mark: non-null
! means non-null: the value may not be null.
- On a result field,
title: String!is the server's promise: every book has a title. The client doesn't have to check fornull. - On an argument,
book(id: ID!)means the client must sendid. Without it the query is rejected:Argument "Query.book(id:)" of type "ID!" is required, but it was not provided.In graphql-js 16, which Apollo Server 5 uses, the message readsField "book" argument "id" of type "ID!" is required, but it was not provided. - Without
!, a field may benull.book(id: ID!): Bookcan answernullfor a book that doesn't exist.
If a resolver returns null for a field with !, the server doesn't send that null. It records the error Cannot return null for non-nullable field Book.author., then moves the null up to the nearest place that may be null.
Lists and the exclamation mark
[Book] means a list of books. The exclamation mark can sit in two places: inside the brackets for each item, outside for the list itself.
The Bookshop schema uses books: [Book!]! and author: Author!. Before you look at the table below, guess: if the author resolver fails for one of the three books, how many books reach the client?
This is what happens to the query { books { title author { name } } } when the author of Beauty Is a Wound (the third book) becomes null:
books in the schema |
Meaning | The data the client gets |
|---|---|---|
[Book!]! |
the list is never null, nor are its items | null |
[Book]! |
the list is never null, its items may be | { "books": [book 1, book 2, null] } |
[Book!] |
the list may be null, its items may not | { "books": null } |
[Book] |
both may be null | { "books": [book 1, book 2, null] } |
All four also carry the error Cannot return null for non-nullable field Book.author. with "path": ["books", 2, "author"]. The null from author moves up to its book (Book! may not be null), then to the list, until it finds a place that may be null. In the real Bookshop schema ([Book!]!) there is no such place, so the whole data becomes null. Zero books reach the client.
If author is allowed to be null (author: Author), the result is three books, the third with "author": null, and no error at all. So use ! for what is truly always there, and leave nullable what comes from a service that can fail.
Enums: a fixed set of choices
Use an enum when a field may only hold one of a few fixed choices. StockStatus is only IN_STOCK or SOLD_OUT, and the status resolver fills it from the stock (a stock of 0 means SOLD_OUT):
{
books(status: SOLD_OUT) {
title
stock
status
}
}
{ "data": { "books": [{ "title": "This Earth of Mankind", "stock": 0, "status": "SOLD_OUT" }] } }
Enum values are written without quotes. The GraphQL engine checks them before any resolver runs:
books(status: EMPTY)is rejected:Value "EMPTY" does not exist in "StockStatus" enum.books(status: "SOLD_OUT")is rejected too, because that's a string:Enum "StockStatus" cannot represent non-enum value: "SOLD_OUT". Did you mean the enum value "SOLD_OUT"?
If status were a String, "EMPTY" would pass validation and the resolver would have to check it itself. An enum gets that check for free from the GraphQL engine.
Input types: arguments shaped like objects
An argument with many fields is gathered into an input type. createBook(input: CreateBookInput!) takes one object holding title, authorId, price and stock. An input type is written with input, not type, and may only hold scalars, enums, lists or other input types. Book can't be used as an argument, and CreateBookInput can't be a field's result. You'll use it in the "Mutations" lesson.
⚠️ Type choices that cause trouble
!on every field. Onenulldeep inside can wipe out the wholedata, as in the Beauty Is a Wound example above. Put!only on fields that are always there.- A String for a value with fixed choices. A wrong value like
"EMPTY"gets through. Use an enum. - Thinking an
IDis a number. The response says"2", a string. Compare withString(book.id) === args.idin a resolver. Intfor a number that's too big.3000000000is rejected:Int cannot represent non 32-bit signed integer value: 3000000000. Prices in whole cents fit up to 2,147,483,647 cents, which is $21,474,836.47.
📌 Interfaces and unions
An interface is a list of fields that every type implementing it must have. A union is only a group of types, with no required shared fields.
interface Product {
id: ID!
title: String!
price: Int!
}
type Ebook implements Product {
id: ID!
title: String!
price: Int!
fileSizeMb: Float!
}
union SearchResult = Book | Author
(Book is written type Book implements Product { ... } with its fields from before.) On a field of type Product, title and price can be asked for directly because they're always there. On SearchResult, fields must be asked for per type with an inline fragment ... on Type, and __typename tells you the actual type:
{
search(text: "ea") {
__typename
... on Book {
title
}
... on Author {
name
}
}
}
{
"data": {
"search": [
{ "__typename": "Book", "title": "This Earth of Mankind" },
{ "__typename": "Book", "title": "Beauty Is a Wound" },
{ "__typename": "Author", "name": "Andrea Hirata" }
]
}
}
Asking for title directly on search is rejected: Cannot query field "title" on type "SearchResult". Did you mean to use an inline fragment on "Product" or "Book"?
📌 Directives: @skip, @include, @deprecated
A directive is a marker starting with @ that changes how something is processed.
@skip(if: true)skips a field when its condition istrue:{ book(id: 1) { title price @skip(if: true) } }answers{ "book": { "title": "The Rainbow Troops" } }. The opposite,@include(if: false), also drops the field. The condition is usually a variable, covered in the "Variables & Fragments" lesson.@deprecated(reason: "...")goes in the schema to mark a field as outdated:stockCount: Int! @deprecated(reason: "Use stock."). The field can still be asked for and still answers, but tools like GraphiQL show a warning with the reason.@specifiedBy(url: "...")goes on a custom scalar to point to the rules of its format:scalar Date @specifiedBy(url: "https://datatracker.ietf.org/doc/html/rfc3339"). Tools read this URL through introspection (specifiedByURL). This directive has been in the specification since the October 2021 edition.@oneOfgoes on an input type where exactly one field must be filled in. This directive joined the specification in the September 2025 edition, and it already works in graphql-js 16 and 17. Without@oneOf, you need two fields (bookByIdandbookByTitle) or your own check in the resolver.
input BookBy @oneOf {
id: ID
title: String
}
type Query {
findBook(by: BookBy!): Book
}
findBook(by: { title: "This Earth of Mankind" }) and findBook(by: { id: 1 }) are accepted. Filling in both, or neither, is rejected before any resolver runs: Within OneOf Input Object type "BookBy", exactly one field must be specified, and the value for that field must be non-null. (graphql-js 16: OneOf Input Object "BookBy" must specify exactly one key.).
📌 Introspection: you can ask about the schema
The schema itself can be asked about with a query. The special fields starting with two underscores, such as __type, are called introspection:
{
__type(name: "Book") {
name
kind
fields {
name
}
}
}
The answer is "name": "Book", "kind": "OBJECT", and the fields id, title, price, stock, status, author. GraphiQL and editors build their autocomplete from this. Some servers, Apollo Server for one, turn introspection off by default in production. The Lab's engine doesn't support introspection yet.
TL;DR: The schema, written in SDL, is the contract of every type and field that can be asked for; a query that doesn't fit is rejected before any resolver runs. There are only five built-in scalars: Int (32-bit), Float, String, Boolean, ID (sent as a string); Date isn't built in, so declare scalar Date. ! means non-null: on a result it's the server's promise, on an argument it's required. A null in a ! field moves up to the nearest place that may be null, so with [Book!]! and author: Author! one failing author makes the whole data null. Enums are for a fixed set of choices, validated automatically by the GraphQL engine (strings aren't). Input types are for arguments shaped like objects. An interface requires shared fields, a union has none and is read with ... on Type. @skip skips a field when its condition is true, @deprecated marks an outdated field with its reason.
⚡ The schema: the list of what can be asked
Write:
type Book {
id: ID!
title: String!
price: Int!
stock: Int!
status: StockStatus!
author: Author!
}
Guess first: if you need a publication date, can you simply write publishedAt: Date?
No. GraphQL has only five built-in scalars: Int, Float, String, Boolean and ID. A schema using Date without declaring it is rejected with Unknown type "Date"., so write scalar Date first.
Meaning: The schema is written in SDL and holds every type and field that can be asked for. A query that doesn't fit the schema is rejected before any resolver runs.
⚡ The exclamation mark means never null
Guess first: the schema says books: [Book!]! and author: Author!. The author resolver fails for one of the three books. How many books reach the client?
Zero. The null from author moves up to its book, then to the list, and finds no place that may be null. In the end the whole data becomes null.
Meaning: On a result field, ! is the server's promise; on an argument, it means the argument is required. Put ! only on fields that are truly always there.
⚡ Enums: a fixed set of choices
Write:
{
books(status: SOLD_OUT) {
title
stock
status
}
}
Meaning: Enum values are written without quotes. books(status: EMPTY) is rejected before any resolver runs, because StockStatus only knows IN_STOCK and SOLD_OUT.