Schema definition language
Read and write GraphQL's schema definition language: type declarations, descriptions, the Query root type, extend, and what the parser can and cannot catch.
Schema definition language
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The schema definition language (SDL) is a compact, language-independent way to write down every type, field, and argument of a GraphQL API.
- Declarations look like type Book { title: String! }; a description goes in triple quotes directly above the thing it documents, while # starts a comment that tools ignore.
- Every schema needs a Query type, the entry point for reads; Mutation and Subscription are optional siblings.
- extend type adds fields to a type declared elsewhere, which lets a large schema be split across files.
- Parsing catches syntax errors and unknown names, but rules such as a missing interface field are checked by schema validation, not by buildSchema alone.
Why a separate language
A GraphQL API is defined by its schema, so the schema has to be something every tool can read: servers, editors, code generators, documentation sites. If it were written as JavaScript objects, only JavaScript tools would understand it. So the GraphQL specification defines a small language for writing schemas, the schema definition language, or SDL. A Go server, a Java server, and a Node server all accept the same text.
SDL describes what exists. It says nothing about how to fetch it. Fetching lives in resolvers, which come later in resolvers.
The shape of SDL
Here is the schema for a small bookshelf:
"""A book on the shelf."""
type Book {
"Display title."
title: String!
"""
Year first published.
Can be missing for old records.
"""
year: Int
}
type Query {
book: Book
}
Reading it from the top:
type Book { ... }declares an object type with two fields.title: String!isname: Type. The!means the field can never be null; that is its own lesson, non-null and lists.type Queryis the entry point. Clients can only begin a query at a field ofQuery, sobookis how they reach aBook.- The strings above things are descriptions. They are Markdown and show up in GraphiQL’s docs panel and in introspection.
A line starting with # is a comment. It is for people reading the file and disappears when the schema is parsed. I ran this schema through buildSchema and printed it back with printSchema from graphql-js 17.1.0: the descriptions come back, a # comment I added at the top does not. If a note should reach API consumers, use a description.
The root types
There are up to three entry points: Query for reads, Mutation for writes, and Subscription for streams of events. Only Query is required. If you must call the root type something else, a schema block names it:
schema { query: Root }
type Root { hi: String }
I ran that and { hi } returned yo from a root value of { hi: 'yo' }. In practice almost everyone uses the default names so tools behave predictably.
Splitting a schema with extend
A schema grows. One file with every type gets unwieldy. SDL supports extend, which adds to a type declared somewhere else:
type Query { book: Book }
extend type Query { ping: String }
extend type Book { pages: Int }
Parsing those three together and printing gives Query with both book and ping, and Book with pages appended to its fields. Teams use this to keep each feature’s types in its own module, and federation, much later, builds on the same idea.
What gets caught, and when
Try some mistakes in graphql-js 17.1.0 with buildSchema:
buildSchema(`type Book { title String }`)
// Syntax Error: Expected ":", found Name "String".
buildSchema(`type Query { pet: Pett }`)
// Unknown type "Pett".
buildSchema(`type Query { a: Int } type Query { b: Int }`)
// There can be only one type named "Query".
Those are structural errors and buildSchema rejects them immediately. Rules about the meaning of the schema are different. buildSchema does not check them; validateSchema (which execution runs for you) does. For example, a schema with no Query type parses fine, and validateSchema then reports “Query root type must be provided.” The same goes for an object that claims to implement an interface without providing its fields. You will see that in the interfaces lesson. The lesson: if you build a schema by hand, call validateSchema or just run a query in a test, rather than trusting that no exception means the schema is valid.
Code-first, for context
You can build the same schema with JavaScript constructors such as GraphQLObjectType and skip SDL. That style is called code-first and has real advantages, which schema-first and code-first compares. Either way the result is the same schema, and you can print it as SDL. SDL is the lingua franca for reading, sharing, and reviewing a schema, which is why it is where this roadmap starts.
What the video covers
Apollo’s “What is a GraphQL Schema?” is under three minutes, from March 2022. Its description says the schema describes the shape of all available data and all operations, lets you split code into entities, and lets frontend and backend teams agree on it upfront and work independently. That is a concept video; I could not read a transcript and do not know how much SDL syntax it shows, which is why this lesson supplies the syntax and the runnable checks. A longer episode that covers the whole type system, including the schema chapter at 00:37, is linked as the runner-up.
Where this fits
A schema is made of types, and the simplest types are the built-in scalars, the leaves of every query. Next: scalar types and custom scalars.
Check yourself
What is the difference between a description and a comment in SDL?
A description in triple quotes is part of the schema and shows up in docs and introspection, while a # comment is dropped when the schema is parsed.
What is the one type every schema must define?
The Query type, which holds the root fields clients can read from.
How can two files both contribute fields to Query?
One declares type Query and the others use extend type Query to add fields, and the merged schema contains all of them.
What does a typo like title String instead of title: String produce?
A syntax error when the schema is parsed, such as Expected a colon, before any query can run.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.