An excerpt from the repository, fetched Nov 13, 2026.
Read the project’s introduction
GraphQL
The GraphQL specification is edited in the markdown files found in
/spec the latest release of which is published at
https://graphql.github.io/graphql-spec/.
The latest draft specification can be found at
https://graphql.github.io/graphql-spec/draft/ which tracks the latest commit to
the main branch in this repository.
Previous releases of the GraphQL specification can be found at permalinks that
match their release tag. For
example, https://graphql.github.io/graphql-spec/October2016/. If you are linking
directly to the GraphQL specification, it's best to link to a tagged permalink
for the particular referenced version.
Overview
This is a Working Draft of the Specification for GraphQL, a query language for
APIs created by Facebook.
The target audience for this specification is not the client developer, but
those who have, or are actively interested in, building their own GraphQL
implementations and tools.
In order to be broadly adopted, GraphQL will have to target a wide variety of
backend environments, frameworks, and languages, which will necessitate a
collaborative effort across projects and organizations. This specification
serves as a point of coordination for this effort.
Looking for help? Find resources
from the community.
Getting Started
GraphQL consists of a type system, query language and execution semantics,
static validation, and type introspection, each outlined below. To guide you
through each of these components, we've written an example designed to
illustrate the various pieces of GraphQL.
This example is not comprehensive, but it is designed to quickly introduce the
core concepts of GraphQL, to provide some context before diving into the more
detailed specification or the
GraphQL.js reference implementation.
The premise of the example is that we want to use GraphQL to query for
information about characters and locations in the original Star Wars trilogy.
Type System
At the heart of any GraphQL implementation is a description of what types of
objects it can return, described in a GraphQL type system and returned in the
GraphQL Schema.
For our Star Wars example, the
starWarsSchema.ts
file in GraphQL.js defines this type system.
The most basic type in the system will be Human, representing characters like
Luke, Leia, and Han. All humans in our type system will have a name, so we
define the Human type to have a field called "name". This returns a String,
and we know that it is not null (since all Humans have a name), so we will
define the "name" field to be a non-nullable String. Using a shorthand notation
that we will use throughout the spec and documentation, we would describe the
human type as:
type Human {
name: String
}
This shorthand is convenient for describing the basic shape of a type system;
the JavaScript implementation is more full-featured, and allows types and fields
to be documented. It also sets up the mapping between the type system and the
underlying data; for a test case in GraphQL.js, the underlying data is a
set of JavaScript objects,
but in most cases the backing data will be accessed through some service, and
this type system layer will be responsible for mapping from types and fields to
that service.
A common pattern in many APIs, and indeed in GraphQL is to give objects an ID
that can be used to refetch the object. So let's add that to our Human type.
We'll also add a string for their home planet.
type Human {
id: String
name: String
homePlanet: String
}
Since we're talking about the Star Wars trilogy, it would be useful to describe
the episodes in which each character appears. To do so, we'll first define an
enum, which lists the three episodes in the trilogy:
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
Now we want to add a field to Human describing what episodes they were in.
This will return a list of Episodes:
type Human {
id: String
name: String
appearsIn: [Episode]
homePlanet: String
}
Now, let's introduce another type, Droid:
type Droid {
id: String
name: String
appearsIn: [Episode]
primaryFunction: String
}
Now we have two types! Let's add a way of going between them: humans and droids
both have friends. But humans can be friends with both humans and droids. How do
we refer to either a human or a droid?
If we look, we note that there's common functionality between humans and droids;
they both have IDs, names, and episodes in which they appear. So we'll add an
interface, Character, and make both Human and Droid implement it. Once we
have that, we can add the friends field, that returns a list of Characters.
Our type system so far is:
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
interface Character {
id: String
name: String
friends: [Character]
appearsIn: [Episode]
}
type Human implements Character {
id: String
name: String
friends: [Character]
appearsIn: [Episode]
homePlanet: String
}
type Droid implements Character {
id: String
name: String
friends: [Character]
appearsIn: [Episode]
primaryFunction: String
}
One question we might ask, though, is whether any of those fields can return
null. By default, null is a permitted value for any type in GraphQL, since
fetching data to fulfill a GraphQL query often requires talking to different
services that may or may not be available. However, if the type system can
guarantee that a type is never null, then we can mark it as Non Null in the type
system. We indicate that in our shorthand by adding an "!" after the type. We
can update our type system to note that the id is never null.
Note that while in our current implementation, we can guarantee that more fields
are non-null (since our current implementation has hard-coded data), we didn't
mark them as non-null. One can imagine we would eventually replace our hardcoded
data with a backend service, which might not be perfectly reliable; by leaving
these fields as nullable, we allow ourselves the flexibility to eventually
return null to indicate a backend error, while also telling the client tha
Read the full README on GitHub