Repository: graphql/graphql-spec
Branch: main
Commit: 3c6695ce9cba
Files: 31
Total size: 661.9 KB
Directory structure:
gitextract_yl1f9z0u/
├── .github/
│ ├── ISSUE_TEMPLATE.md
│ ├── PULL_REQUEST_TEMPLATE.md
│ ├── algorithm-format-check.mjs
│ └── workflows/
│ └── ci.yml
├── .gitignore
├── .prettierignore
├── CONTRIBUTING.md
├── LICENSE.md
├── README.md
├── STYLE_GUIDE.md
├── build.sh
├── changelogs/
│ ├── October2021.md
│ └── September2025.md
├── cspell.yml
├── package.json
├── scripts/
│ ├── generate-contributor-list.mjs
│ └── update-appendix-specified-definitions.mjs
├── signed-agreements/
│ └── README.md
└── spec/
├── Appendix A -- Conformance.md
├── Appendix B -- Notation Conventions.md
├── Appendix C -- Grammar Summary.md
├── Appendix D -- Specified Definitions.md
├── GraphQL.md
├── Section 1 -- Overview.md
├── Section 2 -- Language.md
├── Section 3 -- Type System.md
├── Section 4 -- Introspection.md
├── Section 5 -- Validation.md
├── Section 6 -- Execution.md
├── Section 7 -- Response.md
└── metadata.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/ISSUE_TEMPLATE.md
================================================
!!! IMPORTANT !!!
Before creating your issue:
- Have a question? Find community resources at https://graphql.org/community/
- Find an editing mistake? Create a Pull Request with the edited fix! The Github
UI allows you to edit files directly, find the source files at:
https://github.com/graphql/graphql-spec/tree/master/spec
- Improvements to documentation? Head over to
https://github.com/graphql/graphql.github.io
- Feature request? First read
https://github.com/graphql/graphql-spec/blob/master/CONTRIBUTING.md and prefer
creating a Pull Request!
================================================
FILE: .github/PULL_REQUEST_TEMPLATE.md
================================================
!!! IMPORTANT !!!
Please Read https://github.com/graphql/graphql-spec/blob/master/CONTRIBUTING.md
before creating a Pull Request.
================================================
FILE: .github/algorithm-format-check.mjs
================================================
import { readFile, readdir } from "node:fs/promises";
const SPEC_DIR = new URL("../spec", import.meta.url).pathname;
/** @see {@link https://spec-md.com/#sec-Value-Literals} */
const valueLiteralsRegexp = /\{((?:[^{}]|(?:\{[^{}]*\}))+)\}/g;
process.exitCode = 0;
const filenames = await readdir(SPEC_DIR);
for (const filename of filenames) {
if (!filename.endsWith(".md")) {
continue;
}
const markdown = await readFile(`${SPEC_DIR}/${filename}`, "utf8");
/**
* Not strictly 'lines' since we try and group indented things together as if
* they were one line. Close enough though.
*/
const lines = markdown.split(/\n(?=[\S\n]|\s*(?:-|[0-9]+\.) )/);
for (let i = 0, l = lines.length; i < l; i++) {
const line = lines[i];
// Check algorithm is consistently formatted
{
// Is it an algorithm definition?
const matches = line.match(/^([a-z0-9A-Z]+)(\s*)\(([^)]*)\)(\s*):(\s*)$/);
const grammarMatches =
filename === "Section 2 -- Language.md" &&
line.match(/^([A-Za-z0-9]+) ::?\s+((\S).*)$/);
if (matches) {
const [, algorithmName, ns1, _args, ns2, ns3] = matches;
if (ns1 || ns2 || ns3) {
console.log(
`Bad whitespace in definition of ${algorithmName} in '${filename}':`
);
console.dir(line);
console.log();
process.exitCode = 1;
}
if (lines[i + 1] !== "") {
console.log(
`No empty space after algorithm ${algorithmName} header in '${filename}'`
);
console.log();
process.exitCode = 1;
}
for (let j = i + 2; j < l; j++) {
const step = lines[j];
if (!step.match(/^\s*(-|[0-9]+\.) /)) {
if (step !== "") {
console.log(
`Bad algorithm ${algorithmName} step in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
break;
}
if (!step.match(/[.:]$/)) {
console.log(
`Bad formatting for '${algorithmName}' step (does not end in '.' or ':') in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
if (step.match(/^\s*(-|[0-9]+\.)\s+[a-z]/)) {
console.log(
`Bad formatting of '${algorithmName}' step (should start with a capital) in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
const assertMatch = step.match(/^\s*(-|[0-9]+\.)\s*Assert([^:])/);
if (assertMatch) {
console.log(
`Bad formatting of '${algorithmName}' step (Assert should be immediately followed by ':'; found '${assertMatch[2]}') in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
const stepWithoutValueLiterals = step.replace(
valueLiteralsRegexp,
""
);
if (stepWithoutValueLiterals.match(/\b[A-Z][A-Za-z0-9]+\(/)) {
console.log(
`Bad formatting of '${algorithmName}' step (algorithm call should be wrapped in braces: \`{MyAlgorithm(a, b, c)}\`) in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
const valueLiterals = step.matchAll(valueLiteralsRegexp, "");
for (const lit of valueLiterals) {
const inner = lit[1];
if (inner.includes("{")) {
console.log(
`Bad formatting of '${algorithmName}' step (algorithm call should not contain braces: \`${lit}\`) in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
}
const trimmedInnerLine = step.replace(/\s+/g, " ");
if (
trimmedInnerLine.match(
/(?:[rR]eturn|is (?:not )?)(true|false|null)\b/
) &&
!trimmedInnerLine.match(/null or empty/)
) {
console.log(
`Potential bad formatting of '${algorithmName}' step (true/false/null should be wrapped in curly braces, e.g. '{true}') in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
}
} else if (grammarMatches) {
// This is super loosey-goosey
const [, grammarName, rest] = grammarMatches;
if (rest.trim() === "one of") {
// Still grammar, not algorithm
continue;
}
if (rest.trim() === "" && lines[i + 1] !== "") {
console.log(
`No empty space after grammar ${grammarName} header in '${filename}'`
);
console.log();
process.exitCode = 1;
}
while (lines[i + 1].trim() !== "") {
// Continuation of definition
i++;
}
if (!lines[i + 2].startsWith("- ")) {
// Not an algorithm; probably more grammar
continue;
}
for (let j = i + 2; j < l; j++) {
const step = lines[j];
if (!step.match(/^\s*(-|[0-9]+\.) /)) {
if (step !== "") {
console.log(`Bad grammar ${grammarName} step in '${filename}':`);
console.dir(step);
console.log();
process.exitCode = 1;
}
break;
}
if (!step.match(/[.:]$/)) {
console.log(
`Bad formatting for '${grammarName}' step (does not end in '.' or ':') in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
if (step.match(/^\s*(-|[0-9]\.)\s+[a-z]/)) {
console.log(
`Bad formatting of '${grammarName}' step (should start with a capital) in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
const trimmedInnerLine = step.replace(/\s+/g, " ");
if (
trimmedInnerLine.match(
/(?:[rR]eturn|is (?:not )?)(true|false|null)\b/
) &&
!trimmedInnerLine.match(/null or empty/)
) {
console.log(
`Potential bad formatting of '${grammarName}' step (true/false/null should be wrapped in curly braces, e.g. '{true}') in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
const assertMatch = step.match(/^\s*(-|[0-9]+\.)\s*Assert([^:])/);
if (assertMatch) {
console.log(
`Bad formatting of '${grammarName}' step (Assert should be immediately followed by ':'; found '${assertMatch[2]}') in '${filename}':`
);
console.dir(step);
console.log();
process.exitCode = 1;
}
}
}
}
// Check `- ...:` step is followed by an indent
{
const matches = line.match(/^(\s*)- .*:\s*$/);
if (matches) {
const indent = matches[1];
const nextLine = lines[i + 1];
if (!nextLine.startsWith(`${indent} `)) {
console.log(
`Lacking indent in '${filename}' following ':' character:`
);
console.dir(line);
console.dir(nextLine);
console.log();
// TODO: process.exitCode = 1;
}
}
}
}
}
if (process.exitCode === 0) {
console.log(`Everything looks okay!`);
} else {
console.log(`Please resolve the errors detailed above.`);
}
================================================
FILE: .github/workflows/ci.yml
================================================
name: CI
on:
push:
branches:
- main
pull_request:
jobs:
test-spelling:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run test:spelling
test-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run test:format
- run: npm run test:algorithm-format
test-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run test:build
publish:
if: github.ref == 'refs/heads/main'
needs:
- test-spelling
- test-format
- test-build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
keep_files: true
cname: spec.graphql.org
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
================================================
FILE: .gitignore
================================================
*.swp
*~
.*.haste_cache.*
.DS_Store
npm-debug.log
/build
/public
/gh-pages
/node_modules
================================================
FILE: .prettierignore
================================================
*.swp
*~
.*.haste_cache.*
.DS_Store
npm-debug.log
/build
/changelogs
/out
/gh-pages
/node_modules
/package.json
================================================
FILE: CONTRIBUTING.md
================================================
# GraphQL Specification Contribution Guide
GraphQL is still an evolving language. This repository contains the
specification text as well as Pull Requests with suggested improvements and
contributions.
Contributions that do not change the interpretation of the spec but instead
improve legibility, fix editorial errors, clear up ambiguity and improve
examples are encouraged. These "editorial changes" will normally be given the
["✏ Editorial" label](https://github.com/graphql/graphql-spec/issues?q=sort%3Aupdated-desc+is%3Aopen+label%3A%22%E2%9C%8F%EF%B8%8F+Editorial%22)
and are often merged by a spec editor with little process.
However, contributions that _do_ meaningfully change the interpretation of the
spec must follow an RFC (Request For Comments) process led by a _champion_
through a series of _stages_ intended to improve _visibility_, allow for
_discussion_ to reach the best solution, and arrive at _consensus_. This process
becomes ever more important as GraphQL's community broadens.
When proposing or weighing-in on any issue or pull request, consider the
[Code of Conduct](https://github.com/graphql/foundation/blob/main/CODE-OF-CONDUCT.md)
to better understand expected and unacceptable behavior.
## Contributing to GraphQL Libraries
A common point of confusion for those who wish to contribute to GraphQL is where
to start. In fact, you may have found yourself here after attempting to make an
improvement to a GraphQL library. Should a new addition be made to the GraphQL
spec first or a GraphQL library first? Admittedly, this can become a bit of a
[chicken-or-egg](https://en.wikipedia.org/wiki/Chicken_or_the_egg) dilemma.
GraphQL libraries seek to be "spec compliant", which means they discourage
changes that cause them to behave differently from the spec as written. However,
they also encourage pull requests for changes that accompany an RFC _proposal_
or RFC _draft_. In fact, a spec contribution RFC won't be _accepted_ until it
has experience being implemented in a GraphQL library.
To allow a library to remain spec compliant while also implementing _proposals_
and _drafts_, the library's maintainers may request that these new features are
disabled by default with opt-in option flags or they may simply wait to merge a
well-tested pull request until the spec proposal is _accepted_.
## Guiding Principles
GraphQL's evolution is guided by a few principles. Suggested contributions
should use these principles to guide the details of an RFC and decisions to move
forward. See editor Lee Byron talk about
[guiding principles at GraphQL Europe 2017](https://youtu.be/mePT9MNTM98?t=17m9s).
- **Backwards compatibility**
Once a query is written, it should always mean the same thing and return the
same shaped result. Future changes should not change the meaning of existing
schema or requests or in any other way cause an existing compliant GraphQL
service to become non-compliant for prior versions of the spec.
- **Performance is a feature**
GraphQL typically avoids syntax or behaviors that could jeopardize runtime
efficiency, or that make demands of GraphQL services which cannot efficiently
be fulfilled.
- **Favor no change**
As GraphQL is implemented in over a dozen languages under the collaboration of
hundreds of individuals, incorporating any change has a high cost.
Accordingly, proposed changes must meet a very high bar of added value. The
burden of proof is on the contributor to illustrate this value.
- **Enable new capabilities motivated by real use cases**
Every change should intend on unlocking a real and reasonable use case. Real
examples are always more compelling than theoretical ones, and common
scenarios are more compelling than rare ones. RFCs should do more than offer a
different way to reach an already achievable outcome.
- **Simplicity and consistency over expressiveness and terseness**
Plenty of behaviors and patterns found in other languages are intentionally
absent from GraphQL. "Possible but awkward" is often favored over more complex
alternatives. Simplicity (e.g. fewer concepts) is more important than
expressing more sophisticated ideas or writing less.
- **Preserve option value**
It's hard to know what the future brings; whenever possible, decisions should
be made that allow for more options in the future. Sometimes this is
unintuitive: spec rules often begin more strict than necessary with a future
option to loosen when motivated by a real use case.
- **Understandability is just as important as correctness**
The GraphQL spec, despite describing technical behavior, is intended to be
read by people. Use natural tone and include motivation and examples.
## RFC Contribution Champions
Contributing to GraphQL requires a lot of dedicated work. To set clear
expectations and provide accountability, each proposed RFC (request for
comments) must have a _champion_ who is responsible for addressing feedback and
completing next steps. An RFC may have multiple _champions_. The spec editors
are not responsible for completing RFCs which lack a _champion_ (though an
editor may be a _champion_ for an RFC).
An RFC which does not have a _champion_ may not progress through stages, and can
become stale. Stale proposals may be picked up by a new _champion_ or may be
_rejected_.
## RFC Contribution Stages
RFCs are guided by a _champion_ through a series of stages: _strawman_,
_proposal_, _draft_, and _accepted_ (or _rejected_), each of which has suggested
entrance criteria and next steps detailed below. RFCs typically advance one
stage at a time, but may advance multiple stages at a time. Stage advancements
typically occur during [Working Group](https://github.com/graphql/graphql-wg)
meetings, but may also occur on GitHub.
In general, it's preferable to start with a pull request so that we can best
evaluate the RFC in detail. However, starting with an issue is also permitted if
the full details are not worked out.
All RFCs start as either a _strawman_ or _proposal_.
## Stage 0: _Strawman_
An RFC at the _strawman_ stage captures a described problem or
partially-considered solutions. A _strawman_ does not need to meet any entrance
criteria. A _strawman's_ goal is to prove or disprove a problem and guide
discussion towards either rejection or a preferred solution. A _strawman_ may be
an issue or a pull request (though an illustrative pull request is preferrable).
_There is no entrance criteria for a Strawman_
As implied by the name
[strawman](https://en.wikipedia.org/wiki/Straw_man_proposal), the goal at this
stage is to knock it down (_reject_) by considering other possible related
solutions, showing that the motivating problem can be solved with no change to
the specification, or that it is not aligned with the _guiding principles_.
Once determined that the _strawman_ is compelling, it should seek the entrance
criteria for _proposal_.
## Stage 1: _Proposal_
An RFC at the _proposal_ stage is a solution to a problem with enough fidelity
to be discussed in detail. It must be backed by a willing _champion_. A
_proposal_'s goal is to make a compelling case for acceptance by describing both
the problem and the solution via examples and spec edits. A _proposal_ should be
a pull request.
_Entrance criteria:_
- Identified _champion_
- Clear explanation of problem and solution
- Illustrative examples
- Incomplete spec edits
- Identification of potential concerns, challenges, and drawbacks
A _proposal_ is subject to the same discussion as a _strawman_: ensuring that it
is well aligned with the _guiding principles_, is a problem worth solving, and
is the preferred solution to that problem. A _champion_ is not expected to have
confidence in every detail at this stage and should instead focus on identifying
and resolving issues and edge-cases. To better understand the technical
ramifications of the _proposal_, a _champion_ is encouraged to implement it in a
GraphQL library.
Most _proposals_ are expected to evolve or change and may be rejected.
Therefore, it is unwise to rely on a _proposal_ in a production GraphQL service.
GraphQL libraries _may_ implement _proposals_, though are encouraged to not
enable the _proposed_ feature without explicit opt-in.
## Stage 2: _Draft_
An RFC at the _draft_ stage is a fully formed solution. There is working group
consensus the problem identified should be solved, and this particular solution
is preferred. A _draft's_ goal is to precisely and completely describe the
solution and resolve any concerns through library implementations. A _draft_
must be a pull request.
_Entrance criteria:_
- Consensus the solution is preferred (typically via Working Group)
- Resolution of identified concerns and challenges
- Precisely described with spec edits
- Compliant implementation in GraphQL.js (might not be merged)
A _proposal_ becomes a _draft_ when the set of problems or drawbacks have been
fully considered and accepted or resolved, and the solution is deemed desirable.
A _draft_'s goal is to complete final spec edits that are ready to be merged and
implement the _draft_ in GraphQL libraries along with tests to gain confidence
that the spec text is sufficient.
_Drafts_ may continue to evolve and change, occasionally dramatically, and are
not guaranteed to be accepted. Therefore, it is unwise to rely on a _draft_ in a
production GraphQL Service. GraphQL libraries _should_ implement _drafts_ to
provide valuable feedback, though are encouraged not to enable the _draft_
feature without explicit opt-in when possible.
## Stage 3: _Accepted_
An RFC at the _accepted_ stage is a completed solution. According to a spec
editor it is ready to be merged as-is into the spec document. The RFC is ready
to be deployed in GraphQL libraries. An _accepted_ RFC must be implemented in
GraphQL.js.
_Entrance criteria:_
- Consensus the solution is complete (via editor or working group)
- Complete spec edits, including examples and prose
- Compliant implementation in GraphQL.js (fully tested and merged or ready to
merge)
A _draft_ is _accepted_ when the working group or editor has been convinced via
implementations and tests that it appropriately handles all edge cases; that the
spec changes not only precisely describe the new syntax and semantics but
include sufficient motivating prose and examples; and that the RFC includes
edits to any other affected areas of the spec. Once _accepted_, its _champion_
should encourage adoption of the RFC by opening issues or pull requests on other
popular GraphQL libraries.
An _accepted_ RFC is merged into the GraphQL spec's main branch by an editor and
will be included in the next released revision.
## Stage X: _Rejected_
An RFC may be _rejected_ at any point and for any reason. Most rejections occur
when a _strawman_ is proven to be unnecessary, is misaligned with the _guiding
principles_, or fails to meet the entrance criteria to become a _proposal_. A
_proposal_ may become _rejected_ for similar reasons as well as if it fails to
reach consensus or loses the confidence of its _champion_. Likewise a _draft_
may encounter unforeseen issues during implementations which cause it to lose
consensus or the confidence of its _champion_.
RFCs which have lost a _champion_ will not be _rejected_ immediately, but may
become _rejected_ if they fail to attract a new _champion_.
Once _rejected_, an RFC will typically not be reconsidered. Reconsideration is
possible if a _champion_ believes the original reason for rejection no longer
applies due to new circumstances or new evidence.
================================================
FILE: LICENSE.md
================================================
# Appendix: Copyright and Licensing
The GraphQL Specification Project is made available by the
[Joint Development Foundation](https://www.jointdevelopment.org/) Projects, LLC,
GraphQL Series. The current
[Working Group](https://github.com/graphql/graphql-wg) charter, which includes
the IP policy governing all working group deliverables (including
specifications, source code, and datasets) may be found at
[https://technical-charter.graphql.org](https://technical-charter.graphql.org).
**Copyright Notice**
Copyright © 2015-2018, Facebook, Inc.
Copyright © 2019-present, GraphQL contributors
THESE MATERIALS ARE PROVIDED “AS IS”. The parties expressly disclaim any
warranties (express, implied, or otherwise), including implied warranties of
merchantability, non-infringement, fitness for a particular purpose, or title,
related to the materials. The entire risk as to implementing or otherwise using
the materials is assumed by the implementer and user. IN NO EVENT WILL THE
PARTIES BE LIABLE TO ANY OTHER PARTY FOR LOST PROFITS OR ANY FORM OF INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER FROM ANY CAUSES
OF ACTION OF ANY KIND WITH RESPECT TO THIS DELIVERABLE OR ITS GOVERNING
AGREEMENT, WHETHER BASED ON BREACH OF CONTRACT, TORT (INCLUDING NEGLIGENCE), OR
OTHERWISE, AND WHETHER OR NOT THE OTHER MEMBER HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
**Licensing**
The licenses for the GraphQL Specification Project are:
| Deliverable | License |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Specifications | [Open Web Foundation Agreement 1.0 (Patent and Copyright Grants)](https://www.openwebfoundation.org/the-agreements/the-owf-1-0-agreements-granted-claims/owfa-1-0) |
| Source code | [MIT License](https://opensource.org/licenses/MIT) |
| Data sets | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
================================================
FILE: README.md
================================================
[](https://graphql.org/)
# GraphQL
The GraphQL specification is edited in the markdown files found in
[`/spec`](./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](https://github.com/graphql/graphql-spec/releases). 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](https://graphql.org/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](https://github.com/graphql/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](https://github.com/graphql/graphql-js/blob/main/src/__tests__/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 `Human`s 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:
```graphql
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](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsData.ts),
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.
```graphql
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:
```graphql
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 `Episode`s:
```graphql
type Human {
id: String
name: String
appearsIn: [Episode]
homePlanet: String
}
```
Now, let's introduce another type, `Droid`:
```graphql
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 `Character`s.
Our type system so far is:
```graphql
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 that the
error occurred.
```graphql
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
}
```
We're missing one last piece: an entry point into the type system.
When we define a schema, we define an object type that is the basis for all
query operations. The name of this type is `Query` by convention, and it
describes our public, top-level API. Our `Query` type for this example will look
like this:
```graphql
type Query {
hero(episode: Episode): Character
human(id: String!): Human
droid(id: String!): Droid
}
```
In this example, there are three top-level operations that can be done on our
schema:
- `hero` returns the `Character` who is the hero of the Star Wars trilogy; it
takes an optional argument that allows us to fetch the hero of a specific
episode instead.
- `human` accepts a non-null string as a query argument, a human's ID, and
returns the human with that ID.
- `droid` does the same for droids.
These fields demonstrate another feature of the type system, the ability for a
field to specify arguments that configure their behavior.
When we package the whole type system together, defining the `Query` type above
as our entry point for queries, this creates a GraphQL Schema.
This example just scratched the surface of the type system. The specification
goes into more detail about this topic in the "Type System" section, and the
[type](https://github.com/graphql/graphql-js/blob/main/src/type) directory in
GraphQL.js contains code implementing a specification-compliant GraphQL type
system.
### Query Syntax
GraphQL queries declaratively describe what data the issuer wishes to fetch from
whoever is fulfilling the GraphQL query.
For our Star Wars example, the
[starWarsQueryTests.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsQuery-test.ts)
file in the GraphQL.js repository contains a number of queries and responses.
That file is a test file that uses the schema discussed above and a set of
sample data, located in
[starWarsData.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsData.ts).
This test file can be run to exercise the reference implementation.
An example query on the above schema would be:
```graphql
query HeroNameQuery {
hero {
name
}
}
```
The initial line, `query HeroNameQuery`, defines a query with the operation name
`HeroNameQuery` that starts with the schema's root query type; in this case,
`Query`. As defined above, `Query` has a `hero` field that returns a
`Character`, so we'll query for that. `Character` then has a `name` field that
returns a `String`, so we query for that, completing our query. The result of
this query would then be:
```json
{
"hero": {
"name": "R2-D2"
}
}
```
Specifying the `query` keyword and an operation name is only required when a
GraphQL document defines multiple operations. We therefore could have written
the previous query with the query shorthand:
```graphql
{
hero {
name
}
}
```
Assuming that the backing data for the GraphQL server identified R2-D2 as the
hero. The response continues to vary based on the request; if we asked for
R2-D2's ID and friends with this query:
```graphql
query HeroNameAndFriendsQuery {
hero {
id
name
friends {
id
name
}
}
}
```
then we'll get back a response like this:
```json
{
"hero": {
"id": "2001",
"name": "R2-D2",
"friends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
{
"id": "1002",
"name": "Han Solo"
},
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
```
One of the key aspects of GraphQL is its ability to nest queries. In the above
query, we asked for R2-D2's friends, but we can ask for more information about
each of those objects. So let's construct a query that asks for R2-D2's friends,
gets their name and episode appearances, then asks for each of _their_ friends.
```graphql
query NestedQuery {
hero {
name
friends {
name
appearsIn
friends {
name
}
}
}
}
```
which will give us the nested response
```json
{
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{ "name": "Han Solo" },
{ "name": "Leia Organa" },
{ "name": "C-3PO" },
{ "name": "R2-D2" }
]
},
{
"name": "Han Solo",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{ "name": "Luke Skywalker" },
{ "name": "Leia Organa" },
{ "name": "R2-D2" }
]
},
{
"name": "Leia Organa",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{ "name": "Luke Skywalker" },
{ "name": "Han Solo" },
{ "name": "C-3PO" },
{ "name": "R2-D2" }
]
}
]
}
}
```
The `Query` type above defined a way to fetch a human given their ID. We can use
it by hard-coding the ID in the query:
```graphql
query FetchLukeQuery {
human(id: "1000") {
name
}
}
```
to get
```json
{
"human": {
"name": "Luke Skywalker"
}
}
```
Alternately, we could have defined the query to have a query parameter:
```graphql
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
```
This query is now parameterized by `$someId`; to run it, we must provide that
ID. If we ran it with `$someId` set to "1000", we would get Luke; set to "1002",
we would get Han. If we passed an invalid ID here, we would get `null` back for
the `human`, indicating that no such object exists.
Notice that the key in the response is the name of the field, by default. It is
sometimes useful to change this key, for clarity or to avoid key collisions when
fetching the same field with different arguments.
We can do that with field aliases, as demonstrated in this query:
```graphql
query FetchLukeAliased {
luke: human(id: "1000") {
name
}
}
```
We aliased the result of the `human` field to the key `luke`. Now the response
is:
```json
{
"luke": {
"name": "Luke Skywalker"
}
}
```
Notice the key is "luke" and not "human", as it was in our previous example
where we did not use the alias.
This is particularly useful if we want to use the same field twice with
different arguments, as in the following query:
```graphql
query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
name
}
leia: human(id: "1003") {
name
}
}
```
We aliased the result of the first `human` field to the key `luke`, and the
second to `leia`. So the result will be:
```json
{
"luke": {
"name": "Luke Skywalker"
},
"leia": {
"name": "Leia Organa"
}
}
```
Now imagine we wanted to ask for Luke and Leia's home planets. We could do so
with this query:
```graphql
query DuplicateFields {
luke: human(id: "1000") {
name
homePlanet
}
leia: human(id: "1003") {
name
homePlanet
}
}
```
but we can already see that this could get unwieldy, since we have to add new
fields to both parts of the query. Instead, we can extract out the common fields
into a fragment, and include the fragment in the query, like this:
```graphql
query UseFragment {
luke: human(id: "1000") {
...HumanFragment
}
leia: human(id: "1003") {
...HumanFragment
}
}
fragment HumanFragment on Human {
name
homePlanet
}
```
Both of those queries give this result:
```json
{
"luke": {
"name": "Luke Skywalker",
"homePlanet": "Tatooine"
},
"leia": {
"name": "Leia Organa",
"homePlanet": "Alderaan"
}
}
```
The `UseFragment` and `DuplicateFields` queries will both get the same result,
but `UseFragment` is less verbose; if we wanted to add more fields, we could add
it to the common fragment rather than copying it into multiple places.
We defined the type system above, so we know the type of each object in the
output; the query can ask for that type using the special field `__typename`,
defined on every object.
```graphql
query CheckTypeOfR2 {
hero {
__typename
name
}
}
```
Since R2-D2 is a droid, this will return
```json
{
"hero": {
"__typename": "Droid",
"name": "R2-D2"
}
}
```
This was particularly useful because `hero` was defined to return a `Character`,
which is an interface; we might want to know what concrete type was actually
returned. If we instead asked for the hero of Episode V:
```graphql
query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
```
We would find that it was Luke, who is a Human:
```json
{
"hero": {
"__typename": "Human",
"name": "Luke Skywalker"
}
}
```
As with the type system, this example just scratched the surface of the query
language. The specification goes into more detail about this topic in the
"Language" section, and the
[language](https://github.com/graphql/graphql-js/blob/main/src/language)
directory in GraphQL.js contains code implementing a specification-compliant
GraphQL query language parser and lexer.
### Validation
By using the type system, it can be predetermined whether a GraphQL query is
valid or not. This allows servers and clients to effectively inform developers
when an invalid query has been created, without having to rely on runtime
checks.
For our Star Wars example, the file
[starWarsValidationTests.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsValidation-test.ts)
contains a number of demonstrations of invalid operations, and is a test file
that can be run to exercise the reference implementation's validator.
To start, let's take a complex valid query. This is the `NestedQuery` example
from the above section, but with the duplicated fields factored out into a
fragment:
```graphql
query NestedQueryWithFragment {
hero {
...NameAndAppearances
friends {
...NameAndAppearances
friends {
...NameAndAppearances
}
}
}
}
fragment NameAndAppearances on Character {
name
appearsIn
}
```
And this query is valid. Let's take a look at some invalid queries!
When we query for fields, we have to query for a field that exists on the given
type. So as `hero` returns a `Character`, we have to query for a field on
`Character`. That type does not have a `favoriteSpaceship` field, so this query:
```graphql
# INVALID: favoriteSpaceship does not exist on Character
query HeroSpaceshipQuery {
hero {
favoriteSpaceship
}
}
```
is invalid.
Whenever we query for a field and it returns something other than a scalar or an
enum, we need to specify what data we want to get back from the field. Hero
returns a `Character`, and we've been requesting fields like `name` and
`appearsIn` on it; if we omit that, the query will not be valid:
```graphql
# INVALID: hero is not a scalar, so fields are needed
query HeroNoFieldsQuery {
hero
}
```
Similarly, if a field is a scalar, it doesn't make sense to query for additional
fields on it, and doing so will make the query invalid:
```graphql
# INVALID: name is a scalar, so fields are not permitted
query HeroFieldsOnScalarQuery {
hero {
name {
firstCharacterOfName
}
}
}
```
Earlier, it was noted that a query can only query for fields on the type in
question; when we query for `hero` which returns a `Character`, we can only
query for fields that exist on `Character`. What happens if we want to query for
R2-D2s primary function, though?
```graphql
# INVALID: primaryFunction does not exist on Character
query DroidFieldOnCharacter {
hero {
name
primaryFunction
}
}
```
That query is invalid, because `primaryFunction` is not a field on `Character`.
We want some way of indicating that we wish to fetch `primaryFunction` if the
`Character` is a `Droid`, and to ignore that field otherwise. We can use the
fragments we introduced earlier to do this. By setting up a fragment defined on
`Droid` and including it, we ensure that we only query for `primaryFunction`
where it is defined.
```graphql
query DroidFieldInFragment {
hero {
name
...DroidFields
}
}
fragment DroidFields on Droid {
primaryFunction
}
```
This query is valid, but it's a bit verbose; named fragments were valuable above
when we used them multiple times, but we're only using this one once. Instead of
using a named fragment, we can use an inline fragment; this still allows us to
indicate the type we are querying on, but without naming a separate fragment:
```graphql
query DroidFieldInInlineFragment {
hero {
name
... on Droid {
primaryFunction
}
}
}
```
This has just scratched the surface of the validation system; there are a number
of validation rules in place to ensure that a GraphQL query is semantically
meaningful. The specification goes into more detail about this topic in the
"Validation" section, and the
[validation](https://github.com/graphql/graphql-js/blob/main/src/validation)
directory in GraphQL.js contains code implementing a specification-compliant
GraphQL validator.
### Introspection
It's often useful to ask a GraphQL schema for information about what queries it
supports. GraphQL allows us to do so using the introspection system!
For our Star Wars example, the file
[starWarsIntrospectionTests.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsIntrospection-test.ts)
contains a number of queries demonstrating the introspection system, and is a
test file that can be run to exercise the reference implementation's
introspection system.
We designed the type system, so we know what types are available, but if we
didn't, we can ask GraphQL, by querying the `__schema` field, always available
on the root type of a Query. Let's do so now, and ask what types are available.
```graphql
query IntrospectionTypeQuery {
__schema {
types {
name
}
}
}
```
and we get back:
```json
{
"__schema": {
"types": [
{
"name": "Query"
},
{
"name": "Character"
},
{
"name": "Human"
},
{
"name": "String"
},
{
"name": "Episode"
},
{
"name": "Droid"
},
{
"name": "__Schema"
},
{
"name": "__Type"
},
{
"name": "__TypeKind"
},
{
"name": "Boolean"
},
{
"name": "__Field"
},
{
"name": "__InputValue"
},
{
"name": "__EnumValue"
},
{
"name": "__Directive"
}
]
}
}
```
Wow, that's a lot of types! What are they? Let's group them:
- **Query, Character, Human, Episode, Droid** - These are the ones that we
defined in our type system.
- **String, Boolean** - These are built-in scalars that the type system
provided.
- **`__Schema`, `__Type`, `__TypeKind`, `__Field`, `__InputValue`,
`__EnumValue`, `__Directive`** - These all are preceded with a double
underscore, indicating that they are part of the introspection system.
Now, let's try and figure out a good place to start exploring what queries are
available. When we designed our type system, we specified what type all queries
would start at; let's ask the introspection system about that!
```graphql
query IntrospectionQueryTypeQuery {
__schema {
queryType {
name
}
}
}
```
and we get back:
```json
{
"__schema": {
"queryType": {
"name": "Query"
}
}
}
```
And that matches what we said in the type system section, that the `Query` type
is where we will start! Note that the naming here was just by convention; we
could have named our `Query` type anything else, and it still would have been
returned here if we had specified it as the starting type for queries. Naming it
`Query`, though, is a useful convention.
It is often useful to examine one specific type. Let's take a look at the
`Droid` type:
```graphql
query IntrospectionDroidTypeQuery {
__type(name: "Droid") {
name
}
}
```
and we get back:
```json
{
"__type": {
"name": "Droid"
}
}
```
What if we want to know more about Droid, though? For example, is it an
interface or an object?
```graphql
query IntrospectionDroidKindQuery {
__type(name: "Droid") {
name
kind
}
}
```
and we get back:
```json
{
"__type": {
"name": "Droid",
"kind": "OBJECT"
}
}
```
`kind` returns a `__TypeKind` enum, one of whose values is `OBJECT`. If we asked
about `Character` instead:
```graphql
query IntrospectionCharacterKindQuery {
__type(name: "Character") {
name
kind
}
}
```
and we get back:
```json
{
"__type": {
"name": "Character",
"kind": "INTERFACE"
}
}
```
We'd find that it is an interface.
It's useful for an object to know what fields are available, so let's ask the
introspection system about `Droid`:
```graphql
query IntrospectionDroidFieldsQuery {
__type(name: "Droid") {
name
fields {
name
type {
name
kind
}
}
}
}
```
and we get back:
```json
{
"__type": {
"name": "Droid",
"fields": [
{
"name": "id",
"type": {
"name": null,
"kind": "NON_NULL"
}
},
{
"name": "name",
"type": {
"name": "String",
"kind": "SCALAR"
}
},
{
"name": "friends",
"type": {
"name": null,
"kind": "LIST"
}
},
{
"name": "appearsIn",
"type": {
"name": null,
"kind": "LIST"
}
},
{
"name": "primaryFunction",
"type": {
"name": "String",
"kind": "SCALAR"
}
}
]
}
}
```
Those are our fields that we defined on `Droid`!
`id` looks a bit weird there, it has no name for the type. That's because it's a
"wrapper" type of kind `NON_NULL`. If we queried for `ofType` on that field's
type, we would find the `String` type there, telling us that this is a non-null
String.
Similarly, both `friends` and `appearsIn` have no name, since they are the
`LIST` wrapper type. We can query for `ofType` on those types, which will tell
us what these are lists of.
```graphql
query IntrospectionDroidWrappedFieldsQuery {
__type(name: "Droid") {
name
fields {
name
type {
name
kind
ofType {
name
kind
}
}
}
}
}
```
and we get back:
```json
{
"__type": {
"name": "Droid",
"fields": [
{
"name": "id",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "String",
"kind": "SCALAR"
}
}
},
{
"name": "name",
"type": {
"name": "String",
"kind": "SCALAR",
"ofType": null
}
},
{
"name": "friends",
"type": {
"name": null,
"kind": "LIST",
"ofType": {
"name": "Character",
"kind": "INTERFACE"
}
}
},
{
"name": "appearsIn",
"type": {
"name": null,
"kind": "LIST",
"ofType": {
"name": "Episode",
"kind": "ENUM"
}
}
},
{
"name": "primaryFunction",
"type": {
"name": "String",
"kind": "SCALAR",
"ofType": null
}
}
]
}
}
```
Let's end with a feature of the introspection system particularly useful for
tooling; let's ask the system for documentation!
```graphql
query IntrospectionDroidDescriptionQuery {
__type(name: "Droid") {
name
description
}
}
```
yields
```json
{
"__type": {
"name": "Droid",
"description": "A mechanical creature in the Star Wars universe."
}
}
```
So we can access the documentation about the type system using introspection,
and create documentation browsers, or rich IDE experiences.
This has just scratched the surface of the introspection system; we can query
for enum values, what interfaces a type implements, and more. We can even
introspect on the introspection system itself. The specification goes into more
detail about this topic in the "Introspection" section, and the
[introspection](https://github.com/graphql/graphql-js/blob/main/src/type/introspection.ts)
file in GraphQL.js contains code implementing a specification-compliant GraphQL
query introspection system.
### Additional Content
This README walked through the GraphQL.js reference implementation's type
system, query execution, validation, and introspection systems. There's more in
both [GraphQL.js](https://github.com/graphql/graphql-js/) and specification,
including a description and implementation for executing queries, how to format
a response, explaining how a type system maps to an underlying implementation,
and how to format a GraphQL response, as well as the grammar for GraphQL.
### Contributing to this repo
This repository is managed by EasyCLA. Project participants must sign the free
([GraphQL Specification Membership agreement](https://preview-spec-membership.graphql.org)
before making a contribution. You only need to do this one time, and it can be
signed by
[individual contributors](https://individual-spec-membership.graphql.org/) or
their [employers](https://corporate-spec-membership.graphql.org/).
To initiate the signature process please open a PR against this repo. The
EasyCLA bot will block the merge if we still need a membership agreement from
you.
You can find
[detailed information here](https://github.com/graphql/graphql-wg/tree/main/membership).
If you have issues, please email
[operations@graphql.org](mailto:operations@graphql.org).
If your company benefits from GraphQL and you would like to provide essential
financial support for the systems and people that power our community, please
also consider membership in the
[GraphQL Foundation](https://foundation.graphql.org/join).
================================================
FILE: STYLE_GUIDE.md
================================================
**This document is a work in progress.**
# GraphQL Specification Style Guide
This document outlines the styles used in the GraphQL spec to aid editorial and
consistency. The writing style portions are inspired by the AP style guide. When
making changes to the GraphQL specification, please aim to be consistent with
this style guide.
## Auto-Formatting
The GraphQL specification is formatted using the `prettier` tool, so you should
not need to think about gaps between paragraphs and titles, nor about word
wrapping - this is handled for you.
## Headings
The GraphQL specification uses two types of headings: numbered headings and
unnumbered headings. All headings should be written in Title Case (see below).
### Numbered Headings
Lines beginning with a `#` will become numbered headings in the spec-md output.
```
# H1
## H2
### H3
#### H4
##### H5
```
### Unnumbered Headings
Unnumbered headings are added to split large blocks of text up without impacting
the spec numbering system. In the output are styled similarly to an H4. An
unnumbered heading is a line on its own that is bolded:
```md
\*\*This Is an Example of an Unnumbered Heading\*\*
```
### Title Case
Title case is used for headings. Every word in a heading (including words after
hyphens) should be capitalized, with the following exceptions:
- articles: a, an, the
- conjunctions under 4 letters in length: for, and, nor, but, or, yet, so, as,
if
- prepositions under 4 letters in length: in, at, to, on, off, of, for, vs., per
- directive names and type names are unchanged: @include, @specifiedBy,
\_\_EnumValue, \_\_Schema
All elements in hyphenated words follow the same rules, e.g. headings may
contain `Non-Null`, `Context-Free`, `Built-in` (`in` is a preposition, so is not
capitalized).
## Algorithms
A named algorithm definition starts with the name of the algorithm in
`PascalCase`, an open parenthesis, a comma-and-space separated list of
arguments, a close parenthesis and then a colon. It is followed by a blank
newline and a list of steps in the algorithm which may be numbered or bulleted.
Each step in an algorithm should either end in a colon (`:`) with an indented
step on the next line, or a fullstop (`.`). (A step after a step ending in a
full stop may or may not be indented, use your discretion.)
Indentation in algorithms is significant.
Every step in an algorithm should start with a capital letter.
```
MyAlgorithm(argOne, argTwo):
- Let {something} be {true}.
- For each {arg} in {argOne}:
- If {arg} is greater than {argTwo}:
- Let {something} be {false}.
- Otherwise if {arg} is less than {argTwo}:
- Let {something} be {true}.
- Return {something}.
```
## Definitions
For important terms, use
[Spec Markdown definition paragraphs](https://spec-md.com/#sec-Definition-Paragraph).
Definition paragraphs start with `::` and add the matching italicized term to
the [specification index](https://spec.graphql.org/draft/#index), making it easy
to reference them.
## Tone of Voice
The GraphQL specification is a reference document and should use neutral and
descriptive tone of voice.
**Favor the present tense**
The present tense is usually clearer and shorter:
✅ Present: The client then sends a request to the server.
❌ Future: The client will then send a request to the server.
**Write in the third person**
The specification should avoid the first person (“we,” “our”) and instead use
the third person or passive constructions when necessary. For example:
✅ “The server validates the request against the schema.”
❌ “We validate the request against the schema.”
**Use RFC 2119 keywords**
The specification should use the normative keywords defined in
[RFC 2119](https://www.rfc-editor.org/rfc/rfc2119) (**MUST**, **MUST NOT**,
**SHOULD**, **SHOULD NOT**, **MAY**). However, in the context of a sentence
these words should be lower-cased (e.g., “must,” “should,” “may”) in context of
a sentence. This avoids unnecessary visual emphasis while preserving normative
meaning.
✅ “A query must contain a single root operation type.”
❌ “A query MUST contain a single root operation type.”
**Avoid unnecessary hyphenation**
Only use hypenated compound words when either commonly used elsewhere in english
or technical writing. Otherwise prefer separate words or a non-hyphenated
compound word, whichever is most commonly accepted.
✅ User defined shorthand words are separated by whitespace
❌ User-defined short-hand words are separated by white-space
================================================
FILE: build.sh
================================================
#!/bin/bash -e
# This script publishes the GraphQL specification document to the web.
# Determine if this is a tagged release
GITTAG=$(git tag --points-at HEAD)
# Build the specification draft document
echo "Building spec draft"
mkdir -p public/draft
spec-md --metadata spec/metadata.json --githubSource "https://github.com/graphql/graphql-spec/blame/main/" spec/GraphQL.md > public/draft/index.html
# If this is a tagged commit, also build the release document
if [ -n "$GITTAG" ]; then
echo "Building spec release $GITTAG"
mkdir -p "public/$GITTAG"
spec-md --metadata spec/metadata.json --githubSource "https://github.com/graphql/graphql-spec/blame/$GITTAG/" spec/GraphQL.md > "public/$GITTAG/index.html"
fi
# Create the index file
echo "Rebuilding: / (index)"
HTML="
GraphQL Specification Versions
GraphQL
"
# Include latest draft
GITDATE=$(git show -s --format=%cd --date=format:"%a, %b %-d, %Y" HEAD)
HTML="$HTML
| Prerelease |
Working Draft |
$GITDATE |
|
"
GITHUB_RELEASES="https://github.com/graphql/graphql-spec/releases/tag"
for GITTAG in $(git tag -l --sort='-*committerdate') ; do
VERSIONYEAR=${GITTAG: -4}
TAGTITLE="${GITTAG%$VERSIONYEAR} $VERSIONYEAR"
TAGGEDCOMMIT=$(git rev-list -1 "$GITTAG")
GITDATE=$(git show -s --format=%cd --date=format:"%a, %b %-d, %Y" $TAGGEDCOMMIT)
HTML="$HTML
"
[ -z $HAS_LATEST_RELEASE ] && HTML="$HTML
| Latest Release | " || HTML="$HTML
| "
HAS_LATEST_RELEASE=1
HTML="$HTML
$TAGTITLE |
$GITDATE |
Release Notes |
"
done
HTML="$HTML
"
echo $HTML > "public/index.html"
================================================
FILE: changelogs/October2021.md
================================================
# October2021 Changelog
This describes the set of changes since the last edition of the GraphQL
specification, [June2018](https://spec.graphql.org/June2018/). It's intended to
ease the review of changes since the last edition for reviewers or curious
readers, but is not normative. Please read the
[specification document](https://spec.graphql.org/October2021/) itself for full detail
and context.
## Authors
While many have participated in the design and review RFC process, this is the
set of authors and RFC champions who have committed directly to the
specification text in this edition:
| Author |
| ------------------------------- |
| [ab-pm] |
| [Andreas Marek] |
| [Antoine Boyer] |
| [António Nuno Monteiro] |
| [Arun Ponniah Sethuramalingam] |
| [Benedikt Franke] |
| [Benjie Gillam] |
| [Brian Warner] |
| [Chris Brown] |
| [David Glasser] |
| [dugenkui] |
| [Edwin Shin] |
| [Evan Huus] |
| [Grant Wu] |
| [Görcsös Zoltán] |
| [Ivan Goncharov] |
| [Ivan Maximov] |
| [James Scott] |
| [Jamie Schouten] |
| [Joe Duarte] |
| [Kei Kamikawa] |
| [Lee Byron] |
| [Loren Sands-Ramshaw] |
| [Marais Rossouw] |
| [Marc Knaup] |
| [Martin Bonnin] |
| [Matt Farmer] |
| [Matt Mahoney] |
| [Michael Joseph Rosenthal] |
| [Mike Marcacci] |
| [Matthias Prechtl] |
| [Pascal Senn] |
| [Sebastian Redl] |
| [Tejas Kumar] |
| [Oleg Ilyenko] |
[ab-pm]: https://github.com/ab-pm
[Andreas Marek]: https://github.com/andimarek
[Antoine Boyer]: https://github.com/tinnou
[António Nuno Monteiro]: https://github.com/anmonteiro
[Arun Ponniah Sethuramalingam]: https://github.com/saponniah
[Benedikt Franke]: https://github.com/spawnia
[Benjie Gillam]: https://github.com/benjie
[Brian Warner]: https://github.com/brianwarner
[Chris Brown]: https://github.com/ccbrown
[David Glasser]: https://github.com/glasser
[dugenkui]: https://github.com/dugenkui03
[Edwin Shin]: https://github.com/eddies
[Evan Huus]: https://github.com/eapache
[Grant Wu]: https://github.com/grantwwu
[Görcsös Zoltán]: https://github.com/zgorcsos
[Ivan Goncharov]: https://github.com/IvanGoncharov
[Ivan Maximov]: https://github.com/sungam3r
[James Scott]: https://github.com/scottydocs
[Jamie Schouten]: https://github.com/maantje
[Joe Duarte]: https://github.com/JoeUX
[Kei Kamikawa]: https://github.com/Code-Hex
[Lee Byron]: https://github.com/leebyron
[Loren Sands-Ramshaw]: https://github.com/lorensr
[Marais Rossouw]: https://github.com/maraisr
[Marc Knaup]: https://github.com/fluidsonic
[Martin Bonnin]: https://github.com/martinbonnin
[Matt Farmer]: https://github.com/m14t
[Matt Mahoney]: https://github.com/mjmahone
[Michael Joseph Rosenthal]: https://github.com/micimize
[Mike Marcacci]: https://github.com/mike-marcacci
[Matthias Prechtl]: https://github.com/matprec
[Pascal Senn]: https://github.com/PascalSenn
[Sebastian Redl]: https://github.com/CornedBee
[Tejas Kumar]: https://github.com/TejasQ
[Oleg Ilyenko]: https://github.com/OlegIlyenko
Generated with:
```sh
git log June2018..HEAD --format="%an <%ae>%x0A%(trailers:key=Co-authored-by,valueonly)" -- spec | sort -uf
```
## Changes
Listed in reverse-chronological order (latest commit on top).
| Hash | Change | Authors |
| ---- | ------ | ------- |
| [ae1bab6](https://github.com/graphql/graphql-spec/commit/ae1bab6ff27cc331fc17c10af14ddd6b01512bf9) | Fix typo in Handling Field Errors (#876) | Antoine Boyer
| [fdeb37d](https://github.com/graphql/graphql-spec/commit/fdeb37d4c9528c44c3834913944880dce9f4e8bd) | Updated legal copy (#869) | [Lee Byron], [Brian Warner]
| [50bbaa8](https://github.com/graphql/graphql-spec/commit/50bbaa8bb2503ef9b3c3477d53b266c9e8205a31) | Editorial: Improve Introspection section (#857) | [Lee Byron], [Ivan Maximov], [Benjie Gillam]
| [debcdc3](https://github.com/graphql/graphql-spec/commit/debcdc381b6173adbb89a800fd2a0009a3df8568) | Improve clarity of built-in directives (#633) | [Ivan Maximov], [Lee Byron]
| [5b7b9f6](https://github.com/graphql/graphql-spec/commit/5b7b9f6415ec9861032ced3f061d6fab1406f376) | Add spellcheck to test suite (#858) | [Lee Byron]
| [086bb1f](https://github.com/graphql/graphql-spec/commit/086bb1f1a7f273b6d0787de525f1c23742374ae0) | Editorial: Define 'Schema Specification URL' (#856) | [Lee Byron]
| [40d025c](https://github.com/graphql/graphql-spec/commit/40d025ce5cb681454c0f6455c4d97e38313616d2) | fixed introspection field "specifiedBy" to "specifiedByURL" (#848) | [Kei Kamikawa], [Lee Byron]
| [61c50f2](https://github.com/graphql/graphql-spec/commit/61c50f28cb247539e4da930b5553d25ac3d8c792) | clarify that utf-8 is just a possible encoding of strings (#684) | [Andreas Marek], [Lee Byron]
| [d4777b4](https://github.com/graphql/graphql-spec/commit/d4777b428030347300f98873b5a760b5e29ae726) | Editorial: StringValue (#854) | [Lee Byron]
| [413b689](https://github.com/graphql/graphql-spec/commit/413b68974ef86b6cc8a9272bbc8360762eee3479) | RFC: __typename is not valid at subscription root (#776) | [Benjie Gillam], [Lee Byron]
| [9bb7b77](https://github.com/graphql/graphql-spec/commit/9bb7b777d0a5de26d94bb0e2993fdd4e22c13443) | Fix: Syntax highlighting in example (#851) | [Lee Byron]
| [c6eb911](https://github.com/graphql/graphql-spec/commit/c6eb91110f6f80550f8cac1c69de1c7808b93a21) | make clear that some String escape sequences are optional (#686) | [Andreas Marek], [Lee Byron]
| [044d1d4](https://github.com/graphql/graphql-spec/commit/044d1d459d5ed289a66422cc3a996a9265fe8efa) | Clarity in Section 3 (#847) | [Ivan Maximov]
| [a16d249](https://github.com/graphql/graphql-spec/commit/a16d2493f72020f63f3e689d61420c35944c953c) | State that built-in mandatory directives must be omitted (#816) | [Marais Rossouw], [Lee Byron]
| [d4a865a](https://github.com/graphql/graphql-spec/commit/d4a865adada9c78924cf29e1bc86d6ff6bcc5f15) | [RFC] Custom Scalar Specification URLs (#649) | [Evan Huus], [Matt Farmer], [Lee Byron]
| [9ec15e3](https://github.com/graphql/graphql-spec/commit/9ec15e365933caff4894aa640ac4c55f9f78e2de) | Spec edits to reduce "query ambiguity" (#777) | [Benjie Gillam], [Lee Byron]
| [f474e66](https://github.com/graphql/graphql-spec/commit/f474e666d6698f6fd061fafad862b977834a3d5b) | Editorial: Clarify meta-fields in introspection (#844) | [Lee Byron], [Benjie Gillam]
| [f8b75d3](https://github.com/graphql/graphql-spec/commit/f8b75d30970c75007f4418bf3e79112d65c2f187) | Editorial: Clarify document grammar rules (#840) | [Lee Byron]
| [0062610](https://github.com/graphql/graphql-spec/commit/0062610494be356b033b933023829fdbc7149a96) | Add CoerceResult() (#836) | [Lee Byron]
| [792671b](https://github.com/graphql/graphql-spec/commit/792671bcfc3d451514213b5f57d0f3dbe9068a9b) | __Type represents all types in the system (#775) | [Benjie Gillam], [Lee Byron]
| [d616285](https://github.com/graphql/graphql-spec/commit/d616285dad370ca4cf126482052e313a24249e1e) | Avoid parse ambiguity on type extensions (#598) | [Lee Byron]
| [adee896](https://github.com/graphql/graphql-spec/commit/adee896e83a01ba065e13df360d61eea225f2b8b) | Update 'Field Collection' explanation (#765) | [dugenkui]
| [c43d2f7](https://github.com/graphql/graphql-spec/commit/c43d2f7c8e706442662ccf95e6b7eaaeeab4b878) | Editorial: Clarify field aliases (#843) | [Lee Byron]
| [29bcc26](https://github.com/graphql/graphql-spec/commit/29bcc26766d7f62560bb5d760b39b14bc80063c4) | Editorial: Refine design principles (#839) | [Lee Byron]
| [567e05c](https://github.com/graphql/graphql-spec/commit/567e05cfa5fbe1405c652c5db0b4b65eca431cb1) | Editorial: Query shorthand (#841) | [Lee Byron]
| [2c2cea7](https://github.com/graphql/graphql-spec/commit/2c2cea7327fe8c01a819f7bf9ea9c9454e4946af) | Editorial: root types (#842) | [Lee Byron]
| [080c8b6](https://github.com/graphql/graphql-spec/commit/080c8b669967ccaa5021b9b5b1278807cbfe9c7a) | Editorial: clarify input object introspection (#838) | [Lee Byron]
| [9cb9a4b](https://github.com/graphql/graphql-spec/commit/9cb9a4b25ea975b4affbea6676cf857f8eb43967) | Replace 'query error' with 'request error' (#803) | [Benjie Gillam], [Lee Byron]
| [6dbef35](https://github.com/graphql/graphql-spec/commit/6dbef35869abbc10d7b3e3aef073ed0ebd1bf52c) | Clarify that Float does not include NaN or infinity (#780) | [David Glasser], [Lee Byron]
| [b47598f](https://github.com/graphql/graphql-spec/commit/b47598f49264d7018ef2c07e2dfc21b3e0503420) | Prettier pre-work: formatting tweaks (#832) | [Benjie Gillam], [Lee Byron]
| [d716d84](https://github.com/graphql/graphql-spec/commit/d716d84263a428c3e7f45962a2ed66a5e063d5d9) | Rename references from master to main (#835) | [Lee Byron]
| [f331650](https://github.com/graphql/graphql-spec/commit/f3316502285e46d7b9bea17e895ce79bfcb771e9) | Clarify how lexical tokens are separated (#748) | [Joe Duarte]
| [7546566](https://github.com/graphql/graphql-spec/commit/7546566bc8a9b3992e87c0a2656dab85319b3264) | fix spelling (#834) | [Lee Byron]
| [bed0a19](https://github.com/graphql/graphql-spec/commit/bed0a193436af5e3fc89b9a21a90debf7f5663a7) | Formatting: use markdown lists in 'one of' lists (#726) | [Benjie Gillam]
| [d24252b](https://github.com/graphql/graphql-spec/commit/d24252b82637a1deab3c3b8b07ce6bb3b3fda331) | Add definition of selectionSet to CreateSourceEventStream (#828) | [Benjie Gillam]
| [83b638f](https://github.com/graphql/graphql-spec/commit/83b638f0ec93cebf4d85997e3b49479a56aa051a) | Renumber list items (#824) | [Benjie Gillam]
| [d248f55](https://github.com/graphql/graphql-spec/commit/d248f551bd84042cfba1b48b95848f06dee36ea8) | Editorial: Clean trailing whitespace (#813) | [Lee Byron]
| [eb86ed9](https://github.com/graphql/graphql-spec/commit/eb86ed92bd89b6d58f25d39e6c6e8cb82620b8c9) | Disallow non-breakable chains of circular references in Input Objects (#701) | [Benedikt Franke], [Lee Byron]
| [c83baf6](https://github.com/graphql/graphql-spec/commit/c83baf6efb3abb0faeb55d118324704f3ce2c1f2) | Editorial: s/server/service (#811) | [Lee Byron]
| [d3ea511](https://github.com/graphql/graphql-spec/commit/d3ea51147c2c00c4da21cf0b624bf055dd45a02d) | Add missing VARIABLE DEFINITION (#761) | [dugenkui]
| [39caf61](https://github.com/graphql/graphql-spec/commit/39caf613baab565d42385a7ba2e643eaaa275e9b) | Clarify that __typename may be queried on unions (#756) | [David Glasser]
| [657bc69](https://github.com/graphql/graphql-spec/commit/657bc69cc834a66350ebaaa68263d6fad716f1b5) | Minor grammar fixes (#728) | [Loren Sands-Ramshaw]
| [41dc593](https://github.com/graphql/graphql-spec/commit/41dc593687768af447b70ee82bd7d92f5f7d7e82) | Add explicit example to allow nesting lists within lists (#755) | [Benedikt Franke]
| [73fc593](https://github.com/graphql/graphql-spec/commit/73fc59385f5bcbc6bec4c011eedf18aff1e127b7) | Make 'Field Selections' header stable (#773) | [Benjie Gillam]
| [abed779](https://github.com/graphql/graphql-spec/commit/abed7797820657f0ecc75e04418115ff5bd33561) | Small grammar fix (#762) | [Martin Bonnin]
| [2e5aa78](https://github.com/graphql/graphql-spec/commit/2e5aa786d30fb13f2992d71d154b4320cbcec114) | fix typo (#747) | [Andreas Marek]
| [2d67b69](https://github.com/graphql/graphql-spec/commit/2d67b696256c90e0772823f73ae9e8fd60aa4e7c) | Fix typo in Input Objects (#741) | [dugenkui]
| [05d4a3b](https://github.com/graphql/graphql-spec/commit/05d4a3be4ed8bad274209c6f855d895f824949c2) | fix __Type explanation (#737) | [dugenkui]
| [c976d31](https://github.com/graphql/graphql-spec/commit/c976d31311bdcc0590c930a3da45b9b78201fb68) | Fix typo in CollectFields (#732) | [Benjie Gillam]
| [6c9e13b](https://github.com/graphql/graphql-spec/commit/6c9e13b2281b6e30a630d334fea90d389e9099cb) | [Variables] Generalize description | [Michael Joseph Rosenthal]
| [e5c31c9](https://github.com/graphql/graphql-spec/commit/e5c31c9d8c4439d06c858ec22fde7158509283bb) | Fix 'Format' / 'Formal' typo; fix another header | [Benjie Gillam]
| [0b3c0fa](https://github.com/graphql/graphql-spec/commit/0b3c0fa7b57dd6087cc7b2b31fe7af8be54d3b23) | Section headers for formal specifications | [Benjie Gillam]
| [a0de4c5](https://github.com/graphql/graphql-spec/commit/a0de4c5cc158083b98f5002d6016118c28f42557) | [Editorial] Fix example for Variable Uniqueness (#703) | [Pascal Senn]
| [2bd2d01](https://github.com/graphql/graphql-spec/commit/2bd2d0197ee2780aa7f5895a1d06f8d64f9e83dd) | [grammar] add "the " to Type Condition section (#712) | [Michael Joseph Rosenthal]
| [ef4bbca](https://github.com/graphql/graphql-spec/commit/ef4bbca9a49999947b6d3b319addc422d47fc18e) | [Editorial]: Use non-null for if variable in @skip and @include (#722) | [Loren Sands-Ramshaw]
| [02fd71f](https://github.com/graphql/graphql-spec/commit/02fd71f816139b7e040f969860778d23df288d32) | Add description to Schema (#466) | [Ivan Goncharov]
| [3b001da](https://github.com/graphql/graphql-spec/commit/3b001da015a4b55331b534327a9ad74eb2e6d744) | Grammar: Add missing ImplementsInterface to Interface rules | [Ivan Maximov]
| [8ac091a](https://github.com/graphql/graphql-spec/commit/8ac091a4e0232def801dd8dfc6da208c3e7e347f) | Update draft URL | [Lee Byron]
| [e297e92](https://github.com/graphql/graphql-spec/commit/e297e9254bc7649ee0063164d7f3395425159e26) | RFC: Allow interfaces to implement other interfaces (#373) | [Mike Marcacci], [Lee Byron]
| [97db7cd](https://github.com/graphql/graphql-spec/commit/97db7cd2bdff15c4d195141065fa2bfb41816f60) | [editorial] Fix formatting of algorithms in Validation section (#671) | [Lee Byron]
| [4d55742](https://github.com/graphql/graphql-spec/commit/4d55742ff4f9d18a722d31846bea3ea63cf7971f) | [editorial] Localized note about order of skip and include directives (#669) | [Lee Byron]
| [be33a64](https://github.com/graphql/graphql-spec/commit/be33a640a8d12eb177f7cce0c61a1fad770a3430) | [RFC] Repeatable directives (#472) | [Oleg Ilyenko], [Lee Byron], [Benedikt Franke]
| [39f7a34](https://github.com/graphql/graphql-spec/commit/39f7a34e7af99de345ca344f9e6645ed8a6bc3f7) | Improve clarity of scalar types (#597) | [Lee Byron]
| [8ada467](https://github.com/graphql/graphql-spec/commit/8ada467ed5bc3b72ccd42508f5bb56a7ef3b1328) | Fixed that `CoerceArgumentValues` refers to `variableType` (#659) | [Marc Knaup]
| [09cdaec](https://github.com/graphql/graphql-spec/commit/09cdaecf3a4d08c9c53092ce83b0dede40adcba6) | RFC: Number value literal lookahead restrictions (#601) | [Lee Byron]
| [a73cd6f](https://github.com/graphql/graphql-spec/commit/a73cd6fef02d16483cb5f9c1f7f6d81db3c46584) | Clarify lexing is greedy with lookahead restrictions. (#599) | [Lee Byron]
| [e491220](https://github.com/graphql/graphql-spec/commit/e49122000599567bea17ad6eecf50ad17fa60693) | Added missing parameter in call to CollectFields() (#658) | [Marc Knaup]
| [bce8020](https://github.com/graphql/graphql-spec/commit/bce80208054cb1aaf7b9f9f513270ba413054da3) | [RFC] Add note recommending directive names to be namespaced (#657) | [Antoine Boyer], [Lee Byron]
| [43a5826](https://github.com/graphql/graphql-spec/commit/43a58263fe69e6c016964848a8cf6eab3c69faaf) | Update copyright and URLs from Facebook to GraphQL Foundation (#637) | [Lee Byron]
| [bb95aa5](https://github.com/graphql/graphql-spec/commit/bb95aa5c4023cc3cf04fb3450f894300db77d846) | fixed #613 (#614) | [Ivan Maximov]
| [b967f46](https://github.com/graphql/graphql-spec/commit/b967f46a6d34c8ea45b85ff6b7c97cbcfdcf02c9) | remove redundant step from SameResponseShape (#602) | [Chris Brown]
| [7ca239c](https://github.com/graphql/graphql-spec/commit/7ca239c01eb7cb950d36eebc3f341fa9a9deeb4b) | "Directive order is significant" section (#470) | [Oleg Ilyenko]
| [7237443](https://github.com/graphql/graphql-spec/commit/72374434452fc05ace73d5c5d4287e047e939ae0) | Editorial: Fix reference to StringValue from Ignored (#608) | [Lee Byron]
| [5cc1424](https://github.com/graphql/graphql-spec/commit/5cc14241511d193d2ba2c7dc95b7808e088b0caa) | values of correct type grammar fix (#603) | [Chris Brown]
| [663a18b](https://github.com/graphql/graphql-spec/commit/663a18bbcda0a8686a7bb51b1699aa997a488cb1) | Grammar fix in section 3 (#605) | [ab-pm]
| [6de9b65](https://github.com/graphql/graphql-spec/commit/6de9b654e8cd559978f8cdf5dee64a06cb054e28) | fix ExecuteField usage arguments (#607) | [Chris Brown]
| [dfd7571](https://github.com/graphql/graphql-spec/commit/dfd75718042806733e779930fbcebd6b02bb2106) | Editorial: Make grammar recursion more clear (#593) | [Lee Byron]
| [a3087c2](https://github.com/graphql/graphql-spec/commit/a3087c262022adf6a767d1cf644bc5594eb4eb14) | Remove contradictory language (#535) | [Grant Wu]
| [6b19129](https://github.com/graphql/graphql-spec/commit/6b19129b984faccf97b31356c3ebdc0d16c8e6d4) | Synchronise grammar rules across the spec (#538) | [Ivan Goncharov]
| [c7bface](https://github.com/graphql/graphql-spec/commit/c7bface58bf6f58cc809f279cba1b6245de914b4) | Update spec/Section 4 -- Introspection.md (#524) | [Görcsös Zoltán]
| [69f2bd4](https://github.com/graphql/graphql-spec/commit/69f2bd4740191b1917296d55fe08e07922123c57) | Fix grammar in Section 3 -- Type System.md (#546) | [Edwin Shin]
| [a9b186a](https://github.com/graphql/graphql-spec/commit/a9b186adf178fde9eaba0db2c2abfe05bd3caad5) | Introspection: Clarify nullability of fields in `__Type` type (#536) | [Ivan Goncharov]
| [f18c922](https://github.com/graphql/graphql-spec/commit/f18c92232e7c7a17402abc8e427ab77ec7901eaa) | Fix ambiguity about optional variables in non-null args (#520) | [Lee Byron]
| [51337d6](https://github.com/graphql/graphql-spec/commit/51337d6d038b094bc9598e15674d91b80ceca315) | Add missing '&' to Punctuator production (graphql#573) (#574) | [Sebastian Redl]
| [b79c7f7](https://github.com/graphql/graphql-spec/commit/b79c7f760b039f5c924da2a40107b70ee08fc1dd) | Added missing 'a' to section on 'Descriptions' (#527) | [James Scott]
| [7133b59](https://github.com/graphql/graphql-spec/commit/7133b599a42507734ea0e983e2aa59003951a583) | Add example of negative integer coercion to ID (#480) | [Ivan Goncharov]
| [760753e](https://github.com/graphql/graphql-spec/commit/760753edc3588923b8ee8e9885dadc22e478b911) | [RFC] Allow directives on variable definitions (#510) | [Matt Mahoney]
| [1278654](https://github.com/graphql/graphql-spec/commit/1278654d9bef295c56c08c1c0f63d4056b611e89) | Editorial changes on the spec document. (#493) | [Arun Ponniah Sethuramalingam]
| [b83ca98](https://github.com/graphql/graphql-spec/commit/b83ca9802f2d23a23d4cf2385d05b12cfd83896c) | Change scheme for URLs from http to https where https is available. (#504) | [Jamie Schouten]
| [4efbbc0](https://github.com/graphql/graphql-spec/commit/4efbbc0a091448ab2914dc6e255e0f4d5026f5dd) | Correct description of directive example (#506) | [António Nuno Monteiro]
| [1884f41](https://github.com/graphql/graphql-spec/commit/1884f4140c012063c1367edce466d5d7dbf187c1) | Fix grammar (#514) | [Ivan Goncharov]
| [4a70722](https://github.com/graphql/graphql-spec/commit/4a707222431fb68bbf649aaf7350c4dcca7c9c4d) | Revert "[RFC] Allow directives on variable definitions" (#492) | [Lee Byron]
| [b7b6a0b](https://github.com/graphql/graphql-spec/commit/b7b6a0b3c898b20395d58dd6414465a208fca440) | Move to after DefaultValue, and make [Const] | [Matt Mahoney]
| [98ffe21](https://github.com/graphql/graphql-spec/commit/98ffe217ff4fef3a6d20159ac3d96820915fa955) | [RFC] Allow directives on variable definitions | [Matt Mahoney]
| [1ee42ce](https://github.com/graphql/graphql-spec/commit/1ee42ceb453c124173615f98d0c90506f2345f9d) | Change values of x, y in example 121 (#468) | [Matthias Prechtl]
| [8ee3cc7](https://github.com/graphql/graphql-spec/commit/8ee3cc72c3141568f612dac3b66b3881f023b5ef) | Update Section 6 -- Execution.md (#469) | [Tejas Kumar]
| [b095b18](https://github.com/graphql/graphql-spec/commit/b095b18ff8c2f0f11b9fd33a8fdf2d95241fff28) | Start next working draft for future release | Travis CI
Generated with:
```sh
git log June2018..HEAD --format="| [%h](https://github.com/graphql/graphql-spec/commit/%H) | %s | %an <%ae> %(trailers:key=Co-authored-by,valueonly,separator=%x20)" -- spec
```
## Diff
### spec/GraphQL.md
spec/GraphQL.md
~~~diff
@@ -1,51 +1,72 @@
-GraphQL
--------
+# GraphQL
-*June 2018 Edition*
+*October 2021 Edition*
**Introduction**
This is the specification for GraphQL, a query language and execution engine
originally created at Facebook in 2012 for describing the capabilities and
requirements of data models for client-server applications. The development of
-this open standard started in 2015.
+this open standard started in 2015. This specification was licensed under OWFa
+1.0 in 2017. The [GraphQL Foundation](https://graphql.org/foundation/) was
+formed in 2019 as a neutral focal point for organizations who support the
+GraphQL ecosystem, and the
+[GraphQL Specification Project](https://graphql.org/community/) was established
+also in 2019 as the Joint Development Foundation Projects, LLC, GraphQL Series.
+
+If your organization benefits from GraphQL, please consider
+[becoming a member](https://graphql.org/foundation/join/#graphql-foundation)
+and helping us to sustain the activities that support the health of our neutral
+ecosystem.
+
+The GraphQL Specification Project has evolved and may continue to evolve in
+future editions of this specification. Previous editions of the GraphQL
+specification can be found at permalinks that match their
+[release tag](https://github.com/graphql/graphql-spec/releases). The latest
+working draft release can be found at
+[https://spec.graphql.org/draft](https://spec.graphql.org/draft).
-GraphQL has evolved and may continue to evolve in future editions of this
-specification. Previous editions of the GraphQL specification can be found at
-permalinks that match their [release tag](https://github.com/facebook/graphql/releases).
-The latest working draft release can be found at [facebook.github.io/graphql/draft/](http://facebook.github.io/graphql/draft/).
**Copyright notice**
-Copyright © 2015-present, Facebook, Inc.
+Copyright © 2015-2018, Facebook, Inc.
-As of September 26, 2017, the following persons or entities have made this
-Specification available under the Open Web Foundation Final Specification
-Agreement (OWFa 1.0), which is available at [openwebfoundation.org](http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0).
+Copyright © 2019-present, GraphQL contributors
-* Facebook, Inc.
-
-You can review the signed copies of the Open Web Foundation Final Specification
-Agreement Version 1.0 for this specification at [github.com/facebook/graphql](https://github.com/facebook/graphql/tree/master/signed-agreements),
-which may also include additional parties to those listed above.
-
-Your use of this Specification may be subject to other third party rights.
-THIS SPECIFICATION IS PROVIDED “AS IS.” The contributors expressly disclaim any
+THESE MATERIALS ARE PROVIDED “AS IS.” The parties expressly disclaim any
warranties (express, implied, or otherwise), including implied warranties of
merchantability, non-infringement, fitness for a particular purpose, or title,
-related to the Specification. The entire risk as to implementing or otherwise
-using the Specification is assumed by the Specification implementer and user.
-IN NO EVENT WILL ANY PARTY BE LIABLE TO ANY OTHER PARTY FOR LOST PROFITS OR ANY
-FORM OF INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER
-FROM ANY CAUSES OF ACTION OF ANY KIND WITH RESPECT TO THIS SPECIFICATION OR ITS
-GOVERNING AGREEMENT, WHETHER BASED ON BREACH OF CONTRACT, TORT (INCLUDING
-NEGLIGENCE), OR OTHERWISE, AND WHETHER OR NOT THE OTHER PARTY HAS BEEN ADVISED
-OF THE POSSIBILITY OF SUCH DAMAGE.
+related to the materials. The entire risk as to implementing or otherwise using
+the materials is assumed by the implementer and user. IN NO EVENT WILL THE
+PARTIES BE LIABLE TO ANY OTHER PARTY FOR LOST PROFITS OR ANY FORM OF INDIRECT,
+SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER FROM ANY CAUSES
+OF ACTION OF ANY KIND WITH RESPECT TO THIS DELIVERABLE OR ITS GOVERNING
+AGREEMENT, WHETHER BASED ON BREACH OF CONTRACT, TORT (INCLUDING NEGLIGENCE), OR
+OTHERWISE, AND WHETHER OR NOT THE OTHER MEMBER HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
+**Licensing**
+
+The GraphQL Specification Project is made available by the
+[Joint Development Foundation](https://www.jointdevelopment.org/). The current
+[Working Group](https://github.com/graphql/graphql-wg) charter, which includes
+the IP policy governing all working group deliverables (including specifications,
+source code, and datasets) may be found at
+[https://technical-charter.graphql.org](https://technical-charter.graphql.org).
+
+Currently, the licenses governing GraphQL Specification Project deliverables are:
+
+| Deliverable | License |
+| -------------- | ------------------------------------------------------- |
+| Specifications | [Open Web Foundation Agreement 1.0 Mode](http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0) (Patent and Copyright)
+| Source code | [MIT License](https://opensource.org/licenses/MIT)
+| Data sets | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/)
**Conformance**
A conforming implementation of GraphQL must fulfill all normative requirements.
Conformance requirements are described in this document via both
descriptive assertions and key words with clearly defined meanings.
~~~
spec/Section 1 -- Overview.md
~~~diff
@@ -21,65 +21,65 @@ Which produces the resulting data (in JSON):
{
"user": {
"name": "Mark Zuckerberg"
}
}
```
GraphQL is not a programming language capable of arbitrary computation, but is
-instead a language used to query application servers that have
+instead a language used to make requests to application services that have
capabilities defined in this specification. GraphQL does not mandate a
-particular programming language or storage system for application servers that
-implement it. Instead, application servers take their capabilities and map them
+particular programming language or storage system for application services that
+implement it. Instead, application services take their capabilities and map them
to a uniform language, type system, and philosophy that GraphQL encodes.
This provides a unified interface friendly to product development and a powerful
platform for tool-building.
GraphQL has a number of design principles:
- * **Hierarchical**: Most product development today involves the creation and
- manipulation of view hierarchies. To achieve congruence with the structure
- of these applications, a GraphQL query itself is structured hierarchically.
- The query is shaped just like the data it returns. It is a natural
- way for clients to describe data requirements.
-
* **Product-centric**: GraphQL is unapologetically driven by the requirements
of views and the front-end engineers that write them. GraphQL starts with
their way of thinking and requirements and builds the language and runtime
necessary to enable that.
- * **Strong-typing**: Every GraphQL server defines an application-specific
- type system. Queries are executed within the context of that type system.
- Given a query, tools can ensure that the query is both syntactically
- correct and valid within the GraphQL type system before execution, i.e. at
- development time, and the server can make certain guarantees about the shape
+ * **Hierarchical**: Most product development today involves the creation and
+ manipulation of view hierarchies. To achieve congruence with the structure
+ of these applications, a GraphQL request itself is structured hierarchically.
+ The request is shaped just like the data in its response. It is a natural way
+ for clients to describe data requirements.
+
+ * **Strong-typing**: Every GraphQL service defines an application-specific
+ type system. Requests are executed within the context of that type system.
+ Given a GraphQL operation, tools can ensure that it is both syntactically
+ correct and valid within that type system before execution, i.e. at
+ development time, and the service can make certain guarantees about the shape
and nature of the response.
- * **Client-specified queries**: Through its type system, a GraphQL server
+ * **Client-specified response**: Through its type system, a GraphQL service
publishes the capabilities that its clients are allowed to consume. It is
the client that is responsible for specifying exactly how it will consume
- those published capabilities. These queries are specified at field-level
- granularity. In the majority of client-server applications written
- without GraphQL, the server determines the data returned in its various
- scripted endpoints. A GraphQL query, on the other hand, returns exactly what
- a client asks for and no more.
+ those published capabilities. These requests are specified at field-level
+ granularity. In the majority of client-server applications written without
+ GraphQL, the service determines the shape of data returned from its various
+ endpoints. A GraphQL response, on the other hand, contains exactly what a
+ client asks for and no more.
- * **Introspective**: GraphQL is introspective. A GraphQL server's type system
- must be queryable by the GraphQL language itself, as will be described in this
+ * **Introspective**: GraphQL is introspective. A GraphQL service's type system
+ can be queryable by the GraphQL language itself, as will be described in this
specification. GraphQL introspection serves as a powerful platform for
building common tools and client software libraries.
Because of these principles, GraphQL is a powerful and productive environment
for building client applications. Product developers and designers building
-applications against working GraphQL servers -- supported with quality tools --
-can quickly become productive without reading extensive documentation and with
+applications against working GraphQL services—supported with quality tools—can
+quickly become productive without reading extensive documentation and with
little or no formal training. To enable that experience, there must be those
-that build those servers and tools.
+that build those services and tools.
The following formal specification serves as a reference for those builders.
It describes the language and its grammar, the type system and the
introspection system used to query it, and the execution and validation engines
with the algorithms to power them. The goal of this specification is to provide
a foundation and framework for an ecosystem of GraphQL tools, client libraries,
-and server implementations -- spanning both organizations and platforms -- that
+and service implementations—spanning both organizations and platforms—that
has yet to be built. We look forward to working with the community
in order to do that.
~~~
spec/Section 2 -- Language.md
~~~diff
@@ -1,43 +1,78 @@
# Language
Clients use the GraphQL query language to make requests to a GraphQL service.
We refer to these request sources as documents. A document may contain
operations (queries, mutations, and subscriptions) as well as fragments, a
-common unit of composition allowing for query reuse.
+common unit of composition allowing for data requirement reuse.
A GraphQL document is defined as a syntactic grammar where terminal symbols are
tokens (indivisible lexical units). These tokens are defined in a lexical
-grammar which matches patterns of source characters (defined by a
-double-colon `::`).
+grammar which matches patterns of source characters. In this document, syntactic
+grammar productions are distinguished with a colon `:` while lexical grammar
+productions are distinguished with a double-colon `::`.
-Note: See [Appendix A](#sec-Appendix-Notation-Conventions) for more details about the definition of lexical and syntactic grammar and other notational conventions
-used in this document.
+The source text of a GraphQL document must be a sequence of {SourceCharacter}.
+The character sequence must be described by a sequence of {Token} and {Ignored}
+lexical grammars. The lexical token sequence, omitting {Ignored}, must be
+described by a single {Document} syntactic grammar.
+
+Note: See [Appendix A](#sec-Appendix-Notation-Conventions) for more information
+about the lexical and syntactic grammar and other notational conventions used
+throughout this document.
+
+**Lexical Analysis & Syntactic Parse**
+
+The source text of a GraphQL document is first converted into a sequence of
+lexical tokens, {Token}, and ignored tokens, {Ignored}. The source text is
+scanned from left to right, repeatedly taking the next possible sequence of
+code-points allowed by the lexical grammar productions as the next token. This
+sequence of lexical tokens are then scanned from left to right to produce an
+abstract syntax tree (AST) according to the {Document} syntactical grammar.
+
+Lexical grammar productions in this document use *lookahead restrictions* to
+remove ambiguity and ensure a single valid lexical analysis. A lexical token is
+only valid if not followed by a character in its lookahead restriction.
+
+For example, an {IntValue} has the restriction {[lookahead != Digit]}, so cannot
+be followed by a {Digit}. Because of this, the sequence {`123`} cannot represent
+the tokens ({`12`}, {`3`}) since {`12`} is followed by the {Digit} {`3`} and
+so must only represent a single token. Use {WhiteSpace} or other {Ignored}
+between characters to represent multiple tokens.
+
+Note: This typically has the same behavior as a
+"[maximal munch](https://en.wikipedia.org/wiki/Maximal_munch)" longest possible
+match, however some lookahead restrictions include additional constraints.
## Source Text
-SourceCharacter :: /[\u0009\u000A\u000D\u0020-\uFFFF]/
+SourceCharacter ::
+ - "U+0009"
+ - "U+000A"
+ - "U+000D"
+ - "U+0020–U+FFFF"
GraphQL documents are expressed as a sequence of
-[Unicode](http://unicode.org/standard/standard.html) characters. However, with
+[Unicode](https://unicode.org/standard/standard.html) code points (informally
+referred to as *"characters"* through most of this specification). However, with
few exceptions, most of GraphQL is expressed only in the original non-control
ASCII range so as to be as widely compatible with as many existing tools,
languages, and serialization formats as possible and avoid display issues in
text editors and source control.
+Note: Non-ASCII Unicode characters may appear freely within {StringValue} and
+{Comment} portions of GraphQL.
+
### Unicode
UnicodeBOM :: "Byte Order Mark (U+FEFF)"
-Non-ASCII Unicode characters may freely appear within {StringValue} and
-{Comment} portions of GraphQL.
-
The "Byte Order Mark" is a special Unicode character which
may appear at the beginning of a file containing Unicode which programs may use
to determine the fact that the text stream is Unicode, what endianness the text
stream is in, and which of several Unicode encodings to interpret.
### White Space
@@ -55,158 +90,195 @@ Note: GraphQL intentionally does not consider Unicode "Zs" category characters
as white-space, avoiding misinterpretation by text editors and source
control tools.
### Line Terminators
LineTerminator ::
- "New Line (U+000A)"
- - "Carriage Return (U+000D)" [ lookahead ! "New Line (U+000A)" ]
+ - "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
- "Carriage Return (U+000D)" "New Line (U+000A)"
Like white space, line terminators are used to improve the legibility of source
-text, any amount may appear before or after any other token and have no
-significance to the semantic meaning of a GraphQL Document. Line
-terminators are not found within any other token.
+text and separate lexical tokens, any amount may appear before or after any
+other token and have no significance to the semantic meaning of a GraphQL
+Document. Line terminators are not found within any other token.
-Note: Any error reporting which provide the line number in the source of the
+Note: Any error reporting which provides the line number in the source of the
offending syntax should use the preceding amount of {LineTerminator} to produce
the line number.
### Comments
-Comment :: `#` CommentChar*
+Comment :: `#` CommentChar* [lookahead != CommentChar]
CommentChar :: SourceCharacter but not LineTerminator
GraphQL source documents may contain single-line comments, starting with the
{`#`} marker.
-A comment can contain any Unicode code point except {LineTerminator} so a
-comment always consists of all code points starting with the {`#`} character up
-to but not including the line terminator.
+A comment can contain any Unicode code point in {SourceCharacter} except
+{LineTerminator} so a comment always consists of all code points starting with
+the {`#`} character up to but not including the {LineTerminator} (or end of
+the source).
-Comments behave like white space and may appear after any token, or before a
-line terminator, and have no significance to the semantic meaning of a
+Comments are {Ignored} like white space and may appear after any token, or
+before a {LineTerminator}, and have no significance to the semantic meaning of a
GraphQL Document.
### Insignificant Commas
Comma :: ,
Similar to white space and line terminators, commas ({`,`}) are used to improve
the legibility of source text and separate lexical tokens but are otherwise
syntactically and semantically insignificant within GraphQL Documents.
Non-significant comma characters ensure that the absence or presence of a comma
does not meaningfully alter the interpreted syntax of the document, as this can
be a common user-error in other languages. It also allows for the stylistic use
-of either trailing commas or line-terminators as list delimiters which are both
+of either trailing commas or line terminators as list delimiters which are both
often desired for legibility and maintainability of source code.
### Lexical Tokens
Token ::
- Punctuator
- Name
- IntValue
- FloatValue
- StringValue
A GraphQL document is comprised of several kinds of indivisible lexical tokens
defined here in a lexical grammar by patterns of source Unicode characters.
+Lexical tokens may be separated by {Ignored} tokens.
-Tokens are later used as terminal symbols in a GraphQL Document
-syntactic grammars.
+Tokens are later used as terminal symbols in GraphQL syntactic grammar rules.
### Ignored Tokens
Ignored ::
- UnicodeBOM
- WhiteSpace
- LineTerminator
- Comment
- Comma
-Before and after every lexical token may be any amount of ignored tokens
-including {WhiteSpace} and {Comment}. No ignored regions of a source
-document are significant, however ignored source characters may appear within
-a lexical token in a significant way, for example a {String} may contain white
-space characters.
+{Ignored} tokens are used to improve readability and provide separation between
+lexical tokens, but are otherwise insignificant and not referenced in
+syntactical grammar productions.
-No characters are ignored while parsing a given token, as an example no
-white space characters are permitted between the characters defining a
-{FloatValue}.
+Any amount of {Ignored} may appear before and after every lexical token. No
+ignored regions of a source document are significant, however {SourceCharacter}
+which appear in {Ignored} may also appear within a lexical {Token} in a
+significant way, for example a {StringValue} may contain white space characters.
+No {Ignored} may appear *within* a {Token}, for example no white space
+characters are permitted between the characters defining a {FloatValue}.
### Punctuators
-Punctuator :: one of ! $ ( ) ... : = @ [ ] { | }
+Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
GraphQL documents include punctuation in order to describe structure. GraphQL
is a data description language and not a programming language, therefore GraphQL
lacks the punctuation often used to describe mathematical expressions.
### Names
-Name :: /[_A-Za-z][_0-9A-Za-z]*/
+Name ::
+ - NameStart NameContinue* [lookahead != NameContinue]
+
+NameStart ::
+ - Letter
+ - `_`
+
+NameContinue ::
+ - Letter
+ - Digit
+ - `_`
+
+Letter :: one of
+ - `A` `B` `C` `D` `E` `F` `G` `H` `I` `J` `K` `L` `M`
+ - `N` `O` `P` `Q` `R` `S` `T` `U` `V` `W` `X` `Y` `Z`
+ - `a` `b` `c` `d` `e` `f` `g` `h` `i` `j` `k` `l` `m`
+ - `n` `o` `p` `q` `r` `s` `t` `u` `v` `w` `x` `y` `z`
+
+Digit :: one of
+ - `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
GraphQL Documents are full of named things: operations, fields, arguments,
types, directives, fragments, and variables. All names must follow the same
grammatical form.
Names in GraphQL are case-sensitive. That is to say `name`, `Name`, and `NAME`
all refer to different names. Underscores are significant, which means
`other_name` and `othername` are two different names.
-Names in GraphQL are limited to this ASCII subset of possible
-characters to support interoperation with as many other systems as possible.
+A {Name} must not be followed by a {NameContinue}. In other words, a {Name}
+token is always the longest possible valid sequence. The source characters
+{`a1`} cannot be interpreted as two tokens since {`a`} is followed by the {NameContinue} {`1`}.
+
+Note: Names in GraphQL are limited to the Latin ASCII subset
+of {SourceCharacter} in order to support interoperation with as many other
+systems as possible.
+
+**Reserved Names**
+
+Any {Name} within a GraphQL type system must not start with two underscores
+{"__"} unless it is part of the [introspection system](#sec-Introspection) as
+defined by this specification.
## Document
Document : Definition+
Definition :
- ExecutableDefinition
- - TypeSystemDefinition
- - TypeSystemExtension
+ - TypeSystemDefinitionOrExtension
+
+ExecutableDocument : ExecutableDefinition+
ExecutableDefinition :
- OperationDefinition
- FragmentDefinition
A GraphQL Document describes a complete file or request string operated on
by a GraphQL service or client. A document contains multiple definitions, either
executable or representative of a GraphQL type system.
-Documents are only executable by a GraphQL service if they contain an
-{OperationDefinition} and otherwise only contain {ExecutableDefinition}.
-However documents which do not contain {OperationDefinition} or do contain
-{TypeSystemDefinition} or {TypeSystemExtension} may still be parsed
-and validated to allow client tools to represent many GraphQL uses which may
-appear across many individual files.
+Documents are only executable by a GraphQL service if they are
+{ExecutableDocument} and contain at least one {OperationDefinition}. A
+Document which contains {TypeSystemDefinitionOrExtension} must not be executed;
+GraphQL execution services which receive a Document containing these should
+return a descriptive error.
-If a Document contains only one operation, that operation may be unnamed or
-represented in the shorthand form, which omits both the query keyword and
-operation name. Otherwise, if a GraphQL Document contains multiple
+GraphQL services which only seek to execute GraphQL requests and not construct
+a new GraphQL schema may choose to only permit {ExecutableDocument}.
+
+Documents which do not contain {OperationDefinition} or do contain
+{TypeSystemDefinitionOrExtension} may still be parsed and validated to allow
+client tools to represent many GraphQL uses which may appear across many
+individual files.
+
+If a Document contains only one operation, that operation may be unnamed. If
+that operation is a query without variables or directives then it may also be
+represented in the shorthand form, omitting both the {`query`} keyword as well
+as the operation name. Otherwise, if a GraphQL Document contains multiple
operations, each operation must be named. When submitting a Document with
multiple operations to a GraphQL service, the name of the desired operation to
be executed must also be provided.
-GraphQL services which only seek to provide GraphQL query execution may choose
-to only include {ExecutableDefinition} and omit the {TypeSystemDefinition} and
-{TypeSystemExtension} rules from {Definition}.
-
## Operations
OperationDefinition :
- OperationType Name? VariableDefinitions? Directives? SelectionSet
- SelectionSet
OperationType : one of `query` `mutation` `subscription`
@@ -230,19 +302,20 @@ mutation {
likeCount
}
}
}
```
**Query shorthand**
-If a document contains only one query operation, and that query defines no
-variables and contains no directives, that operation may be represented in a
-short-hand form which omits the query keyword and query name.
+If a document contains only one operation and that operation is a query which
+defines no variables and contains no directives then that operation may be
+represented in a short-hand form which omits the {`query`} keyword and operation
+name.
For example, this unnamed query operation is written via query shorthand.
```graphql example
{
field
}
```
@@ -266,18 +339,18 @@ under-fetching data.
```graphql example
{
id
firstName
lastName
}
```
-In this query, the `id`, `firstName`, and `lastName` fields form a selection
-set. Selection sets may also contain fragment references.
+In this query operation, the `id`, `firstName`, and `lastName` fields form a
+selection set. Selection sets may also contain fragment references.
## Fields
Field : Alias? Name Arguments? Directives? SelectionSet?
A selection set is primarily composed of fields. A field describes one discrete
piece of information available to request within a selection set.
@@ -335,17 +408,17 @@ unique identifier.
## Arguments
Arguments[Const] : ( Argument[?Const]+ )
Argument[Const] : Name : Value[?Const]
Fields are conceptually functions which return values, and occasionally accept
arguments which alter their behavior. These arguments often map directly to
-function arguments within a GraphQL server's implementation.
+function arguments within a GraphQL service's implementation.
In this example, we want to query a specific user (requested via the `id`
argument) and their profile picture of a specific `size`:
```graphql example
{
user(id: 4) {
id
@@ -367,17 +440,17 @@ Many arguments can exist for a given field:
}
```
**Arguments are unordered**
Arguments may be provided in any syntactic order and maintain identical
semantic meaning.
-These two queries are semantically identical:
+These two operations are semantically identical:
```graphql example
{
picture(width: 200, height: 100)
}
```
```graphql example
@@ -386,71 +459,68 @@ These two queries are semantically identical:
}
```
## Field Alias
Alias : Name :
-By default, the key in the response object will use the field name
-queried. However, you can define a different name by specifying an alias.
+By default a field's response key in the response object will use that field's
+name. However, you can define a different response key by specifying an alias.
In this example, we can fetch two profile pictures of different sizes and ensure
-the resulting object will not have duplicate keys:
+the resulting response object will not have duplicate keys:
```graphql example
{
user(id: 4) {
id
name
smallPic: profilePic(size: 64)
bigPic: profilePic(size: 1024)
}
}
```
-Which returns the result:
+which returns the result:
```json example
{
"user": {
"id": 4,
"name": "Mark Zuckerberg",
"smallPic": "https://cdn.site.io/pic-4-64.jpg",
"bigPic": "https://cdn.site.io/pic-4-1024.jpg"
}
}
```
-Since the top level of a query is a field, it also can be given an alias:
+The fields at the top level of an operation can also be given an alias:
```graphql example
{
zuck: user(id: 4) {
id
name
}
}
```
-Returns the result:
+which returns the result:
```json example
{
"zuck": {
"id": 4,
"name": "Mark Zuckerberg"
}
}
```
-A field's response key is its alias if an alias is provided, and it is
-otherwise the field's name.
-
## Fragments
FragmentSpread : ... FragmentName Directives?
FragmentDefinition : fragment FragmentName TypeCondition Directives? SelectionSet
FragmentName : Name but not `on`
@@ -478,17 +548,17 @@ query noFragments {
name
profilePic(size: 50)
}
}
}
```
The repeated fields could be extracted into a fragment and composed by
-a parent fragment or query.
+a parent fragment or operation.
```graphql example
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
}
mutualFriends(first: 10) {
@@ -499,18 +569,18 @@ query withFragments {
fragment friendFields on User {
id
name
profilePic(size: 50)
}
```
-Fragments are consumed by using the spread operator (`...`). All fields selected
-by the fragment will be added to the query field selection at the same level
+Fragments are consumed by using the spread operator (`...`). All fields
+selected by the fragment will be added to the field selection at the same level
as the fragment invocation. This happens through multiple levels of fragment
spreads.
For example:
```graphql example
query withNestedFragments {
user(id: 4) {
@@ -529,40 +599,40 @@ fragment friendFields on User {
...standardProfilePic
}
fragment standardProfilePic on User {
profilePic(size: 50)
}
```
-The queries `noFragments`, `withFragments`, and `withNestedFragments` all
+The operations `noFragments`, `withFragments`, and `withNestedFragments` all
produce the same response object.
### Type Conditions
TypeCondition : on NamedType
Fragments must specify the type they apply to. In this example, `friendFields`
can be used in the context of querying a `User`.
Fragments cannot be specified on any input value (scalar, enumeration, or input
object).
Fragments can be specified on object types, interfaces, and unions.
-Selections within fragments only return values when concrete type of the object
+Selections within fragments only return values when the concrete type of the object
it is operating on matches the type of the fragment.
-For example in this query on the Facebook data model:
+For example in this operation using the Facebook data model:
```graphql example
query FragmentTyping {
- profiles(handles: ["zuck", "cocacola"]) {
+ profiles(handles: ["zuck", "coca-cola"]) {
handle
...userFragment
...pageFragment
}
}
fragment userFragment on User {
friends {
@@ -582,21 +652,21 @@ The `profiles` root field returns a list where each element could be a `Page` or
present and `likers` will not. Conversely when the result is a `Page`, `likers`
will be present and `friends` will not.
```json example
{
"profiles": [
{
"handle": "zuck",
- "friends": { "count" : 1234 }
+ "friends": { "count": 1234 }
},
{
- "handle": "cocacola",
- "likers": { "count" : 90234512 }
+ "handle": "coca-cola",
+ "likers": { "count": 90234512 }
}
]
}
```
### Inline Fragments
@@ -604,17 +674,17 @@ InlineFragment : ... TypeCondition? Directives? SelectionSet
Fragments can be defined inline within a selection set. This is done to
conditionally include fields based on their runtime type. This feature of
standard fragment inclusion was demonstrated in the `query FragmentTyping`
example. We could accomplish the same thing using inline fragments.
```graphql example
query inlineFragmentTyping {
- profiles(handles: ["zuck", "cocacola"]) {
+ profiles(handles: ["zuck", "coca-cola"]) {
handle
... on User {
friends {
count
}
}
... on Page {
likers {
@@ -661,98 +731,131 @@ Field and directive arguments accept input values of various literal primitives;
input values can be scalars, enumeration values, lists, or input objects.
If not defined as constant (for example, in {DefaultValue}), input values can be
specified as a variable. List and inputs objects may also contain variables (unless defined to be constant).
### Int Value
-IntValue :: IntegerPart
+IntValue :: IntegerPart [lookahead != {Digit, `.`, NameStart}]
IntegerPart ::
- NegativeSign? 0
- NegativeSign? NonZeroDigit Digit*
NegativeSign :: -
-Digit :: one of 0 1 2 3 4 5 6 7 8 9
-
NonZeroDigit :: Digit but not `0`
-An Int number is specified without a decimal point or exponent (ex. `1`).
+An {IntValue} is specified without a decimal point or exponent but may be
+negative (ex. {-123}). It must not have any leading {0}.
+
+An {IntValue} must not be followed by a {Digit}. In other words, an {IntValue}
+token is always the longest possible valid sequence. The source characters
+{12} cannot be interpreted as two tokens since {1} is followed by the {Digit}
+{2}. This also means the source {00} is invalid since it can neither be
+interpreted as a single token nor two {0} tokens.
+
+An {IntValue} must not be followed by a {`.`} or {NameStart}. If either {`.`} or
+{ExponentIndicator} follows then the token must only be interpreted as a
+possible {FloatValue}. No other {NameStart} character can follow. For example
+the sequences `0x123` and `123L` have no valid lexical representations.
### Float Value
FloatValue ::
- - IntegerPart FractionalPart
- - IntegerPart ExponentPart
- - IntegerPart FractionalPart ExponentPart
+ - IntegerPart FractionalPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
+ - IntegerPart FractionalPart [lookahead != {Digit, `.`, NameStart}]
+ - IntegerPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
FractionalPart :: . Digit+
ExponentPart :: ExponentIndicator Sign? Digit+
ExponentIndicator :: one of `e` `E`
Sign :: one of + -
-A Float number includes either a decimal point (ex. `1.0`) or an exponent
-(ex. `1e50`) or both (ex. `6.0221413e23`).
+A {FloatValue} includes either a decimal point (ex. {1.0}) or an exponent
+(ex. {1e50}) or both (ex. {6.0221413e23}) and may be negative. Like {IntValue},
+it also must not have any leading {0}.
+
+A {FloatValue} must not be followed by a {Digit}. In other words, a {FloatValue}
+token is always the longest possible valid sequence. The source characters
+{1.23} cannot be interpreted as two tokens since {1.2} is followed by the
+{Digit} {3}.
+
+A {FloatValue} must not be followed by a {.}. For example, the sequence {1.23.4}
+cannot be interpreted as two tokens ({1.2}, {3.4}).
+
+A {FloatValue} must not be followed by a {NameStart}. For example the sequence
+`0x1.2p3` has no valid lexical representation.
+
+Note: The numeric literals {IntValue} and {FloatValue} both restrict being
+immediately followed by a letter (or other {NameStart}) to reduce confusion
+or unexpected behavior since GraphQL only supports decimal numbers.
### Boolean Value
BooleanValue : one of `true` `false`
The two keywords `true` and `false` represent the two boolean values.
### String Value
StringValue ::
- - `"` StringCharacter* `"`
+ - `""` [lookahead != `"`]
+ - `"` StringCharacter+ `"`
- `"""` BlockStringCharacter* `"""`
StringCharacter ::
- - SourceCharacter but not `"` or \ or LineTerminator
- - \u EscapedUnicode
- - \ EscapedCharacter
+ - SourceCharacter but not `"` or `\` or LineTerminator
+ - `\u` EscapedUnicode
+ - `\` EscapedCharacter
EscapedUnicode :: /[0-9A-Fa-f]{4}/
-EscapedCharacter :: one of `"` \ `/` b f n r t
+EscapedCharacter :: one of `"` `\` `/` `b` `f` `n` `r` `t`
BlockStringCharacter ::
- SourceCharacter but not `"""` or `\"""`
- `\"""`
-Strings are sequences of characters wrapped in double-quotes (`"`). (ex.
-`"Hello World"`). White space and other otherwise-ignored characters are
+Strings are sequences of characters wrapped in quotation marks (U+0022).
+(ex. {`"Hello World"`}). White space and other otherwise-ignored characters are
significant within a string value.
-Note: Unicode characters are allowed within String value literals, however
-{SourceCharacter} must not contain some ASCII control characters so escape
-sequences must be used to represent these characters.
+The empty string {`""`} must not be followed by another {`"`} otherwise it would
+be interpreted as the beginning of a block string. As an example, the source
+{`""""""`} can only be interpreted as a single empty block string and not three
+empty strings.
+
+Non-ASCII Unicode characters are allowed within single-quoted strings.
+Since {SourceCharacter} must not contain some ASCII control characters, escape
+sequences must be used to represent these characters. The {`\`}, {`"`}
+characters also must be escaped. All other escape sequences are optional.
**Block Strings**
Block strings are sequences of characters wrapped in triple-quotes (`"""`).
White space, line terminators, quote, and backslash characters may all be
used unescaped to enable verbatim text. Characters must all be valid
{SourceCharacter}.
Since block strings represent freeform text often used in indented
positions, the string value semantics of a block string excludes uniform
indentation and blank initial and trailing lines via {BlockStringValue()}.
For example, the following operation containing a block string:
-```graphql example
+```raw graphql example
mutation {
sendEmail(message: """
Hello,
World!
Yours,
GraphQL.
""")
@@ -785,44 +888,49 @@ which makes it a little harder to read."""
```
Note: If non-printable ASCII characters are needed in a string value, a standard
quoted string with appropriate escape sequences must be used instead of a
block string.
**Semantics**
-StringValue :: `"` StringCharacter* `"`
+StringValue :: `""`
- * Return the Unicode character sequence of all {StringCharacter}
- Unicode character values (which may be an empty sequence).
+ * Return an empty sequence.
-StringCharacter :: SourceCharacter but not `"` or \ or LineTerminator
+StringValue :: `"` StringCharacter+ `"`
- * Return the character value of {SourceCharacter}.
+ * Return the sequence of all {StringCharacter} code points.
+
+StringCharacter :: SourceCharacter but not `"` or `\` or LineTerminator
+
+ * Return the code point {SourceCharacter}.
-StringCharacter :: \u EscapedUnicode
+StringCharacter :: `\u` EscapedUnicode
- * Return the character whose code unit value in the Unicode Basic Multilingual
- Plane is the 16-bit hexadecimal value {EscapedUnicode}.
+ * Let {value} be the 16-bit hexadecimal value represented by the sequence of
+ hexadecimal digits within {EscapedUnicode}.
+ * Return the code point {value}.
-StringCharacter :: \ EscapedCharacter
+StringCharacter :: `\` EscapedCharacter
- * Return the character value of {EscapedCharacter} according to the table below.
+ * Return the code point represented by {EscapedCharacter} according to the
+ table below.
-| Escaped Character | Code Unit Value | Character Name |
-| ----------------- | --------------- | ---------------------------- |
-| `"` | U+0022 | double quote |
-| `\` | U+005C | reverse solidus (back slash) |
-| `/` | U+002F | solidus (forward slash) |
-| `b` | U+0008 | backspace |
-| `f` | U+000C | form feed |
-| `n` | U+000A | line feed (new line) |
-| `r` | U+000D | carriage return |
-| `t` | U+0009 | horizontal tab |
+| Escaped Character | Code Point | Character Name |
+| ----------------- | ---------- | ---------------------------- |
+| {`"`} | U+0022 | double quote |
+| {`\`} | U+005C | reverse solidus (back slash) |
+| {`/`} | U+002F | solidus (forward slash) |
+| {`b`} | U+0008 | backspace |
+| {`f`} | U+000C | form feed |
+| {`n`} | U+000A | line feed (new line) |
+| {`r`} | U+000D | carriage return |
+| {`t`} | U+0009 | horizontal tab |
StringValue :: `"""` BlockStringCharacter* `"""`
* Let {rawValue} be the Unicode character sequence of all
{BlockStringCharacter} Unicode character values (which may be an empty
sequence).
* Return the result of {BlockStringValue(rawValue)}.
@@ -879,24 +987,24 @@ For example, these two field calls are similar, but are not identical:
```graphql example
{
field(arg: null)
field
}
```
-The first has explictly provided {null} to the argument "arg", while the second
+The first has explicitly provided {null} to the argument "arg", while the second
has implicitly not provided a value to the argument "arg". These two forms may
be interpreted differently. For example, a mutation representing deleting a
field vs not altering a field, respectively. Neither form may be used for an
input expecting a Non-Null type.
Note: The same two methods of representing the lack of a value are possible via
-variables by either providing the a variable value as {null} and not providing
+variables by either providing the variable value as {null} or not providing
a variable value at all.
### Enum Value
EnumValue : Name but not `true`, `false` or `null`
Enum values are represented as unquoted names (ex. `MOBILE_WEB`). It is
@@ -945,17 +1053,17 @@ curly-braces `{ }`. The values of an object literal may be any input value
literal or variable (ex. `{ name: "Hello world", score: 1.0 }`). We refer to
literal representation of input objects as "object literals."
**Input object fields are unordered**
Input object fields may be provided in any syntactic order and maintain
identical semantic meaning.
-These two queries are semantically identical:
+These two operations are semantically identical:
```graphql example
{
nearestThing(location: { lon: 12.43, lat: -53.211 })
}
```
```graphql example
@@ -981,60 +1089,59 @@ ObjectValue : { ObjectField+ }
## Variables
Variable : $ Name
VariableDefinitions : ( VariableDefinition+ )
-VariableDefinition : Variable : Type DefaultValue?
+VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
DefaultValue : = Value[Const]
-A GraphQL query can be parameterized with variables, maximizing query reuse,
+A GraphQL operation can be parameterized with variables, maximizing reuse,
and avoiding costly string building in clients at runtime.
If not defined as constant (for example, in {DefaultValue}), a {Variable} can be
supplied for an input value.
Variables must be defined at the top of an operation and are in scope
-throughout the execution of that operation.
+throughout the execution of that operation. Values for those variables are
+provided to a GraphQL service as part of a request so they may be substituted
+in during execution.
In this example, we want to fetch a profile picture size based on the size
of a particular device:
```graphql example
query getZuckProfile($devicePicSize: Int) {
user(id: 4) {
id
name
profilePic(size: $devicePicSize)
}
}
```
-Values for those variables are provided to a GraphQL service along with a
-request so they may be substituted during execution. If providing JSON for the
-variables' values, we could run this query and request profilePic of
-size `60` width:
+If providing JSON for the variables' values, we could request a `profilePic` of
+size `60`:
```json example
{
"devicePicSize": 60
}
```
**Variable use within Fragments**
-Query variables can be used within fragments. Query variables have global scope
-with a given operation, so a variable used within a fragment must be declared
-in any top-level operation that transitively consumes that fragment. If
-a variable is referenced in a fragment and is included by an operation that does
-not define that variable, the operation cannot be executed.
+Variables can be used within fragments. Variables have global scope with a given operation, so a variable used within a fragment must be declared in any
+top-level operation that transitively consumes that fragment. If a variable is
+referenced in a fragment and is included by an operation that does not define
+that variable, that operation is invalid (see [All Variable Uses Defined](#sec-All-Variable-Uses-Defined)).
## Type References
Type :
- NamedType
- ListType
- NonNullType
@@ -1042,19 +1149,19 @@ Type :
NamedType : Name
ListType : [ Type ]
NonNullType :
- NamedType !
- ListType !
-GraphQL describes the types of data expected by query variables. Input types
-may be lists of another input type, or a non-null variant of any other
-input type.
+GraphQL describes the types of data expected by arguments and variables.
+Input types may be lists of another input type, or a non-null variant of any
+other input type.
**Semantics**
Type : Name
* Let {name} be the string value of {Name}
* Let {type} be the type defined in the Schema named {name}
* {type} must not be {null}
@@ -1084,13 +1191,36 @@ validation behavior in a GraphQL document.
In some cases, you need to provide options to alter GraphQL's execution
behavior in ways field arguments will not suffice, such as conditionally
including or skipping a field. Directives provide this by describing additional information to the executor.
Directives have a name along with a list of arguments which may accept values
of any input type.
-Directives can be used to describe additional information for types, fields, fragments
-and operations.
+Directives can be used to describe additional information for types, fields,
+fragments and operations.
As future versions of GraphQL adopt new configurable execution capabilities,
-they may be exposed via directives.
+they may be exposed via directives. GraphQL services and tools may also provide
+any additional *custom directive* beyond those described here.
+
+**Directive order is significant**
+
+Directives may be provided in a specific syntactic order which may have semantic interpretation.
+
+These two type definitions may have different semantic meaning:
+
+```graphql example
+type Person
+ @addExternalFields(source: "profiles")
+ @excludeField(name: "photo") {
+ name: String
+}
+```
+
+```graphql example
+type Person
+ @excludeField(name: "photo")
+ @addExternalFields(source: "profiles") {
+ name: String
+}
+```
~~~
spec/Section 3 -- Type System.md
~~~diff
@@ -1,51 +1,126 @@
# Type System
-The GraphQL Type system describes the capabilities of a GraphQL server and is
-used to determine if a query is valid. The type system also describes the
-input types of query variables to determine if values provided at runtime
-are valid.
+The GraphQL Type system describes the capabilities of a GraphQL service and is
+used to determine if a requested operation is valid, to guarantee the type of
+response results, and describes the input types of variables to determine if
+values provided at request time are valid.
+
+TypeSystemDocument : TypeSystemDefinition+
TypeSystemDefinition :
- SchemaDefinition
- TypeDefinition
- DirectiveDefinition
The GraphQL language includes an
[IDL](https://en.wikipedia.org/wiki/Interface_description_language) used to
describe a GraphQL service's type system. Tools may use this definition language
to provide utilities such as client code generation or service boot-strapping.
-GraphQL tools which only seek to provide GraphQL query execution may choose not
-to parse {TypeSystemDefinition}.
-
-A GraphQL Document which contains {TypeSystemDefinition} must not be executed;
-GraphQL execution services which receive a GraphQL Document containing type
-system definitions should return a descriptive error.
+GraphQL tools or services which only seek to execute GraphQL requests and not
+construct a new GraphQL schema may choose not to allow {TypeSystemDefinition}.
+Tools which only seek to produce schema and not execute requests may choose to
+only allow {TypeSystemDocument} and not allow {ExecutableDefinition} or
+{TypeSystemExtension} but should provide a descriptive error if present.
Note: The type system definition language is used throughout the remainder of
this specification document when illustrating example type systems.
## Type System Extensions
+TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+
+
+TypeSystemDefinitionOrExtension :
+ - TypeSystemDefinition
+ - TypeSystemExtension
+
TypeSystemExtension :
- SchemaExtension
- TypeExtension
-Type system extensions are used to represent a GraphQL type system which has been
-extended from some original type system. For example, this might be used by a
-local service to represent data a GraphQL client only accesses locally, or by a
-GraphQL service which is itself an extension of another GraphQL service.
+Type system extensions are used to represent a GraphQL type system which has
+been extended from some original type system. For example, this might be used by
+a local service to represent data a GraphQL client only accesses locally, or by
+a GraphQL service which is itself an extension of another GraphQL service.
+
+Tools which only seek to produce and extend schema and not execute requests may
+choose to only allow {TypeSystemExtensionDocument} and not allow
+{ExecutableDefinition} but should provide a descriptive error if present.
+
+
+## Descriptions
+
+Description : StringValue
+
+Documentation is a first-class feature of GraphQL type systems. To ensure
+the documentation of a GraphQL service remains consistent with its capabilities,
+descriptions of GraphQL definitions are provided alongside their definitions and
+made available via introspection.
+
+To allow GraphQL service designers to easily publish documentation alongside the
+capabilities of a GraphQL service, GraphQL descriptions are defined using the
+Markdown syntax (as specified by [CommonMark](https://commonmark.org/)). In the
+type system definition language, these description strings (often {BlockString})
+occur immediately before the definition they describe.
+
+GraphQL schema and all other definitions (e.g. types, fields, arguments, etc.)
+which can be described should provide a {Description} unless they are considered
+self descriptive.
+
+As an example, this simple GraphQL schema is well described:
+
+```raw graphql example
+"""
+A simple GraphQL schema which is well described.
+"""
+schema {
+ query: Query
+}
+
+"""
+Root type for all your query operations
+"""
+type Query {
+ """
+ Translates a string from a given language into a different language.
+ """
+ translate(
+ "The original language that `text` is provided in."
+ fromLanguage: Language
+
+ "The translated language to be returned."
+ toLanguage: Language
+
+ "The text to be translated."
+ text: String
+ ): String
+}
+
+"""
+The set of languages supported by `translate`.
+"""
+enum Language {
+ "English"
+ EN
+
+ "French"
+ FR
+
+ "Chinese"
+ CH
+}
+```
## Schema
-SchemaDefinition : schema Directives[Const]? { RootOperationTypeDefinition+ }
+SchemaDefinition : Description? schema Directives[Const]? { RootOperationTypeDefinition+ }
RootOperationTypeDefinition : OperationType : NamedType
A GraphQL service's collective type system capabilities are referred to as that
service's "schema". A schema is defined in terms of the types and directives it
supports as well as the root operation types for each kind of operation:
query, mutation, and subscription; this determines the place in the type system
where those operations begin.
@@ -64,60 +139,64 @@ begins with {"__"} (two underscores), as this is used exclusively by GraphQL's
introspection system.
### Root Operation Types
A schema defines the initial root operation type for each kind of operation it
supports: query, mutation, and subscription; this determines the place in the
type system where those operations begin.
-The `query` root operation type must be provided and must be an Object type.
+The {`query`} root operation type must be provided and must be an Object type.
-The `mutation` root operation type is optional; if it is not provided, the
+The {`mutation`} root operation type is optional; if it is not provided, the
service does not support mutations. If it is provided, it must be an
Object type.
-Similarly, the `subscription` root operation type is also optional; if it is not
-provided, the service does not support subscriptions. If it is provided, it must
-be an Object type.
+Similarly, the {`subscription`} root operation type is also optional; if it is
+not provided, the service does not support subscriptions. If it is provided, it
+must be an Object type.
-The fields on the `query` root operation type indicate what fields are available
-at the top level of a GraphQL query. For example, a basic GraphQL query like:
+The {`query`}, {`mutation`}, and {`subscription`} root types must all be
+different types if provided.
+
+The fields on the {`query`} root operation type indicate what fields are
+available at the top level of a GraphQL query operation.
+
+For example, this example operation:
```graphql example
query {
myName
}
```
-Is valid when the `query` root operation type has a field named "myName".
+is only valid when the {`query`} root operation type has a field named "myName":
```graphql example
type Query {
myName: String
}
```
-Similarly, the following mutation is valid if a `mutation` root operation type
-has a field named "setName". Note that the `query` and `mutation` root types
-must be different types.
+Similarly, the following mutation is only valid if the {`mutation`} root
+operation type has a field named "setName".
```graphql example
mutation {
setName(name: "Zuck") {
newName
}
}
```
When using the type system definition language, a document must include at most
-one `schema` definition.
+one {`schema`} definition.
In this example, a GraphQL schema is defined with both query and mutation
-root types:
+root operation types:
```graphql example
schema {
query: MyQueryRootType
mutation: MyMutationRootType
}
type MyQueryRootType {
@@ -127,107 +206,55 @@ type MyQueryRootType {
type MyMutationRootType {
setSomeField(to: String): String
}
```
**Default Root Operation Type Names**
While any type can be the root operation type for a GraphQL operation, the type
-system definition language can omit the schema definition when the `query`,
-`mutation`, and `subscription` root types are named `Query`, `Mutation`, and
-`Subscription` respectively.
+system definition language can omit the schema definition when the {`query`},
+{`mutation`}, and {`subscription`} root types are named {"Query"}, {"Mutation"},
+and {"Subscription"} respectively.
Likewise, when representing a GraphQL schema using the type system definition
language, a schema definition should be omitted if it only uses the default root
operation type names.
This example describes a valid complete GraphQL schema, despite not explicitly
-including a `schema` definition. The `Query` type is presumed to be the `query`
-root operation type of the schema.
+including a {`schema`} definition. The {"Query"} type is presumed to be the
+{`query`} root operation type of the schema.
```graphql example
type Query {
someField: String
}
```
### Schema Extension
SchemaExtension :
- - extend schema Directives[Const]? { OperationTypeDefinition+ }
- - extend schema Directives[Const]
+ - extend schema Directives[Const]? { RootOperationTypeDefinition+ }
+ - extend schema Directives[Const] [lookahead != `{`]
Schema extensions are used to represent a schema which has been extended from
an original schema. For example, this might be used by a GraphQL service which
adds additional operation types, or additional directives to an existing schema.
+Note: Schema extensions without additional operation type definitions must not
+be followed by a {`{`} (such as a query shorthand) to avoid parsing ambiguity.
+The same limitation applies to the type definitions and extensions below.
+
**Schema Validation**
Schema extensions have the potential to be invalid if incorrectly defined.
1. The Schema must already be defined.
-2. Any directives provided must not already apply to the original Schema.
-
-
-## Descriptions
-
-Description : StringValue
-
-Documentation is first-class feature of GraphQL type systems. To ensure
-the documentation of a GraphQL service remains consistent with its capabilities,
-descriptions of GraphQL definitions are provided alongside their definitions and
-made available via introspection.
-
-To allow GraphQL service designers to easily publish documentation alongside the
-capabilities of a GraphQL service, GraphQL descriptions are defined using the
-Markdown syntax (as specified by [CommonMark](http://commonmark.org/)). In the
-type system definition language, these description strings (often {BlockString})
-occur immediately before the definition they describe.
-
-All GraphQL types, fields, arguments and other definitions which can be
-described should provide a {Description} unless they are considered self
-descriptive.
-
-As an example, this simple GraphQL schema is well described:
-
-```graphql example
-"""
-A simple GraphQL schema which is well described.
-"""
-type Query {
- """
- Translates a string from a given language into a different language.
- """
- translate(
- "The original language that `text` is provided in."
- fromLanguage: Language
-
- "The translated language to be returned."
- toLanguage: Language
-
- "The text to be translated."
- text: String
- ): String
-}
-
-"""
-The set of languages supported by `translate`.
-"""
-enum Language {
- "English"
- EN
-
- "French"
- FR
-
- "Chinese"
- CH
-}
-```
+2. Any non-repeatable directives provided must not already apply to the
+ original Schema.
## Types
TypeDefinition :
- ScalarTypeDefinition
- ObjectTypeDefinition
- InterfaceTypeDefinition
@@ -244,53 +271,54 @@ are enumerable. GraphQL offers an `Enum` type in those cases, where the type
specifies the space of valid responses.
Scalars and Enums form the leaves in response trees; the intermediate levels are
`Object` types, which define a set of fields, where each field is another
type in the system, allowing the definition of arbitrary type hierarchies.
GraphQL supports two abstract types: interfaces and unions.
-An `Interface` defines a list of fields; `Object` types that implement that
-interface are guaranteed to implement those fields. Whenever the type system
-claims it will return an interface, it will return a valid implementing type.
+An `Interface` defines a list of fields; `Object` types and other Interface
+types which implement this Interface are guaranteed to implement those fields.
+Whenever a field claims it will return an Interface type, it will return a
+valid implementing Object type during execution.
A `Union` defines a list of possible types; similar to interfaces, whenever the
type system claims a union will be returned, one of the possible types will be
returned.
Finally, oftentimes it is useful to provide complex structs as inputs to
GraphQL field arguments or variables; the `Input Object` type allows the schema
to define exactly what data is expected.
### Wrapping Types
All of the types so far are assumed to be both nullable and singular: e.g. a
scalar string returns either null or a singular string.
-A GraphQL schema may describe that a field represents list of another types;
+A GraphQL schema may describe that a field represents a list of another type;
the `List` type is provided for this reason, and wraps another type.
Similarly, the `Non-Null` type wraps another type, and denotes that the
-resulting value will never be {null} (and that an error cannot result in a
+resulting value will never be {null} (and that a field error cannot result in a
{null} value).
These two types are referred to as "wrapping types"; non-wrapping types are
referred to as "named types". A wrapping type has an underlying named type,
found by continually unwrapping the type until a named type is found.
### Input and Output Types
Types are used throughout GraphQL to describe both the values accepted as input
to arguments and variables as well as the values output by fields. These two
uses categorize types as *input types* and *output types*. Some kinds of types,
like Scalar and Enum types, can be used as both input types and output types;
-other kinds types can only be used in one or the other. Input Object types can
+other kinds of types can only be used in one or the other. Input Object types can
only be used as input types. Object, Interface, and Union types can only be used
as output types. Lists and Non-Null types may be used as input types or output
types depending on how the wrapped type may be used.
IsInputType(type) :
* If {type} is a List type or Non-Null type:
* Let {unwrappedType} be the unwrapped type of {type}.
* Return IsInputType({unwrappedType})
@@ -322,273 +350,319 @@ from some original type. For example, this might be used by a local service to
represent additional fields a GraphQL client only accesses locally.
## Scalars
ScalarTypeDefinition : Description? scalar Name Directives[Const]?
Scalar types represent primitive leaf values in a GraphQL type system. GraphQL
-responses take the form of a hierarchical tree; the leaves on these trees are
-GraphQL scalars.
-
-All GraphQL scalars are representable as strings, though depending on the
-response format being used, there may be a more appropriate primitive for the
-given scalar type, and server should use those types when appropriate.
-
-GraphQL provides a number of built-in scalars, but type systems can add
-additional scalars with semantic meaning. For example, a GraphQL system could
-define a scalar called `Time` which, while serialized as a string, promises to
-conform to ISO-8601. When querying a field of type `Time`, you can then rely on
-the ability to parse the result with an ISO-8601 parser and use a
-client-specific primitive for time. Another example of a potentially useful
-custom scalar is `Url`, which serializes as a string, but is guaranteed by
-the server to be a valid URL.
+responses take the form of a hierarchical tree; the leaves of this tree are
+typically GraphQL Scalar types (but may also be Enum types or {null} values).
+
+GraphQL provides a number of built-in scalars which are fully defined in the
+sections below, however type systems may also add additional custom scalars to
+introduce additional semantic meaning.
+
+**Built-in Scalars**
+
+GraphQL specifies a basic set of well-defined Scalar types: {Int}, {Float},
+{String}, {Boolean}, and {ID}. A GraphQL framework should support all of these
+types, and a GraphQL service which provides a type by these names must adhere to
+the behavior described for them in this document. As an example, a service must
+not include a type called {Int} and use it to represent 64-bit numbers,
+internationalization information, or anything other than what is defined in
+this document.
+
+When returning the set of types from the `__Schema` introspection type, all
+referenced built-in scalars must be included. If a built-in scalar type is not
+referenced anywhere in a schema (there is no field, argument, or input field of
+that type) then it must not be included.
+
+When representing a GraphQL schema using the type system definition language,
+all built-in scalars must be omitted for brevity.
+
+**Custom Scalars**
+
+GraphQL services may use custom scalar types in addition to the built-in
+scalars. For example, a GraphQL service could define a scalar called `UUID`
+which, while serialized as a string, conforms to [RFC 4122](https://tools.ietf.org/html/rfc4122).
+When querying a field of type `UUID`, you can then rely on the ability to parse
+the result with a RFC 4122 compliant parser. Another example of a potentially
+useful custom scalar is `URL`, which serializes as a string, but is guaranteed
+by the server to be a valid URL.
+
+:: When defining a custom scalar, GraphQL services should provide a *scalar
+specification URL* via the `@specifiedBy` directive or the `specifiedByURL`
+introspection field. This URL must link to a human-readable specification of the
+data format, serialization, and coercion rules for the scalar.
+
+For example, a GraphQL service providing a `UUID` scalar may link to RFC 4122,
+or some custom document defining a reasonable subset of that RFC. If a *scalar
+specification URL* is present, systems and tools that are aware of it should
+conform to its described rules.
```graphql example
-scalar Time
-scalar Url
+scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
+scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
```
-A server may omit any of the built-in scalars from its schema, for example if a
-schema does not refer to a floating-point number, then it must not include the
-`Float` type. However, if a schema includes a type with the name of one of the
-types described here, it must adhere to the behavior described. As an example,
-a server must not include a type called `Int` and use it to represent
-128-bit numbers, internationalization information, or anything other than what
-is defined in this document.
+Custom *scalar specification URL*s should provide a single, stable format to
+avoid ambiguity. If the linked specification is in flux, the service should link
+to a fixed version rather than to a resource which might change.
-When representing a GraphQL schema using the type system definition language,
-the built-in scalar types should be omitted for brevity.
+Custom *scalar specification URL*s should not be changed once defined. Doing so
+would likely disrupt tooling or could introduce breaking changes within the
+linked specification's contents.
-**Result Coercion**
+Built-in scalar types must not provide a *scalar specification URL* as they are
+specified by this document.
+
+Note: Custom scalars should also summarize the specified format and provide
+examples in their description.
-A GraphQL server, when preparing a field of a given scalar type, must uphold the
+**Result Coercion and Serialization**
+
+A GraphQL service, when preparing a field of a given scalar type, must uphold the
contract the scalar type describes, either by coercing the value or producing a
-field error if a value cannot be coerced or if coercion may result in data loss.
+[field error](#sec-Errors.Field-errors) if a value cannot be coerced or if
+coercion may result in data loss.
A GraphQL service may decide to allow coercing different internal types to the
-expected return type. For example when coercing a field of type `Int` a boolean
-`true` value may produce `1` or a string value `"123"` may be parsed as base-10
-`123`. However if internal type coercion cannot be reasonably performed without
+expected return type. For example when coercing a field of type {Int} a boolean
+{true} value may produce {1} or a string value {"123"} may be parsed as base-10
+{123}. However if internal type coercion cannot be reasonably performed without
losing information, then it must raise a field error.
-Since this coercion behavior is not observable to clients of the GraphQL server,
+Since this coercion behavior is not observable to clients of the GraphQL service,
the precise rules of coercion are left to the implementation. The only
-requirement is that the server must yield values which adhere to the expected
+requirement is that the service must yield values which adhere to the expected
Scalar type.
+GraphQL scalars are serialized according to the serialization format being used.
+There may be a most appropriate serialized primitive for each given scalar type,
+and the service should produce each primitive where appropriate.
+
+See [Serialization Format](#sec-Serialization-Format) for more detailed
+information on the serialization of scalars in common JSON and other formats.
+
**Input Coercion**
-If a GraphQL server expects a scalar type as input to an argument, coercion
+If a GraphQL service expects a scalar type as input to an argument, coercion
is observable and the rules must be well defined. If an input value does not
-match a coercion rule, a query error must be raised.
+match a coercion rule, a [request error](#sec-Errors.Request-errors) must be
+raised (input values are validated before execution begins).
GraphQL has different constant literals to represent integer and floating-point
input values, and coercion rules may apply differently depending on which type
-of input value is encountered. GraphQL may be parameterized by query variables,
-the values of which are often serialized when sent over a transport like HTTP. Since
+of input value is encountered. GraphQL may be parameterized by variables, the
+values of which are often serialized when sent over a transport like HTTP. Since
some common serializations (ex. JSON) do not discriminate between integer
and floating-point values, they are interpreted as an integer input value if
they have an empty fractional part (ex. `1.0`) and otherwise as floating-point
input value.
For all types below, with the exception of Non-Null, if the explicit value
{null} is provided, then the result of input coercion is {null}.
-**Built-in Scalars**
-
-GraphQL provides a basic set of well-defined Scalar types. A GraphQL server
-should support all of these types, and a GraphQL server which provide a type by
-these names must adhere to the behavior described below.
-
### Int
The Int scalar type represents a signed 32-bit numeric non-fractional value.
Response formats that support a 32-bit integer or a number type should use
that type to represent this scalar.
**Result Coercion**
-Fields returning the type `Int` expect to encounter 32-bit integer
+Fields returning the type {Int} expect to encounter 32-bit integer
internal values.
-GraphQL servers may coerce non-integer internal values to integers when
+GraphQL services may coerce non-integer internal values to integers when
reasonable without losing information, otherwise they must raise a field error.
Examples of this may include returning `1` for the floating-point number `1.0`,
or returning `123` for the string `"123"`. In scenarios where coercion may lose
data, raising a field error is more appropriate. For example, a floating-point
number `1.2` should raise a field error instead of being truncated to `1`.
If the integer internal value represents a value less than -231 or
greater than or equal to 231, a field error should be raised.
**Input Coercion**
When expected as an input type, only integer input values are accepted. All
-other input values, including strings with numeric content, must raise a query
+other input values, including strings with numeric content, must raise a request
error indicating an incorrect type. If the integer input value represents a
value less than -231 or greater than or equal to 231, a
-query error should be raised.
+request error should be raised.
Note: Numeric integer values larger than 32-bit should either use String or a
custom-defined Scalar type, as not all platforms and transports support
encoding integer numbers larger than 32-bit.
### Float
-The Float scalar type represents signed double-precision fractional values
-as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point).
-Response formats that support an appropriate double-precision number type
-should use that type to represent this scalar.
+The Float scalar type represents signed double-precision finite values as
+specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
+Response formats that support an appropriate double-precision number type should
+use that type to represent this scalar.
**Result Coercion**
-Fields returning the type `Float` expect to encounter double-precision
+Fields returning the type {Float} expect to encounter double-precision
floating-point internal values.
-GraphQL servers may coerce non-floating-point internal values to `Float` when
+GraphQL services may coerce non-floating-point internal values to {Float} when
reasonable without losing information, otherwise they must raise a field error.
Examples of this may include returning `1.0` for the integer number `1`, or
`123.0` for the string `"123"`.
+Non-finite floating-point internal values ({NaN} and {Infinity}) cannot be
+coerced to {Float} and must raise a field error.
+
**Input Coercion**
When expected as an input type, both integer and float input values are
accepted. Integer input values are coerced to Float by adding an empty
fractional part, for example `1.0` for the integer input value `1`. All
-other input values, including strings with numeric content, must raise a query
-error indicating an incorrect type. If the integer input value represents a
-value not representable by IEEE 754, a query error should be raised.
+other input values, including strings with numeric content, must raise a request
+error indicating an incorrect type. If the input value otherwise represents a
+value not representable by finite IEEE 754 (e.g. {NaN}, {Infinity}, or a value
+outside the available precision), a request error must be raised.
### String
-The String scalar type represents textual data, represented as UTF-8 character
-sequences. The String type is most often used by GraphQL to represent free-form
-human-readable text. All response formats must support string representations,
-and that representation must be used here.
+The String scalar type represents textual data, represented as a sequence of
+Unicode code points. The String type is most often used by GraphQL to
+represent free-form human-readable text. How the String is encoded internally
+(for example UTF-8) is left to the service implementation. All response
+serialization formats must support a string representation (for example, JSON
+Unicode strings), and that representation must be used to serialize this type.
**Result Coercion**
-Fields returning the type `String` expect to encounter UTF-8 string internal values.
+Fields returning the type {String} expect to encounter Unicode string values.
-GraphQL servers may coerce non-string raw values to `String` when reasonable
+GraphQL services may coerce non-string raw values to {String} when reasonable
without losing information, otherwise they must raise a field error. Examples of
this may include returning the string `"true"` for a boolean true value, or the
string `"1"` for the integer `1`.
**Input Coercion**
-When expected as an input type, only valid UTF-8 string input values are
-accepted. All other input values must raise a query error indicating an
+When expected as an input type, only valid Unicode string input values are
+accepted. All other input values must raise a request error indicating an
incorrect type.
### Boolean
The Boolean scalar type represents `true` or `false`. Response formats should
use a built-in boolean type if supported; otherwise, they should use their
representation of the integers `1` and `0`.
**Result Coercion**
-Fields returning the type `Boolean` expect to encounter boolean internal values.
+Fields returning the type {Boolean} expect to encounter boolean internal values.
-GraphQL servers may coerce non-boolean raw values to `Boolean` when reasonable
+GraphQL services may coerce non-boolean raw values to {Boolean} when reasonable
without losing information, otherwise they must raise a field error. Examples of
this may include returning `true` for non-zero numbers.
**Input Coercion**
When expected as an input type, only boolean input values are accepted. All
-other input values must raise a query error indicating an incorrect type.
+other input values must raise a request error indicating an incorrect type.
### ID
The ID scalar type represents a unique identifier, often used to refetch an
object or as the key for a cache. The ID type is serialized in the same way as
-a `String`; however, it is not intended to be human-readable. While it is
-often numeric, it should always serialize as a `String`.
+a {String}; however, it is not intended to be human-readable. While it is
+often numeric, it should always serialize as a {String}.
**Result Coercion**
GraphQL is agnostic to ID format, and serializes to string to ensure consistency
across many formats ID could represent, from small auto-increment numbers, to
large 128-bit random numbers, to base64 encoded values, or string values of a
-format like [GUID](http://en.wikipedia.org/wiki/Globally_unique_identifier).
+format like [GUID](https://en.wikipedia.org/wiki/Globally_unique_identifier).
-GraphQL servers should coerce as appropriate given the ID formats they expect.
+GraphQL services should coerce as appropriate given the ID formats they expect.
When coercion is not possible they must raise a field error.
**Input Coercion**
-When expected as an input type, any string (such as `"4"`) or integer (such
-as `4`) input value should be coerced to ID as appropriate for the ID formats
-a given GraphQL server expects. Any other input value, including float input
-values (such as `4.0`), must raise a query error indicating an incorrect type.
+When expected as an input type, any string (such as `"4"`) or integer (such as
+`4` or `-4`) input value should be coerced to ID as appropriate for the ID
+formats a given GraphQL service expects. Any other input value, including float
+input values (such as `4.0`), must raise a request error indicating an incorrect
+type.
### Scalar Extensions
ScalarTypeExtension :
- extend scalar Name Directives[Const]
Scalar type extensions are used to represent a scalar type which has been
extended from some original scalar type. For example, this might be used by a
GraphQL tool or service which adds directives to an existing scalar.
**Type Validation**
Scalar type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be a Scalar type.
-2. Any directives provided must not already apply to the original Scalar type.
+2. Any non-repeatable directives provided must not already apply to the
+ original Scalar type.
## Objects
-ObjectTypeDefinition : Description? type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
+ObjectTypeDefinition :
+ - Description? type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
+ - Description? type Name ImplementsInterfaces? Directives[Const]? [lookahead != `{`]
ImplementsInterfaces :
- - implements `&`? NamedType
- ImplementsInterfaces & NamedType
+ - implements `&`? NamedType
FieldsDefinition : { FieldDefinition+ }
FieldDefinition : Description? Name ArgumentsDefinition? : Type Directives[Const]?
-GraphQL queries are hierarchical and composed, describing a tree of information.
-While Scalar types describe the leaf values of these hierarchical queries, Objects
-describe the intermediate levels.
+GraphQL operations are hierarchical and composed, describing a tree of
+information. While Scalar types describe the leaf values of these hierarchical
+operations, Objects describe the intermediate levels.
GraphQL Objects represent a list of named fields, each of which yield a value of
a specific type. Object values should be serialized as ordered maps, where the
-queried field names (or aliases) are the keys and the result of evaluating
-the field is the value, ordered by the order in which they appear in the query.
+selected field names (or aliases) are the keys and the result of evaluating
+the field is the value, ordered by the order in which they appear in
+the selection set.
All fields defined within an Object type must not have a name which begins with
{"__"} (two underscores), as this is used exclusively by GraphQL's
introspection system.
For example, a type `Person` could be described as:
```graphql example
type Person {
name: String
age: Int
picture: Url
}
```
-Where `name` is a field that will yield a `String` value, and `age` is a field
-that will yield an `Int` value, and `picture` is a field that will yield a
+Where `name` is a field that will yield a {String} value, and `age` is a field
+that will yield an {Int} value, and `picture` is a field that will yield a
`Url` value.
A query of an object value must select at least one field. This selection of
fields will yield an ordered map containing exactly the subset of the object
queried, which should be represented in the order in which they were queried.
Only fields that are declared on the object type may validly be queried on
that object.
@@ -640,18 +714,18 @@ For example, the `Person` type might include a `relationship`:
type Person {
name: String
age: Int
picture: Url
relationship: Person
}
```
-Valid queries must supply a nested field set for a field that returns
-an object, so this query is not valid:
+Valid operations must supply a nested field set for any field that returns an
+object, so this operation is not valid:
```graphql counter-example
{
name
relationship
}
```
@@ -675,17 +749,17 @@ And will yield the subset of each object type queried:
"name": "Priscilla Chan"
}
}
```
**Field Ordering**
When querying an Object, the resulting mapping of fields are conceptually
-ordered in the same order in which they were encountered during query execution,
+ordered in the same order in which they were encountered during execution,
excluding fragments for which the type does not apply and fields or
fragments that are skipped via `@skip` or `@include` directives. This ordering
is correctly produced when using the {CollectFields()} algorithm.
Response serialization formats capable of representing ordered maps should
maintain this ordering. Serialization formats which can only represent unordered
maps (such as JSON) should retain this order textually. That is, if two fields
`{foo, bar}` were queried in that order, the resulting JSON serialization
@@ -798,41 +872,64 @@ of rules must be adhered to by every Object type in a GraphQL schema.
2. The field must not have a name which begins with the
characters {"__"} (two underscores).
3. The field must return a type where {IsOutputType(fieldType)} returns {true}.
4. For each argument of the field:
1. The argument must not have a name which begins with the
characters {"__"} (two underscores).
2. The argument must accept a type where {IsInputType(argumentType)}
returns {true}.
-4. An object type may declare that it implements one or more unique interfaces.
-5. An object type must be a super-set of all interfaces it implements:
- 1. The object type must include a field of the same name for every field
- defined in an interface.
- 1. The object field must be of a type which is equal to or a sub-type of
- the interface field (covariant).
- 1. An object field type is a valid sub-type if it is equal to (the same
- type as) the interface field type.
- 2. An object field type is a valid sub-type if it is an Object type and
- the interface field type is either an Interface type or a Union type
- and the object field type is a possible type of the interface field
- type.
- 3. An object field type is a valid sub-type if it is a List type and
- the interface field type is also a List type and the list-item type
- of the object field type is a valid sub-type of the list-item type
- of the interface field type.
- 4. An object field type is a valid sub-type if it is a Non-Null variant
- of a valid sub-type of the interface field type.
- 2. The object field must include an argument of the same name for every
- argument defined in the interface field.
- 1. The object field argument must accept the same type (invariant) as
- the interface field argument.
- 3. The object field may include additional arguments not defined in the
- interface field, but any additional argument must not be required, e.g.
- must not be of a non-nullable type.
+3. An object type may declare that it implements one or more unique interfaces.
+4. An object type must be a super-set of all interfaces it implements:
+ 1. Let this object type be {objectType}.
+ 2. For each interface declared implemented as {interfaceType},
+ {IsValidImplementation(objectType, interfaceType)} must be {true}.
+
+IsValidImplementation(type, implementedType):
+
+ 1. If {implementedType} declares it implements any interfaces,
+ {type} must also declare it implements those interfaces.
+ 2. {type} must include a field of the same name for every field
+ defined in {implementedType}.
+ 1. Let {field} be that named field on {type}.
+ 2. Let {implementedField} be that named field on {implementedType}.
+ 3. {field} must include an argument of the same name for every argument
+ defined in {implementedField}.
+ 1. That named argument on {field} must accept the same type
+ (invariant) as that named argument on {implementedField}.
+ 4. {field} may include additional arguments not defined in
+ {implementedField}, but any additional argument must not be required,
+ e.g. must not be of a non-nullable type.
+ 5. {field} must return a type which is equal to or a sub-type of
+ (covariant) the return type of {implementedField} field's return type:
+ 1. Let {fieldType} be the return type of {field}.
+ 2. Let {implementedFieldType} be the return type of {implementedField}.
+ 3. {IsValidImplementationFieldType(fieldType, implementedFieldType)}
+ must be {true}.
+
+IsValidImplementationFieldType(fieldType, implementedFieldType):
+ 1. If {fieldType} is a Non-Null type:
+ 1. Let {nullableType} be the unwrapped nullable type of {fieldType}.
+ 2. Let {implementedNullableType} be the unwrapped nullable type
+ of {implementedFieldType} if it is a Non-Null type, otherwise let it be
+ {implementedFieldType} directly.
+ 3. Return {IsValidImplementationFieldType(nullableType, implementedNullableType)}.
+ 2. If {fieldType} is a List type and {implementedFieldType} is also a List type:
+ 1. Let {itemType} be the unwrapped item type of {fieldType}.
+ 2. Let {implementedItemType} be the unwrapped item type
+ of {implementedFieldType}.
+ 3. Return {IsValidImplementationFieldType(itemType, implementedItemType)}.
+ 3. If {fieldType} is the same type as {implementedFieldType} then return {true}.
+ 4. If {fieldType} is an Object type and {implementedFieldType} is
+ a Union type and {fieldType} is a possible type of {implementedFieldType}
+ then return {true}.
+ 5. If {fieldType} is an Object or Interface type and {implementedFieldType}
+ is an Interface type and {fieldType} declares it implements
+ {implementedFieldType} then return {true}.
+ 6. Otherwise return {false}.
### Field Arguments
ArgumentsDefinition : ( InputValueDefinition+ )
InputValueDefinition : Description? Name : Type DefaultValue? Directives[Const]?
@@ -850,64 +947,64 @@ determine what size of an image to return.
```graphql example
type Person {
name: String
picture(size: Int): Url
}
```
-GraphQL queries can optionally specify arguments to their fields to provide
+Operations can optionally specify arguments to their fields to provide
these arguments.
-This example query:
+This example operation:
```graphql example
{
name
picture(size: 600)
}
```
-May yield the result:
+May return the result:
```json example
{
"name": "Mark Zuckerberg",
"picture": "http://some.cdn/picture_600.jpg"
}
```
The type of an object field argument must be an input type (any type except an
Object, Interface, or Union type).
### Field Deprecation
Fields in an object may be marked as deprecated as deemed necessary by the
-application. It is still legal to query for these fields (to ensure existing
-clients are not broken by the change), but the fields should be appropriately
-treated in documentation and tooling.
+application. It is still legal to include these fields in a selection set
+(to ensure existing clients are not broken by the change), but the fields should
+be appropriately treated in documentation and tooling.
When using the type system definition language, `@deprecated` directives are
used to indicate that a field is deprecated:
```graphql example
type ExampleType {
oldField: String @deprecated
}
```
### Object Extensions
ObjectTypeExtension :
- extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
- - extend type Name ImplementsInterfaces? Directives[Const]
- - extend type Name ImplementsInterfaces
+ - extend type Name ImplementsInterfaces? Directives[Const] [lookahead != `{`]
+ - extend type Name ImplementsInterfaces [lookahead != `{`]
Object type extensions are used to represent a type which has been extended from
some original type. For example, this might be used to represent local data, or
by a GraphQL service which is itself an extension of another GraphQL service.
In this example, a local data field is added to a `Story` type:
```graphql example
@@ -929,30 +1026,33 @@ extend type User @addedDirective
Object type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be an Object type.
2. The fields of an Object type extension must have unique names; no two fields
may share the same name.
3. Any fields of an Object type extension must not be already defined on the
original Object type.
-4. Any directives provided must not already apply to the original Object type.
+4. Any non-repeatable directives provided must not already apply to the
+ original Object type.
5. Any interfaces provided must not be already implemented by the original
Object type.
6. The resulting extended object type must be a super-set of all interfaces it
implements.
## Interfaces
-InterfaceTypeDefinition : Description? interface Name Directives[Const]? FieldsDefinition?
+InterfaceTypeDefinition :
+ - Description? interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
+ - Description? interface Name ImplementsInterfaces? Directives[Const]? [lookahead != `{`]
GraphQL interfaces represent a list of named fields and their arguments. GraphQL
-objects can then implement these interfaces which requires that the object type
-will define all fields defined by those interfaces.
+objects and interfaces can then implement these interfaces which requires that
+the implementing type will define all fields defined by those interfaces.
Fields on a GraphQL interface have the same rules as fields on a GraphQL object;
their type can be Scalar, Object, Enum, Interface, or Union, or any wrapping
type whose base type is one of those five.
For example, an interface `NamedEntity` may describe a required field and types
such as `Person` or `Business` may then implement this interface to guarantee
this field will always exist.
@@ -989,59 +1089,116 @@ To continue the example, a `Contact` might refer to `NamedEntity`.
```graphql example
type Contact {
entity: NamedEntity
phoneNumber: String
address: String
}
```
-This allows us to write a query for a `Contact` that can select the
+This allows us to write a selection set for a `Contact` that can select the
common fields.
```graphql example
{
entity {
name
}
phoneNumber
}
```
-When querying for fields on an interface type, only those fields declared on
+When selecting fields on an interface type, only those fields declared on
the interface may be queried. In the above example, `entity` returns a
`NamedEntity`, and `name` is defined on `NamedEntity`, so it is valid. However,
-the following would not be a valid query:
+the following would not be a valid selection set against `Contact`:
```graphql counter-example
{
entity {
name
age
}
phoneNumber
}
```
because `entity` refers to a `NamedEntity`, and `age` is not defined on that
interface. Querying for `age` is only valid when the result of `entity` is a
-`Person`; the query can express this using a fragment or an inline fragment:
+`Person`; this can be expressed using a fragment or an inline fragment:
```graphql example
{
entity {
name
... on Person {
age
}
- },
+ }
phoneNumber
}
```
+**Interfaces Implementing Interfaces**
+
+When defining an interface that implements another interface, the implementing
+interface must define each field that is specified by the implemented interface.
+For example, the interface Resource must define the field id to implement the
+Node interface:
+
+```raw graphql example
+interface Node {
+ id: ID!
+}
+
+interface Resource implements Node {
+ id: ID!
+ url: String
+}
+```
+
+Transitively implemented interfaces (interfaces implemented by the interface
+that is being implemented) must also be defined on an implementing type or
+interface. For example, `Image` cannot implement `Resource` without also
+implementing `Node`:
+
+```raw graphql example
+interface Node {
+ id: ID!
+}
+
+interface Resource implements Node {
+ id: ID!
+ url: String
+}
+
+interface Image implements Resource & Node {
+ id: ID!
+ url: String
+ thumbnail: String
+}
+```
+
+Interface definitions must not contain cyclic references nor implement
+themselves. This example is invalid because `Node` and `Named` implement
+themselves and each other:
+
+```graphql counter-example
+interface Node implements Named & Node {
+ id: ID!
+ name: String
+}
+
+interface Named implements Node & Named {
+ id: ID!
+ name: String
+}
+```
+
+
**Result Coercion**
The interface type should have some way of determining which object a given
result corresponds to. Once it has done so, the result coercion of the interface
is the same as the result coercion of the object.
**Input Coercion**
@@ -1059,23 +1216,30 @@ Interface types have the potential to be invalid if incorrectly defined.
characters {"__"} (two underscores).
3. The field must return a type where {IsOutputType(fieldType)}
returns {true}.
4. For each argument of the field:
1. The argument must not have a name which begins with the
characters {"__"} (two underscores).
2. The argument must accept a type where {IsInputType(argumentType)}
returns {true}.
+3. An interface type may declare that it implements one or more unique
+ interfaces, but may not implement itself.
+4. An interface type must be a super-set of all interfaces it implements:
+ 1. Let this interface type be {implementingType}.
+ 2. For each interface declared implemented as {implementedType},
+ {IsValidImplementation(implementingType, implementedType)} must be {true}.
### Interface Extensions
InterfaceTypeExtension :
- - extend interface Name Directives[Const]? FieldsDefinition
- - extend interface Name Directives[Const]
+ - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
+ - extend interface Name ImplementsInterfaces? Directives[Const] [lookahead != `{`]
+ - extend interface Name ImplementsInterfaces [lookahead != `{`]
Interface type extensions are used to represent an interface which has been
extended from some original interface. For example, this might be used to
represent common local data on many types, or by a GraphQL service which is
itself an extension of another GraphQL service.
In this example, an extended data field is added to a `NamedEntity` type along
with the types which implement it:
@@ -1108,40 +1272,44 @@ extend interface NamedEntity @addedDirective
Interface type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be an Interface type.
2. The fields of an Interface type extension must have unique names; no two
fields may share the same name.
3. Any fields of an Interface type extension must not be already defined on the
original Interface type.
-4. Any Object type which implemented the original Interface type must also be a
- super-set of the fields of the Interface type extension (which may be due to
- Object type extension).
-5. Any directives provided must not already apply to the original Interface type.
+4. Any Object or Interface type which implemented the original Interface type
+ must also be a super-set of the fields of the Interface type extension (which
+ may be due to Object type extension).
+5. Any non-repeatable directives provided must not already apply to the
+ original Interface type.
+6. The resulting extended Interface type must be a super-set of all Interfaces
+ it implements.
## Unions
UnionTypeDefinition : Description? union Name Directives[Const]? UnionMemberTypes?
UnionMemberTypes :
- - = `|`? NamedType
- UnionMemberTypes | NamedType
+ - = `|`? NamedType
GraphQL Unions represent an object that could be one of a list of GraphQL
Object types, but provides for no guaranteed fields between those types.
They also differ from interfaces in that Object types declare what interfaces
they implement, but are not aware of what unions contain them.
With interfaces and objects, only those fields defined on the type can be
queried directly; to query other fields on an interface, typed fragments
must be used. This is the same as for unions, but unions do not define any
fields, so **no** fields may be queried on this type without the use of
-type refining fragments or inline fragments.
+type refining fragments or inline fragments (with the exception of the
+meta-field {__typename}).
For example, we might define the following types:
```graphql example
union SearchResult = Photo | Person
type Person {
name: String
@@ -1153,32 +1321,30 @@ type Photo {
width: Int
}
type SearchQuery {
firstSearchResult: SearchResult
}
```
-When querying the `firstSearchResult` field of type `SearchQuery`, the
-query would ask for all fields inside of a fragment indicating the appropriate
-type. If the query wanted the name if the result was a Person, and the height if
-it was a photo, the following query is invalid, because the union itself
-defines no fields:
+In this example, a query operation wants the name if the result was a Person,
+and the height if it was a photo. However because a union itself defines no
+fields, this could be ambiguous and is invalid.
```graphql counter-example
{
firstSearchResult {
name
height
}
}
```
-Instead, the query would be:
+A valid operation includes typed fragments (in this example, inline fragments):
```graphql example
{
firstSearchResult {
... on Person {
name
}
... on Photo {
@@ -1186,17 +1352,17 @@ Instead, the query would be:
}
}
}
```
Union members may be defined with an optional leading `|` character to aid
formatting when representing a longer list of possible types:
-```graphql example
+```raw graphql example
union SearchResult =
| Photo
| Person
```
**Result Coercion**
The union type should have some way of determining which object a given result
@@ -1234,27 +1400,30 @@ Union type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be a Union type.
2. The member types of a Union type extension must all be Object base types;
Scalar, Interface and Union types must not be member types of a Union.
Similarly, wrapping types must not be member types of a Union.
3. All member types of a Union type extension must be unique.
4. All member types of a Union type extension must not already be a member of
the original Union type.
-5. Any directives provided must not already apply to the original Union type.
+5. Any non-repeatable directives provided must not already apply to the
+ original Union type.
## Enums
-EnumTypeDefinition : Description? enum Name Directives[Const]? EnumValuesDefinition?
+EnumTypeDefinition :
+ - Description? enum Name Directives[Const]? EnumValuesDefinition
+ - Description? enum Name Directives[Const]? [lookahead != `{`]
EnumValuesDefinition : { EnumValueDefinition+ }
EnumValueDefinition : Description? EnumValue Directives[Const]?
-GraphQL Enum types, like scalar types, also represent leaf values in a GraphQL
+GraphQL Enum types, like Scalar types, also represent leaf values in a GraphQL
type system. However Enum types describe the set of possible values.
Enums are not references for a numeric value, but are unique values in their own
right. They may serialize as a string: the name of the represented value.
In this example, an Enum type called `Direction` is defined:
```graphql example
@@ -1263,62 +1432,65 @@ enum Direction {
EAST
SOUTH
WEST
}
```
**Result Coercion**
-GraphQL servers must return one of the defined set of possible values. If a
+GraphQL services must return one of the defined set of possible values. If a
reasonable coercion is not possible they must raise a field error.
**Input Coercion**
GraphQL has a constant literal to represent enum input values. GraphQL string
-literals must not be accepted as an enum input and instead raise a query error.
+literals must not be accepted as an enum input and instead raise a request error.
-Query variable transport serializations which have a different representation
+Variable transport serializations which have a different representation
for non-string symbolic values (for example, [EDN](https://github.com/edn-format/edn))
should only allow such values as enum input values. Otherwise, for most
transport serializations that do not, strings may be interpreted as the enum
input value with the same name.
**Type Validation**
Enum types have the potential to be invalid if incorrectly defined.
1. An Enum type must define one or more unique enum values.
### Enum Extensions
EnumTypeExtension :
- extend enum Name Directives[Const]? EnumValuesDefinition
- - extend enum Name Directives[Const]
+ - extend enum Name Directives[Const] [lookahead != `{`]
Enum type extensions are used to represent an enum type which has been
extended from some original enum type. For example, this might be used to
represent additional local data, or by a GraphQL service which is itself an
extension of another GraphQL service.
**Type Validation**
Enum type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be an Enum type.
2. All values of an Enum type extension must be unique.
3. All values of an Enum type extension must not already be a value of
the original Enum.
-4. Any directives provided must not already apply to the original Enum type.
+4. Any non-repeatable directives provided must not already apply to the
+ original Enum type.
## Input Objects
-InputObjectTypeDefinition : Description? input Name Directives[Const]? InputFieldsDefinition?
+InputObjectTypeDefinition :
+ - Description? input Name Directives[Const]? InputFieldsDefinition
+ - Description? input Name Directives[Const]? [lookahead != `{`]
InputFieldsDefinition : { InputValueDefinition+ }
Fields may accept arguments to configure their behavior. These inputs are often
scalars or enums, but they sometimes need to represent more complex values.
A GraphQL Input Object defines a set of input fields; the input fields are either
scalars, enums, or other input objects. This allows arguments to accept
@@ -1334,51 +1506,105 @@ input Point2D {
```
Note: The GraphQL Object type ({ObjectTypeDefinition}) defined above is
inappropriate for re-use here, because Object types can contain fields that
define arguments or contain references to interfaces and unions, neither of
which is appropriate for use as an input argument. For this reason, input
objects have a separate type in the system.
+**Circular References**
+
+Input Objects are allowed to reference other Input Objects as field types. A
+circular reference occurs when an Input Object references itself either directly
+or through referenced Input Objects.
+
+Circular references are generally allowed, however they may not be defined as an
+unbroken chain of Non-Null singular fields. Such Input Objects are invalid
+because there is no way to provide a legal value for them.
+
+This example of a circularly-referenced input type is valid as the field `self`
+may be omitted or the value {null}.
+
+```graphql example
+input Example {
+ self: Example
+ value: String
+}
+```
+
+This example is also valid as the field `self` may be an empty List.
+
+```graphql example
+input Example {
+ self: [Example!]!
+ value: String
+}
+```
+
+This example of a circularly-referenced input type is invalid as the field
+`self` cannot be provided a finite value.
+
+```graphql counter-example
+input Example {
+ value: String
+ self: Example!
+}
+```
+
+This example is also invalid, as there is a non-null singular circular reference
+via the `First.second` and `Second.first` fields.
+
+```graphql counter-example
+input First {
+ second: Second!
+ value: String
+}
+
+input Second {
+ first: First!
+ value: String
+}
+```
+
**Result Coercion**
An input object is never a valid result. Input Object types cannot be the return
type of an Object or Interface field.
**Input Coercion**
The value for an input object should be an input object literal or an unordered
-map supplied by a variable, otherwise a query error must be thrown. In either
+map supplied by a variable, otherwise a request error must be raised. In either
case, the input object literal or unordered map must not contain any entries
-with names not defined by a field of this input object type, otherwise an error
-must be thrown.
+with names not defined by a field of this input object type, otherwise a
+response error must be raised.
The result of coercion is an unordered map with an entry for each field both
defined by the input object type and for which a value exists. The resulting map
is constructed with the following rules:
* If no value is provided for a defined input object field and that field
definition provides a default value, the default value should be used. If no
default value is provided and the input object field's type is non-null, an
- error should be thrown. Otherwise, if the field is not required, then no entry
+ error should be raised. Otherwise, if the field is not required, then no entry
is added to the coerced unordered map.
* If the value {null} was provided for an input object field, and the field's
type is not a non-null type, an entry in the coerced unordered map is given
the value {null}. In other words, there is a semantic difference between the
explicitly provided value {null} versus having not provided a value.
* If a literal value is provided for an input object field, an entry in the
coerced unordered map is given the result of coercing that value according
to the input coercion rules for the type of that field.
* If a variable is provided for an input object field, the runtime value of that
variable must be used. If the runtime value is {null} and the field type
- is non-null, a field error must be thrown. If no runtime value is provided,
+ is non-null, a field error must be raised. If no runtime value is provided,
the variable definition's default value should be used. If the variable
definition does not provide a default value, the input object field
definition's default value should be used.
Following are examples of input coercion for an input object type with a
`String` field `a` and a required (non-null) `Int!` field `b`:
```graphql example
@@ -1393,17 +1619,17 @@ Literal Value | Variables | Coerced Value
`{ a: "abc", b: 123 }` | `{}` | `{ a: "abc", b: 123 }`
`{ a: null, b: 123 }` | `{}` | `{ a: null, b: 123 }`
`{ b: 123 }` | `{}` | `{ b: 123 }`
`{ a: $var, b: 123 }` | `{ var: null }` | `{ a: null, b: 123 }`
`{ a: $var, b: 123 }` | `{}` | `{ b: 123 }`
`{ b: $var }` | `{ var: 123 }` | `{ b: 123 }`
`$var` | `{ var: { b: 123 } }` | `{ b: 123 }`
`"abc123"` | `{}` | Error: Incorrect value
-`$var` | `{ var: "abc123" } }` | Error: Incorrect value
+`$var` | `{ var: "abc123" }` | Error: Incorrect value
`{ a: "abc", b: "123" }` | `{}` | Error: Incorrect value for field {b}
`{ a: "abc" }` | `{}` | Error: Missing required field {b}
`{ b: $var }` | `{}` | Error: Missing required field {b}.
`$var` | `{ var: { a: "abc" } }` | Error: Missing required field {b}
`{ a: "abc", b: null }` | `{}` | Error: {b} must be non-null.
`{ b: $var }` | `{ var: null }` | Error: {b} must be non-null.
`{ b: 123, c: "xyz" }` | `{}` | Error: Unexpected field {c}
@@ -1412,89 +1638,95 @@ Literal Value | Variables | Coerced Value
1. An Input Object type must define one or more input fields.
2. For each input field of an Input Object type:
1. The input field must have a unique name within that Input Object type;
no two input fields may share the same name.
2. The input field must not have a name which begins with the
characters {"__"} (two underscores).
3. The input field must accept a type where {IsInputType(inputFieldType)}
returns {true}.
+3. If an Input Object references itself either directly or through referenced
+ Input Objects, at least one of the fields in the chain of references must be
+ either a nullable or a List type.
### Input Object Extensions
InputObjectTypeExtension :
- extend input Name Directives[Const]? InputFieldsDefinition
- - extend input Name Directives[Const]
+ - extend input Name Directives[Const] [lookahead != `{`]
Input object type extensions are used to represent an input object type which
has been extended from some original input object type. For example, this might
be used by a GraphQL service which is itself an extension of another GraphQL service.
**Type Validation**
Input object type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be a Input Object type.
-3. All fields of an Input Object type extension must have unique names.
-4. All fields of an Input Object type extension must not already be a field of
+2. All fields of an Input Object type extension must have unique names.
+3. All fields of an Input Object type extension must not already be a field of
the original Input Object.
-5. Any directives provided must not already apply to the original Input Object type.
+4. Any non-repeatable directives provided must not already apply to the
+ original Input Object type.
## List
A GraphQL list is a special collection type which declares the type of each
item in the List (referred to as the *item type* of the list). List values are
serialized as ordered lists, where each item in the list is serialized as per
-the item type. To denote that a field uses a List type the item type is wrapped
-in square brackets like this: `pets: [Pet]`.
+the item type.
+
+To denote that a field uses a List type the item type is wrapped in square brackets
+like this: `pets: [Pet]`. Nesting lists is allowed: `matrix: [[Int]]`.
**Result Coercion**
-GraphQL servers must return an ordered list as the result of a list type. Each
+GraphQL services must return an ordered list as the result of a list type. Each
item in the list must be the result of a result coercion of the item type. If a
reasonable coercion is not possible it must raise a field error. In
particular, if a non-list is returned, the coercion should fail, as this
indicates a mismatch in expectations between the type system and the
implementation.
-If a list's item type is nullable, then errors occuring during preparation or
+If a list's item type is nullable, then errors occurring during preparation or
coercion of an individual item in the list must result in a the value {null} at
-that position in the list along with an error added to the response. If a list's
-item type is non-null, an error occuring at an individual item in the list must
-result in a field error for the entire list.
+that position in the list along with a field error added to the response.
+If a list's item type is non-null, a field error occurring at an individual item
+in the list must result in a field error for the entire list.
-Note: For more information on the error handling process, see "Errors and
-Non-Nullability" within the Execution section.
+Note: See [Handling Field Errors](#sec-Handling-Field-Errors) for more about
+this behavior.
**Input Coercion**
When expected as an input, list values are accepted only when each item in the
list can be accepted by the list's item type.
If the value passed as an input to a list type is *not* a list and not the
{null} value, then the result of input coercion is a list of size one,
where the single item value is the result of input coercion for the list's item
type on the provided value (note this may apply recursively for nested lists).
-This allow inputs which accept one or many arguments (sometimes referred to as
+This allows inputs which accept one or many arguments (sometimes referred to as
"var args") to declare their input type as a list while for the common case of a
single value, a client can just pass that value directly rather than
constructing the list.
Following are examples of input coercion with various list types and values:
Expected Type | Provided Value | Coerced Value
------------- | ---------------- | ---------------------------
`[Int]` | `[1, 2, 3]` | `[1, 2, 3]`
`[Int]` | `[1, "b", true]` | Error: Incorrect item value
`[Int]` | `1` | `[1]`
`[Int]` | `null` | `null`
-`[[Int]]` | `[[1], [2, 3]]` | `[[1], [2, 3]`
+`[[Int]]` | `[[1], [2, 3]]` | `[[1], [2, 3]]`
`[[Int]]` | `[1, 2, 3]` | Error: Incorrect item value
`[[Int]]` | `1` | `[[1]]`
`[[Int]]` | `null` | `null`
## Non-Null
By default, all types in GraphQL are nullable; the {null} value is a valid
@@ -1502,43 +1734,43 @@ response for all of the above types. To declare a type that disallows null,
the GraphQL Non-Null type can be used. This type wraps an underlying type,
and this type acts identically to that wrapped type, with the exception
that {null} is not a valid response for the wrapping type. A trailing
exclamation mark is used to denote a field that uses a Non-Null type like this:
`name: String!`.
**Nullable vs. Optional**
-Fields are *always* optional within the context of a query, a field may be
-omitted and the query is still valid. However fields that return Non-Null types
-will never return the value {null} if queried.
+Fields are *always* optional within the context of a selection set, a field may
+be omitted and the selection set is still valid. However fields that return
+Non-Null types will never return the value {null} if queried.
Inputs (such as field arguments), are always optional by default. However a
non-null input type is required. In addition to not accepting the value {null},
it also does not accept omission. For the sake of simplicity nullable types
are always optional and non-null types are always required.
**Result Coercion**
In all of the above result coercions, {null} was considered a valid value.
To coerce the result of a Non-Null type, the coercion of the wrapped type
should be performed. If that result was not {null}, then the result of coercing
the Non-Null type is that result. If that result was {null}, then a field error
must be raised.
-Note: When a field error is raised on a non-null value, the error propogates to
+Note: When a field error is raised on a non-null value, the error propagates to
the parent field. For more information on this process, see
"Errors and Non-Nullability" within the Execution section.
**Input Coercion**
If an argument or input-object field of a Non-Null type is not provided, is
provided with the literal value {null}, or is provided with a variable that was
either not provided a value at runtime, or was provided the value {null}, then
-a query error must be raised.
+a request error must be raised.
If the value provided to the Non-Null type is provided with a literal value
other than {null}, or a Non-Null variable value, it is coerced using the input
coercion for the wrapped type.
A non-null argument cannot be omitted:
```graphql counter-example
@@ -1601,74 +1833,103 @@ Expected Type | Internal Value | Coerced Result
`[Int!]!` | `[1, 2, 3]` | `[1, 2, 3]`
`[Int!]!` | `null` | Error: Value cannot be null
`[Int!]!` | `[1, 2, null]` | Error: Item cannot be null
`[Int!]!` | `[1, 2, Error]` | Error: Error occurred in item
## Directives
-DirectiveDefinition : Description? directive @ Name ArgumentsDefinition? on DirectiveLocations
+DirectiveDefinition : Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
DirectiveLocations :
- - `|`? DirectiveLocation
- DirectiveLocations | DirectiveLocation
+ - `|`? DirectiveLocation
DirectiveLocation :
- ExecutableDirectiveLocation
- TypeSystemDirectiveLocation
ExecutableDirectiveLocation : one of
- `QUERY`
- `MUTATION`
- `SUBSCRIPTION`
- `FIELD`
- `FRAGMENT_DEFINITION`
- `FRAGMENT_SPREAD`
- `INLINE_FRAGMENT`
+ - `QUERY`
+ - `MUTATION`
+ - `SUBSCRIPTION`
+ - `FIELD`
+ - `FRAGMENT_DEFINITION`
+ - `FRAGMENT_SPREAD`
+ - `INLINE_FRAGMENT`
+ - `VARIABLE_DEFINITION`
TypeSystemDirectiveLocation : one of
- `SCHEMA`
- `SCALAR`
- `OBJECT`
- `FIELD_DEFINITION`
- `ARGUMENT_DEFINITION`
- `INTERFACE`
- `UNION`
- `ENUM`
- `ENUM_VALUE`
- `INPUT_OBJECT`
- `INPUT_FIELD_DEFINITION`
+ - `SCHEMA`
+ - `SCALAR`
+ - `OBJECT`
+ - `FIELD_DEFINITION`
+ - `ARGUMENT_DEFINITION`
+ - `INTERFACE`
+ - `UNION`
+ - `ENUM`
+ - `ENUM_VALUE`
+ - `INPUT_OBJECT`
+ - `INPUT_FIELD_DEFINITION`
A GraphQL schema describes directives which are used to annotate various parts
of a GraphQL document as an indicator that they should be evaluated differently
by a validator, executor, or client tool such as a code generator.
+**Built-in Directives**
+
+:: A *built-in directive* is any directive defined within this specification.
+
GraphQL implementations should provide the `@skip` and `@include` directives.
GraphQL implementations that support the type system definition language must
provide the `@deprecated` directive if representing deprecated portions of
the schema.
+GraphQL implementations that support the type system definition language should
+provide the `@specifiedBy` directive if representing custom scalar
+definitions.
+
+When representing a GraphQL schema using the type system definition language
+any *built-in directive* may be omitted for brevity.
+
+When introspecting a GraphQL service all provided directives, including
+any *built-in directive*, must be included in the set of returned directives.
+
+**Custom Directives**
+
+:: GraphQL services and client tooling may provide any additional
+*custom directive* beyond those defined in this document. Directives are the
+preferred way to extend GraphQL with custom or experimental behavior.
+
+Note: When defining a *custom directive*, it is recommended to prefix the
+directive's name to make its scope of usage clear and to prevent a collision
+with *built-in directive* which may be specified by future versions of this
+document (which will not include `_` in their name). For example, a
+*custom directive* used by Facebook's GraphQL service should be named `@fb_auth`
+instead of `@auth`. This is especially recommended for proposed additions to
+this specification which can change during the [RFC process](https://github.com/graphql/graphql-spec/blob/main/CONTRIBUTING.md).
+For example a work in progress version of `@live` should be named `@rfc_live`.
+
Directives must only be used in the locations they are declared to belong in.
-In this example, a directive is defined which can be used to annotate a
-fragment definition:
+In this example, a directive is defined which can be used to annotate a field:
```graphql example
directive @example on FIELD
fragment SomeFragment on SomeType {
field @example
}
```
Directive locations may be defined with an optional leading `|` character to aid
formatting when representing a longer list of possible locations:
-```graphql example
+```raw graphql example
directive @example on
| FIELD
| FRAGMENT_SPREAD
| INLINE_FRAGMENT
```
Directives can also be used to annotate the type system definition language
as well, which can be a useful tool for supplying additional metadata in order
@@ -1680,22 +1941,41 @@ In this example, the directive `@example` annotates field and argument definitio
```graphql example
directive @example on FIELD_DEFINITION | ARGUMENT_DEFINITION
type SomeType {
field(arg: Int @example): String @example
}
```
+A directive may be defined as repeatable by including the "repeatable" keyword.
+Repeatable directives are often useful when the same directive should be used
+with different arguments at a single location, especially in cases where
+additional information needs to be provided to a type or schema extension via
+a directive:
+
+```graphql example
+directive @delegateField(name: String!) repeatable on OBJECT | INTERFACE
+
+type Book @delegateField(name: "pageCount") @delegateField(name: "author") {
+ id: ID!
+}
+
+extend type Book @delegateField(name: "index")
+```
+
While defining a directive, it must not reference itself directly or indirectly:
```graphql counter-example
directive @invalidExample(arg: String @invalidExample) on ARGUMENT_DEFINITION
```
+Note: The order in which directives appear may be significant, including
+repeatable directives.
+
**Validation**
1. A directive definition must not contain the use of a directive which
references itself directly.
2. A directive definition must not contain the use of a directive which
references itself indirectly by referencing a Type or Directive which
transitively includes a reference to this directive.
3. The directive must not have a name which begins with the characters
@@ -1707,73 +1987,93 @@ directive @invalidExample(arg: String @invalidExample) on ARGUMENT_DEFINITION
returns {true}.
### @skip
```graphql
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
```
-The `@skip` directive may be provided for fields, fragment spreads, and
-inline fragments, and allows for conditional exclusion during execution as
-described by the if argument.
+The `@skip` *built-in directive* may be provided for fields, fragment spreads,
+and inline fragments, and allows for conditional exclusion during execution as
+described by the `if` argument.
In this example `experimentalField` will only be queried if the variable
`$someTest` has the value `false`.
```graphql example
-query myQuery($someTest: Boolean) {
+query myQuery($someTest: Boolean!) {
experimentalField @skip(if: $someTest)
}
```
### @include
```graphql
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
```
-The `@include` directive may be provided for fields, fragment spreads, and
-inline fragments, and allows for conditional inclusion during execution as
-described by the if argument.
+The `@include` *built-in directive* may be provided for fields, fragment
+spreads, and inline fragments, and allows for conditional inclusion during
+execution as described by the `if` argument.
In this example `experimentalField` will only be queried if the variable
`$someTest` has the value `true`
```graphql example
-query myQuery($someTest: Boolean) {
+query myQuery($someTest: Boolean!) {
experimentalField @include(if: $someTest)
}
```
Note: Neither `@skip` nor `@include` has precedence over the other. In the case
-that both the `@skip` and `@include` directives are provided in on the same the
+that both the `@skip` and `@include` directives are provided on the same
field or fragment, it *must* be queried only if the `@skip` condition is false
*and* the `@include` condition is true. Stated conversely, the field or fragment
must *not* be queried if either the `@skip` condition is true *or* the
`@include` condition is false.
### @deprecated
```graphql
directive @deprecated(
reason: String = "No longer supported"
) on FIELD_DEFINITION | ENUM_VALUE
```
-The `@deprecated` directive is used within the type system definition language
-to indicate deprecated portions of a GraphQL service's schema, such as
+The `@deprecated` *built-in directive* is used within the type system definition
+language to indicate deprecated portions of a GraphQL service's schema, such as
deprecated fields on a type or deprecated enum values.
Deprecations include a reason for why it is deprecated, which is formatted using
-Markdown syntax (as specified by [CommonMark](http://commonmark.org/)).
+Markdown syntax (as specified by [CommonMark](https://commonmark.org/)).
In this example type definition, `oldField` is deprecated in favor of
using `newField`.
```graphql example
type ExampleType {
newField: String
oldField: String @deprecated(reason: "Use `newField`.")
}
```
+
+
+### @specifiedBy
+
+```graphql
+directive @specifiedBy(url: String!) on SCALAR
+```
+
+The `@specifiedBy` *built-in directive* is used within the type system
+definition language to provide a *scalar specification URL* for specifying the
+behavior of [custom scalar types](#sec-Scalars.Custom-Scalars). The URL should
+point to a human-readable specification of the data format, serialization, and
+coercion rules. It must not appear on built-in scalar types.
+
+In this example, a custom scalar type for `UUID` is defined with a URL pointing
+to the relevant IETF specification.
+
+```graphql example
+scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
+```
~~~
spec/Section 4 -- Introspection.md
~~~diff
@@ -1,43 +1,43 @@
# Introspection
-A GraphQL server supports introspection over its schema. This schema is queried
+A GraphQL service supports introspection over its schema. This schema is queried
using GraphQL itself, creating a powerful platform for tool-building.
-Take an example query for a trivial app. In this case there is a User type with
+Take an example request for a trivial app. In this case there is a User type with
three fields: id, name, and birthday.
-For example, given a server with the following type definition:
+For example, given a service with the following type definition:
```graphql example
type User {
id: String
name: String
birthday: Date
}
```
-The query
+A request containing the operation:
```graphql example
{
__type(name: "User") {
name
fields {
name
type {
name
}
}
}
}
```
-would return
+would produce the result:
```json example
{
"__type": {
"name": "User",
"fields": [
{
"name": "id",
@@ -45,112 +45,127 @@ would return
},
{
"name": "name",
"type": { "name": "String" }
},
{
"name": "birthday",
"type": { "name": "Date" }
- },
+ }
]
}
}
```
-## Reserved Names
+**Reserved Names**
Types and fields required by the GraphQL introspection system that are used in
the same context as user-defined types and fields are prefixed with {"__"} two
underscores. This in order to avoid naming collisions with user-defined GraphQL
-types. Conversely, GraphQL type system authors must not define any types,
-fields, arguments, or any other type system artifact with two leading
-underscores.
-
-
-## Documentation
-
-All types in the introspection system provide a `description` field of type
-`String` to allow type designers to publish documentation in addition to
-capabilities. A GraphQL server may return the `description` field using Markdown
-syntax (as specified by [CommonMark](http://commonmark.org/)). Therefore it is
-recommended that any tool that displays `description` use a CommonMark-compliant
-Markdown renderer.
-
-
-## Deprecation
-
-To support the management of backwards compatibility, GraphQL fields and enum
-values can indicate whether or not they are deprecated (`isDeprecated: Boolean`)
-and a description of why it is deprecated (`deprecationReason: String`).
-
-Tools built using GraphQL introspection should respect deprecation by
-discouraging deprecated use through information hiding or developer-facing
-warnings.
+types.
+Otherwise, any {Name} within a GraphQL type system must not start with
+two underscores {"__"}.
## Type Name Introspection
-GraphQL supports type name introspection at any point within a query by the
-meta-field `__typename: String!` when querying against any Object, Interface,
-or Union. It returns the name of the object type currently being queried.
+GraphQL supports type name introspection within any selection set in an
+operation, with the single exception of selections at the root of a subscription
+operation. Type name introspection is accomplished via the meta-field
+`__typename: String!` on any Object, Interface, or Union. It returns the name of
+the concrete Object type at that point during execution.
This is most often used when querying against Interface or Union types to
-identify which actual type of the possible types has been returned.
+identify which actual Object type of the possible types has been returned.
-This field is implicit and does not appear in the fields list in any defined type.
+As a meta-field, `__typename` is implicit and does not appear in the fields list
+in any defined type.
+Note: `__typename` may not be included as a root field in a subscription
+operation.
## Schema Introspection
The schema introspection system is accessible from the meta-fields `__schema`
and `__type` which are accessible from the type of the root of a query
operation.
```graphql
__schema: __Schema!
__type(name: String!): __Type
```
-These fields are implicit and do not appear in the fields list in the root type
-of the query operation.
+Like all meta-fields, these are implicit and do not appear in the fields list in
+the root type of the query operation.
-The schema of the GraphQL schema introspection system:
+**First Class Documentation**
+
+All types in the introspection system provide a `description` field of type
+`String` to allow type designers to publish documentation in addition to
+capabilities. A GraphQL service may return the `description` field using Markdown
+syntax (as specified by [CommonMark](https://commonmark.org/)). Therefore it is
+recommended that any tool that displays `description` use a CommonMark-compliant
+Markdown renderer.
+
+**Deprecation**
+
+To support the management of backwards compatibility, GraphQL fields and enum
+values can indicate whether or not they are deprecated (`isDeprecated: Boolean`)
+and a description of why it is deprecated (`deprecationReason: String`).
+
+Tools built using GraphQL introspection should respect deprecation by
+discouraging deprecated use through information hiding or developer-facing
+warnings.
+
+**Schema Introspection Schema**
+
+The schema introspection system is itself represented as a GraphQL schema. Below
+are the full set of type system definitions providing schema introspection,
+which are fully defined in the sections below.
```graphql
type __Schema {
+ description: String
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}
type __Type {
kind: __TypeKind!
name: String
description: String
-
- # OBJECT and INTERFACE only
+ # must be non-null for OBJECT and INTERFACE, otherwise null.
fields(includeDeprecated: Boolean = false): [__Field!]
-
- # OBJECT only
+ # must be non-null for OBJECT and INTERFACE, otherwise null.
interfaces: [__Type!]
-
- # INTERFACE and UNION only
+ # must be non-null for INTERFACE and UNION, otherwise null.
possibleTypes: [__Type!]
-
- # ENUM only
+ # must be non-null for ENUM, otherwise null.
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
-
- # INPUT_OBJECT only
+ # must be non-null for INPUT_OBJECT, otherwise null.
inputFields: [__InputValue!]
-
- # NON_NULL and LIST only
+ # must be non-null for NON_NULL and LIST, otherwise null.
ofType: __Type
+ # may be non-null for custom SCALAR, otherwise null.
+ specifiedByURL: String
+}
+
+enum __TypeKind {
+ SCALAR
+ OBJECT
+ INTERFACE
+ UNION
+ ENUM
+ INPUT_OBJECT
+ LIST
+ NON_NULL
}
type __Field {
name: String!
description: String
args: [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
@@ -166,211 +181,245 @@ type __InputValue {
type __EnumValue {
name: String!
description: String
isDeprecated: Boolean!
deprecationReason: String
}
-enum __TypeKind {
- SCALAR
- OBJECT
- INTERFACE
- UNION
- ENUM
- INPUT_OBJECT
- LIST
- NON_NULL
-}
-
type __Directive {
name: String!
description: String
locations: [__DirectiveLocation!]!
args: [__InputValue!]!
+ isRepeatable: Boolean!
}
enum __DirectiveLocation {
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
+ VARIABLE_DEFINITION
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
}
```
+### The __Schema Type
-### The __Type Type
+The `__Schema` type is returned from the `__schema` meta-field and provides
+all information about the schema of a GraphQL service.
-`__Type` is at the core of the type introspection system.
-It represents scalars, interfaces, object types, unions, enums in the system.
+Fields\:
-`__Type` also represents type modifiers, which are used to modify a type
-that it refers to (`ofType: __Type`). This is how we represent lists,
-non-nullable types, and the combinations thereof.
+* `description` may return a String or {null}.
+* `queryType` is the root type of a query operation.
+* `mutationType` is the root type of a mutation operation, if supported.
+ Otherwise {null}.
+* `subscriptionType` is the root type of a subscription operation, if supported.
+ Otherwise {null}.
+* `types` must return the set of all named types contained within this schema.
+ Any named type which can be found through a field of any introspection type
+ must be included in this set.
+* `directives` must return the set of all directives available within
+ this schema including all built-in directives.
-### Type Kinds
+### The __Type Type
+
+`__Type` is at the core of the type introspection system, it represents all
+types in the system: both named types (e.g. Scalars and Object types) and
+type modifiers (e.g. List and Non-Null types).
+
+Type modifiers are used to modify the type presented in the field `ofType`.
+This modified type may recursively be a modified type, representing lists,
+non-nullables, and combinations thereof, ultimately modifying a named type.
There are several different kinds of type. In each kind, different fields are
-actually valid. These kinds are listed in the `__TypeKind` enumeration.
+actually valid. All possible kinds are listed in the `__TypeKind` enum.
+
+Each sub-section below defines the expected fields of `__Type` given each
+possible value of the `__TypeKind` enum:
+* {"SCALAR"}
+* {"OBJECT"}
+* {"INTERFACE"}
+* {"UNION"}
+* {"ENUM"}
+* {"INPUT_OBJECT"}
+* {"LIST"}
+* {"NON_NULL"}
-#### Scalar
+**Scalar**
Represents scalar types such as Int, String, and Boolean. Scalars cannot have fields.
-A GraphQL type designer should describe the data format and scalar coercion
-rules in the description field of any scalar.
+Also represents [Custom scalars](#sec-Scalars.Custom-Scalars) which may provide
+`specifiedByURL` as a *scalar specification URL*.
-Fields
+Fields\:
* `kind` must return `__TypeKind.SCALAR`.
* `name` must return a String.
* `description` may return a String or {null}.
+* `specifiedByURL` may return a String (in the form of a URL) for custom
+ scalars, otherwise must be {null}.
* All other fields must return {null}.
-#### Object
+**Object**
Object types represent concrete instantiations of sets of fields. The
introspection types (e.g. `__Type`, `__Field`, etc) are examples of objects.
-Fields
+Fields\:
* `kind` must return `__TypeKind.OBJECT`.
* `name` must return a String.
* `description` may return a String or {null}.
-* `fields`: The set of fields query-able on this type.
+* `fields` must return the set of fields that can be selected for this type.
* Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated fields are also returned.
-* `interfaces`: The set of interfaces that an object implements.
+* `interfaces` must return the set of interfaces that an object implements
+ (if none, `interfaces` must return the empty set).
* All other fields must return {null}.
-#### Union
+**Union**
Unions are an abstract type where no common fields are declared. The possible
types of a union are explicitly listed out in `possibleTypes`. Types can be
made parts of unions without modification of that type.
-Fields
+Fields\:
* `kind` must return `__TypeKind.UNION`.
* `name` must return a String.
* `description` may return a String or {null}.
* `possibleTypes` returns the list of types that can be represented within this
union. They must be object types.
* All other fields must return {null}.
-#### Interface
+**Interface**
Interfaces are an abstract type where there are common fields declared. Any type
that implements an interface must define all the fields with names and types
exactly matching. The implementations of this interface are explicitly listed
out in `possibleTypes`.
-Fields
+Fields\:
* `kind` must return `__TypeKind.INTERFACE`.
* `name` must return a String.
* `description` may return a String or {null}.
-* `fields`: The set of fields required by this interface.
+* `fields` must return the set of fields required by this interface.
* Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated fields are also returned.
+* `interfaces` must return the set of interfaces that an object implements
+ (if none, `interfaces` must return the empty set).
* `possibleTypes` returns the list of types that implement this interface.
They must be object types.
* All other fields must return {null}.
-#### Enum
+**Enum**
Enums are special scalars that can only have a defined set of values.
-Fields
+Fields\:
* `kind` must return `__TypeKind.ENUM`.
* `name` must return a String.
* `description` may return a String or {null}.
-* `enumValues`: The list of `EnumValue`. There must be at least one and they
- must have unique names.
+* `enumValues` must return the set of enum values as a list of `__EnumValue`.
+ There must be at least one and they must have unique names.
* Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated enum values are also returned.
* All other fields must return {null}.
-#### Input Object
+**Input Object**
-Input objects are composite types used as inputs into queries defined as a list
-of named input values.
+Input objects are composite types defined as a list of named input values. They
+are only used as inputs to arguments and variables and cannot be a field
+return type.
For example the input object `Point` could be defined as:
```graphql example
input Point {
x: Int
y: Int
}
```
-Fields
+Fields\:
* `kind` must return `__TypeKind.INPUT_OBJECT`.
* `name` must return a String.
* `description` may return a String or {null}.
-* `inputFields`: a list of `InputValue`.
+* `inputFields` must return the set of input fields as a list of `__InputValue`.
* All other fields must return {null}.
-#### List
+**List**
Lists represent sequences of values in GraphQL. A List type is a type modifier:
it wraps another type instance in the `ofType` field, which defines the type of
each item in the list.
-Fields
+The modified type in the `ofType` field may itself be a modified type, allowing
+the representation of Lists of Lists, or Lists of Non-Nulls.
+
+Fields\:
* `kind` must return `__TypeKind.LIST`.
-* `ofType`: Any type.
+* `ofType` must return a type of any kind.
* All other fields must return {null}.
-#### Non-Null
+**Non-Null**
GraphQL types are nullable. The value {null} is a valid response for field type.
-A Non-null type is a type modifier: it wraps another type instance in the
+A Non-Null type is a type modifier: it wraps another type instance in the
`ofType` field. Non-null types do not allow {null} as a response, and indicate
required inputs for arguments and input object fields.
+The modified type in the `ofType` field may itself be a modified List type,
+allowing the representation of Non-Null of Lists. However it must not be a
+modified Non-Null type to avoid a redundant Non-Null of Non-Null.
+
+Fields\:
+
* `kind` must return `__TypeKind.NON_NULL`.
-* `ofType`: Any type except Non-null.
+* `ofType` must return a type of any kind except Non-Null.
* All other fields must return {null}.
### The __Field Type
The `__Field` type represents each field in an Object or Interface type.
-Fields
+Fields\:
* `name` must return a String
* `description` may return a String or {null}
* `args` returns a List of `__InputValue` representing the arguments this
field accepts.
* `type` must return a `__Type` that represents the type of value returned by
this field.
* `isDeprecated` returns {true} if this field should no longer be used,
@@ -378,42 +427,69 @@ Fields
* `deprecationReason` optionally provides a reason why this field is deprecated.
### The __InputValue Type
The `__InputValue` type represents field and directive arguments as well as the
`inputFields` of an input object.
-Fields
+Fields\:
* `name` must return a String
* `description` may return a String or {null}
* `type` must return a `__Type` that represents the type this input
value expects.
* `defaultValue` may return a String encoding (using the GraphQL language) of the
default value used by this input value in the condition a value is not
provided at runtime. If this input value has no default value, returns {null}.
### The __EnumValue Type
The `__EnumValue` type represents one of possible values of an enum.
-Fields
+Fields\:
* `name` must return a String
* `description` may return a String or {null}
-* `isDeprecated` returns {true} if this field should no longer be used,
+* `isDeprecated` returns {true} if this enum value should no longer be used,
otherwise {false}.
-* `deprecationReason` optionally provides a reason why this field is deprecated.
+* `deprecationReason` optionally provides a reason why this enum value is deprecated.
### The __Directive Type
-The `__Directive` type represents a Directive that a server supports.
-
-Fields
+The `__Directive` type represents a directive that a service supports.
+
+This includes both any *built-in directive* and any *custom directive*.
+
+Individual directives may only be used in locations that are explicitly
+supported. All possible locations are listed in the `__DirectiveLocation` enum:
+
+* {"QUERY"}
+* {"MUTATION"}
+* {"SUBSCRIPTION"}
+* {"FIELD"}
+* {"FRAGMENT_DEFINITION"}
+* {"FRAGMENT_SPREAD"}
+* {"INLINE_FRAGMENT"}
+* {"VARIABLE_DEFINITION"}
+* {"SCHEMA"}
+* {"SCALAR"}
+* {"OBJECT"}
+* {"FIELD_DEFINITION"}
+* {"ARGUMENT_DEFINITION"}
+* {"INTERFACE"}
+* {"UNION"}
+* {"ENUM"}
+* {"ENUM_VALUE"}
+* {"INPUT_OBJECT"}
+* {"INPUT_FIELD_DEFINITION"}
+
+Fields\:
* `name` must return a String
* `description` may return a String or {null}
* `locations` returns a List of `__DirectiveLocation` representing the valid
locations this directive may be placed.
* `args` returns a List of `__InputValue` representing the arguments this
directive accepts.
+* `isRepeatable` must return a Boolean that indicates if the directive may be
+ used repeatedly at a single location.
~~~
spec/Section 5 -- Validation.md
~~~diff
@@ -16,17 +16,17 @@ validated before. For example: the request may be validated during development,
provided it does not later change, or a service may validate a request once and
memoize the result to avoid validating the same request again in the future.
Any client-side or development-time tool should report validation errors and not
allow the formulation or execution of requests known to be invalid at that given
point in time.
**Type system evolution**
-As GraphQL type system schema evolve over time by adding new types and new
+As GraphQL type system schema evolves over time by adding new types and new
fields, it is possible that a request which was previously valid could later
become invalid. Any change that can cause a previously valid request to become
invalid is considered a *breaking change*. GraphQL services and schema
maintainers are encouraged to avoid breaking changes, however in order to be
more resilient to these breaking changes, sophisticated GraphQL systems may
still allow for the execution of requests which *at some point* were known to
be free of any validation errors, and have not changed since.
@@ -35,24 +35,28 @@ be free of any validation errors, and have not changed since.
For this section of this schema, we will assume the following type system
in order to demonstrate examples:
```graphql example
type Query {
dog: Dog
}
-enum DogCommand { SIT, DOWN, HEEL }
+enum DogCommand {
+ SIT
+ DOWN
+ HEEL
+}
type Dog implements Pet {
name: String!
nickname: String
barkVolume: Int
doesKnowCommand(dogCommand: DogCommand!): Boolean!
- isHousetrained(atOtherHomes: Boolean): Boolean!
+ isHouseTrained(atOtherHomes: Boolean): Boolean!
owner: Human
}
interface Sentient {
name: String!
}
interface Pet {
@@ -61,19 +65,22 @@ interface Pet {
type Alien implements Sentient {
name: String!
homePlanet: String
}
type Human implements Sentient {
name: String!
+ pets: [Pet!]
}
-enum CatCommand { JUMP }
+enum CatCommand {
+ JUMP
+}
type Cat implements Pet {
name: String!
nickname: String
doesKnowCommand(catCommand: CatCommand!): Boolean!
meowVolume: Int
}
@@ -84,31 +91,31 @@ union HumanOrAlien = Human | Alien
## Documents
### Executable Definitions
**Formal Specification**
- * For each definition {definition} in the document.
- * {definition} must be {OperationDefinition} or {FragmentDefinition} (it must
- not be {TypeSystemDefinition}).
+* For each definition {definition} in the document.
+* {definition} must be {ExecutableDefinition} (it must not be
+ {TypeSystemDefinitionOrExtension}).
**Explanatory Text**
GraphQL execution will only consider the executable definitions Operation and
Fragment. Type system definitions and extensions are not executable, and are not
considered during execution.
-To avoid ambiguity, a document containing {TypeSystemDefinition} is invalid
-for execution.
+To avoid ambiguity, a document containing {TypeSystemDefinitionOrExtension} is
+invalid for execution.
GraphQL documents not intended to be directly executed may include
-{TypeSystemDefinition}.
+{TypeSystemDefinitionOrExtension}.
For example, the following document is invalid for execution since the original
executing schema may not know about the provided type extension:
```graphql counter-example
query getDogName {
dog {
name
@@ -124,21 +131,21 @@ extend type Dog {
## Operations
### Named Operation Definitions
#### Operation Name Uniqueness
**Formal Specification**
- * For each operation definition {operation} in the document.
- * Let {operationName} be the name of {operation}.
- * If {operationName} exists
- * Let {operations} be all operation definitions in the document named {operationName}.
- * {operations} must be a set of one.
+* For each operation definition {operation} in the document.
+* Let {operationName} be the name of {operation}.
+* If {operationName} exists
+ * Let {operations} be all operation definitions in the document named {operationName}.
+ * {operations} must be a set of one.
**Explanatory Text**
Each named operation definition must be unique within a document when referred
to by its name.
For example the following document is valid:
@@ -193,20 +200,20 @@ mutation dogOperation {
```
### Anonymous Operation Definitions
#### Lone Anonymous Operation
**Formal Specification**
- * Let {operations} be all operation definitions in the document.
- * Let {anonymous} be all anonymous operation definitions in the document.
- * If {operations} is a set of more than 1:
- * {anonymous} must be empty.
+* Let {operations} be all operation definitions in the document.
+* Let {anonymous} be all anonymous operation definitions in the document.
+* If {operations} is a set of more than 1:
+ * {anonymous} must be empty.
**Explanatory Text**
GraphQL allows a short-hand form for defining query operations when only that
one operation exists in the document.
For example the following document is valid:
@@ -237,23 +244,24 @@ query getName {
```
### Subscription Operation Definitions
#### Single root field
**Formal Specification**
- * For each subscription operation definition {subscription} in the document
- * Let {subscriptionType} be the root Subscription type in {schema}.
- * Let {selectionSet} be the top level selection set on {subscription}.
- * Let {variableValues} be the empty set.
- * Let {groupedFieldSet} be the result of
- {CollectFields(subscriptionType, selectionSet, variableValues)}.
- * {groupedFieldSet} must have exactly one entry.
+* For each subscription operation definition {subscription} in the document
+* Let {subscriptionType} be the root Subscription type in {schema}.
+* Let {selectionSet} be the top level selection set on {subscription}.
+* Let {variableValues} be the empty set.
+* Let {groupedFieldSet} be the result of
+ {CollectFields(subscriptionType, selectionSet, variableValues)}.
+* {groupedFieldSet} must have exactly one entry, which must not be an
+ introspection field.
**Explanatory Text**
Subscription operations must have exactly one root field.
Valid examples:
```graphql example
@@ -299,42 +307,41 @@ fragment multipleSubscriptions on Subscription {
newMessage {
body
sender
}
disallowedSecondRootField
}
```
-Introspection fields are counted. The following example is also invalid:
+The root field of a subscription operation must not be an introspection field.
+The following example is also invalid:
```graphql counter-example
subscription sub {
- newMessage {
- body
- sender
- }
__typename
}
```
Note: While each subscription must have exactly one root field, a document may
contain any number of operations, each of which may contain different root
fields. When executed, a document containing multiple subscription operations
must provide the operation name as described in {GetOperation()}.
## Fields
-### Field Selections on Objects, Interfaces, and Unions Types
+### Field Selections
+
+Field selections must exist on Object, Interface, and Union types.
**Formal Specification**
- * For each {selection} in the document.
- * Let {fieldName} be the target field of {selection}
- * {fieldName} must be defined on type in scope
+* For each {selection} in the document.
+* Let {fieldName} be the target field of {selection}
+* {fieldName} must be defined on type in scope
**Explanatory Text**
The target field of a field selection must be defined on the scoped type of the
selection set. There are no limitations on alias names.
For example the following fragment would not pass validation:
@@ -396,48 +403,50 @@ fragment directFieldSelectionOnUnion on CatOrDog {
}
```
### Field Selection Merging
**Formal Specification**
- * Let {set} be any selection set defined in the GraphQL document.
- * {FieldsInSetCanMerge(set)} must be true.
+* Let {set} be any selection set defined in the GraphQL document.
+* {FieldsInSetCanMerge(set)} must be true.
+
+FieldsInSetCanMerge(set):
-FieldsInSetCanMerge(set) :
* Let {fieldsForName} be the set of selections with a given response name in
{set} including visiting fragments and inline fragments.
* Given each pair of members {fieldA} and {fieldB} in {fieldsForName}:
* {SameResponseShape(fieldA, fieldB)} must be true.
* If the parent types of {fieldA} and {fieldB} are equal or if either is not
an Object Type:
* {fieldA} and {fieldB} must have identical field names.
* {fieldA} and {fieldB} must have identical sets of arguments.
* Let {mergedSet} be the result of adding the selection set of {fieldA}
and the selection set of {fieldB}.
* {FieldsInSetCanMerge(mergedSet)} must be true.
-SameResponseShape(fieldA, fieldB) :
+SameResponseShape(fieldA, fieldB):
+
* Let {typeA} be the return type of {fieldA}.
* Let {typeB} be the return type of {fieldB}.
* If {typeA} or {typeB} is Non-Null.
* If {typeA} or {typeB} is nullable, return false.
* Let {typeA} be the nullable type of {typeA}
* Let {typeB} be the nullable type of {typeB}
* If {typeA} or {typeB} is List.
* If {typeA} or {typeB} is not List, return false.
* Let {typeA} be the item type of {typeA}
* Let {typeB} be the item type of {typeB}
* Repeat from step 3.
* If {typeA} or {typeB} is Scalar or Enum.
* If {typeA} and {typeB} are the same type return true, otherwise return
false.
- * If {typeA} or {typeB} is not a composite type, return false.
+ * Assert: {typeA} and {typeB} are both composite types.
* Let {mergedSet} be the result of adding the selection set of {fieldA} and
the selection set of {fieldB}.
* Let {fieldsForName} be the set of selections with a given response name in
{mergedSet} including visiting fragments and inline fragments.
* Given each pair of members {subfieldA} and {subfieldB} in {fieldsForName}:
* If {SameResponseShape(subfieldA, subfieldB)} is false, return false.
* Return true.
@@ -556,27 +565,27 @@ fragment conflictingDifferingResponses on Pet {
}
```
### Leaf Field Selections
**Formal Specification**
- * For each {selection} in the document
- * Let {selectionType} be the result type of {selection}
- * If {selectionType} is a scalar or enum:
- * The subselection set of that selection must be empty
- * If {selectionType} is an interface, union, or object
- * The subselection set of that selection must NOT BE empty
+* For each {selection} in the document
+* Let {selectionType} be the result type of {selection}
+* If {selectionType} is a scalar or enum:
+ * The subselection set of that selection must be empty
+* If {selectionType} is an interface, union, or object
+ * The subselection set of that selection must NOT BE empty
**Explanatory Text**
Field selections on scalars or enums are never allowed, because they
-are the leaf nodes of any GraphQL query.
+are the leaf nodes of any GraphQL operation.
The following is valid.
```graphql example
fragment scalarSelection on Dog {
barkVolume
}
```
@@ -586,21 +595,22 @@ The following is invalid.
```graphql counter-example
fragment scalarSelectionsNotAllowedOnInt on Dog {
barkVolume {
sinceWhen
}
}
```
-Conversely the leaf field selections of GraphQL queries
+Conversely the leaf field selections of GraphQL operations
must be of type scalar or enum. Leaf selections on objects, interfaces,
and unions without subfields are disallowed.
-Let's assume the following additions to the query root type of the schema:
+Let's assume the following additions to the query root operation type of
+the schema:
```graphql example
extend type Query {
human: Human
pet: Pet
catOrDog: CatOrDog
}
```
@@ -627,98 +637,98 @@ query directQueryOnUnionWithoutSubFields {
Arguments are provided to both fields and directives. The following validation
rules apply in both cases.
### Argument Names
**Formal Specification**
- * For each {argument} in the document
- * Let {argumentName} be the Name of {argument}.
- * Let {argumentDefinition} be the argument definition provided by the parent field or definition named {argumentName}.
- * {argumentDefinition} must exist.
+* For each {argument} in the document
+* Let {argumentName} be the Name of {argument}.
+* Let {argumentDefinition} be the argument definition provided by the parent field or definition named {argumentName}.
+* {argumentDefinition} must exist.
**Explanatory Text**
Every argument provided to a field or directive must be defined in the set of
possible arguments of that field or directive.
For example the following are valid:
```graphql example
fragment argOnRequiredArg on Dog {
doesKnowCommand(dogCommand: SIT)
}
fragment argOnOptional on Dog {
- isHousetrained(atOtherHomes: true) @include(if: true)
+ isHouseTrained(atOtherHomes: true) @include(if: true)
}
```
the following is invalid since `command` is not defined on `DogCommand`.
```graphql counter-example
fragment invalidArgName on Dog {
doesKnowCommand(command: CLEAN_UP_HOUSE)
}
```
and this is also invalid as `unless` is not defined on `@include`.
```graphql counter-example
fragment invalidArgName on Dog {
- isHousetrained(atOtherHomes: true) @include(unless: false)
+ isHouseTrained(atOtherHomes: true) @include(unless: false)
}
```
In order to explore more complicated argument examples, let's add the following
to our type system:
```graphql example
type Arguments {
- multipleReqs(x: Int!, y: Int!): Int!
+ multipleRequirements(x: Int!, y: Int!): Int!
booleanArgField(booleanArg: Boolean): Boolean
floatArgField(floatArg: Float): Float
intArgField(intArg: Int): Int
nonNullBooleanArgField(nonNullBooleanArg: Boolean!): Boolean!
booleanListArgField(booleanListArg: [Boolean]!): [Boolean]
optionalNonNullBooleanArgField(optionalBooleanArg: Boolean! = false): Boolean!
}
extend type Query {
arguments: Arguments
}
```
-Order does not matter in arguments. Therefore both the following example are valid.
+Order does not matter in arguments. Therefore both the following examples are valid.
```graphql example
fragment multipleArgs on Arguments {
- multipleReqs(x: 1, y: 2)
+ multipleRequirements(x: 1, y: 2)
}
fragment multipleArgsReverseOrder on Arguments {
- multipleReqs(y: 1, x: 2)
+ multipleRequirements(y: 2, x: 1)
}
```
### Argument Uniqueness
Fields and directives treat arguments as a mapping of argument name to value.
More than one argument with the same name in an argument set is ambiguous
and invalid.
**Formal Specification**
- * For each {argument} in the Document.
- * Let {argumentName} be the Name of {argument}.
- * Let {arguments} be all Arguments named {argumentName} in the Argument Set which contains {argument}.
- * {arguments} must be the set containing only {argument}.
+* For each {argument} in the Document.
+* Let {argumentName} be the Name of {argument}.
+* Let {arguments} be all Arguments named {argumentName} in the Argument Set which contains {argument}.
+* {arguments} must be the set containing only {argument}.
#### Required Arguments
* For each Field or Directive in the document.
* Let {arguments} be the arguments provided by the Field or Directive.
* Let {argumentDefinitions} be the set of argument definitions of that Field or Directive.
* For each {argumentDefinition} in {argumentDefinitions}:
@@ -745,17 +755,17 @@ fragment goodBooleanArg on Arguments {
fragment goodNonNullArg on Arguments {
nonNullBooleanArgField(nonNullBooleanArg: true)
}
```
The argument can be omitted from a field with a nullable argument.
-Therefore the following query is valid:
+Therefore the following fragment is valid:
```graphql example
fragment goodBooleanArgDefault on Arguments {
booleanArgField
}
```
but this is not valid on a required argument.
@@ -778,20 +788,20 @@ fragment missingRequiredArg on Arguments {
## Fragments
### Fragment Declarations
#### Fragment Name Uniqueness
**Formal Specification**
- * For each fragment definition {fragment} in the document
- * Let {fragmentName} be the name of {fragment}.
- * Let {fragments} be all fragment definitions in the document named {fragmentName}.
- * {fragments} must be a set of one.
+* For each fragment definition {fragment} in the document
+* Let {fragmentName} be the name of {fragment}.
+* Let {fragments} be all fragment definitions in the document named {fragmentName}.
+* {fragments} must be a set of one.
**Explanatory Text**
Fragment definitions are referenced in fragment spreads by name. To avoid
ambiguity, each fragment's name must be unique within a document.
Inline fragments are not considered fragment definitions, and are unaffected by this
validation rule.
@@ -837,25 +847,25 @@ fragment fragmentOne on Dog {
}
```
#### Fragment Spread Type Existence
**Formal Specification**
- * For each named spread {namedSpread} in the document
- * Let {fragment} be the target of {namedSpread}
- * The target type of {fragment} must be defined in the schema
+* For each named spread {namedSpread} in the document
+* Let {fragment} be the target of {namedSpread}
+* The target type of {fragment} must be defined in the schema
**Explanatory Text**
Fragments must be specified on types that exist in the schema. This
applies for both named and inline fragments. If they are
-not defined in the schema, the query does not validate.
+not defined in the schema, the fragment is invalid.
For example the following fragments are valid:
```graphql example
fragment correctType on Dog {
name
}
@@ -886,19 +896,19 @@ fragment inlineNotExistingType on Dog {
}
```
#### Fragments On Composite Types
**Formal Specification**
- * For each {fragment} defined in the document.
- * The target type of fragment must have kind {UNION}, {INTERFACE}, or
- {OBJECT}.
+* For each {fragment} defined in the document.
+* The target type of fragment must have kind {UNION}, {INTERFACE}, or
+ {OBJECT}.
**Explanatory Text**
Fragments can only be declared on unions, interfaces, and objects. They are
invalid on scalars. They can only be applied on non-leaf fields. This rule
applies to both inline and named fragments.
The following fragment declarations are valid:
@@ -933,53 +943,53 @@ fragment inlineFragOnScalar on Dog {
}
```
#### Fragments Must Be Used
**Formal Specification**
- * For each {fragment} defined in the document.
- * {fragment} must be the target of at least one spread in the document
+* For each {fragment} defined in the document.
+* {fragment} must be the target of at least one spread in the document
**Explanatory Text**
Defined fragments must be used within a document.
For example the following is an invalid document:
-```graphql counter-example
+```raw graphql counter-example
fragment nameFragment on Dog { # unused
name
}
{
dog {
name
}
}
```
### Fragment Spreads
Field selection is also determined by spreading fragments into one
-another. The selection set of the target fragment is unioned with
+another. The selection set of the target fragment is combined into
the selection set at the level at which the target fragment is
referenced.
#### Fragment spread target defined
**Formal Specification**
- * For every {namedSpread} in the document.
- * Let {fragment} be the target of {namedSpread}
- * {fragment} must be defined in the document
+* For every {namedSpread} in the document.
+* Let {fragment} be the target of {namedSpread}
+* {fragment} must be defined in the document
**Explanatory Text**
Named fragment spreads must refer to fragments defined within the
document. It is a validation error if the target of a spread is
not defined.
```graphql counter-example
@@ -990,27 +1000,28 @@ not defined.
}
```
#### Fragment spreads must not form cycles
**Formal Specification**
- * For each {fragmentDefinition} in the document
- * Let {visited} be the empty set.
- * {DetectCycles(fragmentDefinition, visited)}
+* For each {fragmentDefinition} in the document
+* Let {visited} be the empty set.
+* {DetectFragmentCycles(fragmentDefinition, visited)}
+
+DetectFragmentCycles(fragmentDefinition, visited):
-{DetectCycles(fragmentDefinition, visited)} :
* Let {spreads} be all fragment spread descendants of {fragmentDefinition}
* For each {spread} in {spreads}
* {visited} must not contain {spread}
* Let {nextVisited} be the set including {spread} and members of {visited}
* Let {nextFragmentDefinition} be the target of {spread}
- * {DetectCycles(nextFragmentDefinition, nextVisited)}
+ * {DetectFragmentCycles(nextFragmentDefinition, nextVisited)}
**Explanatory Text**
The graph of fragment spreads must not form any cycles including spreading itself.
Otherwise an operation could infinitely spread or infinitely execute on cycles
in the underlying data.
This invalidates fragments that would result in an infinite spread:
@@ -1062,38 +1073,39 @@ executed against cyclic data:
fragment dogFragment on Dog {
name
owner {
...ownerFragment
}
}
-fragment ownerFragment on Dog {
+fragment ownerFragment on Human {
name
pets {
...dogFragment
}
}
```
#### Fragment spread is possible
**Formal Specification**
- * For each {spread} (named or inline) defined in the document.
- * Let {fragment} be the target of {spread}
- * Let {fragmentType} be the type condition of {fragment}
- * Let {parentType} be the type of the selection set containing {spread}
- * Let {applicableTypes} be the intersection of
- {GetPossibleTypes(fragmentType)} and {GetPossibleTypes(parentType)}
- * {applicableTypes} must not be empty.
+* For each {spread} (named or inline) defined in the document.
+* Let {fragment} be the target of {spread}
+* Let {fragmentType} be the type condition of {fragment}
+* Let {parentType} be the type of the selection set containing {spread}
+* Let {applicableTypes} be the intersection of
+ {GetPossibleTypes(fragmentType)} and {GetPossibleTypes(parentType)}
+* {applicableTypes} must not be empty.
+
+GetPossibleTypes(type):
-GetPossibleTypes(type) :
* If {type} is an object type, return a set containing {type}
* If {type} is an interface type, return the set of types implementing {type}
* If {type} is a union type, return the set of possible types of {type}
**Explanatory Text**
Fragments are declared on a type and will only apply when the
runtime object type matches the type condition. They also are
@@ -1233,17 +1245,17 @@ fragment unionWithInterface on Pet {
fragment dogOrHumanFragment on DogOrHuman {
... on Dog {
barkVolume
}
}
```
-is consider valid because {Dog} implements interface {Pet} and is a
+is considered valid because {Dog} implements interface {Pet} and is a
member of {DogOrHuman}.
However
```graphql counter-example
fragment nonIntersectingInterfaces on Pet {
...sentientFragment
}
@@ -1252,33 +1264,61 @@ fragment sentientFragment on Sentient {
name
}
```
is not valid because there exists no type that implements both {Pet}
and {Sentient}.
+**Interface Spreads in implemented Interface Scope**
+
+Additionally, an interface type fragment can always be spread into an
+interface scope which it implements.
+
+In the example below, the `...resourceFragment` fragments spreads is valid,
+since `Resource` implements `Node`.
+
+```raw graphql example
+interface Node {
+ id: ID!
+}
+
+interface Resource implements Node {
+ id: ID!
+ url: String
+}
+
+fragment interfaceWithInterface on Node {
+ ...resourceFragment
+}
+
+fragment resourceFragment on Resource {
+ url
+}
+```
+
+
## Values
### Values of Correct Type
-**Format Specification**
+**Formal Specification**
- * For each input Value {value} in the document.
- * Let {type} be the type expected in the position {value} is found.
- * {value} must be coercible to {type}.
+* For each input Value {value} in the document.
+ * Let {type} be the type expected in the position {value} is found.
+ * {value} must be coercible to {type}.
**Explanatory Text**
Literal values must be compatible with the type expected in the position they
are found as per the coercion rules defined in the Type System chapter.
-The type expected in a position include the type defined by the argument a value
+The type expected in a position includes the type defined by the argument a value
is provided for, the type defined by an input object field a value is provided
for, and the type of a variable definition a default value is provided for.
The following examples are valid use of value literals:
```graphql example
fragment goodBooleanArg on Arguments {
booleanArgField(booleanArg: true)
@@ -1307,21 +1347,21 @@ query badComplexValue {
}
```
### Input Object Field Names
**Formal Specification**
- * For each Input Object Field {inputField} in the document
- * Let {inputFieldName} be the Name of {inputField}.
- * Let {inputFieldDefinition} be the input field definition provided by the
- parent input object type named {inputFieldName}.
- * {inputFieldDefinition} must exist.
+* For each Input Object Field {inputField} in the document
+* Let {inputFieldName} be the Name of {inputField}.
+* Let {inputFieldDefinition} be the input field definition provided by the
+ parent input object type named {inputFieldName}.
+* {inputFieldDefinition} must exist.
**Explanatory Text**
Every input field provided in an input object value must be defined in the set
of possible fields of that input object's expected type.
For example the following example input object is valid:
@@ -1340,138 +1380,140 @@ which is not defined on the expected type:
}
```
### Input Object Field Uniqueness
**Formal Specification**
- * For each input object value {inputObject} in the document.
- * For every {inputField} in {inputObject}
- * Let {name} be the Name of {inputField}.
- * Let {fields} be all Input Object Fields named {name} in {inputObject}.
- * {fields} must be the set containing only {inputField}.
+* For each input object value {inputObject} in the document.
+* For every {inputField} in {inputObject}
+ * Let {name} be the Name of {inputField}.
+ * Let {fields} be all Input Object Fields named {name} in {inputObject}.
+ * {fields} must be the set containing only {inputField}.
**Explanatory Text**
Input objects must not contain more than one field of the same name, otherwise
an ambiguity would exist which includes an ignored portion of syntax.
-For example the following query will not pass validation.
+For example the following document will not pass validation.
```graphql counter-example
{
field(arg: { field: true, field: false })
}
```
### Input Object Required Fields
**Formal Specification**
- * For each Input Object in the document.
- * Let {fields} be the fields provided by that Input Object.
- * Let {fieldDefinitions} be the set of input field definitions of that Input Object.
- * For each {fieldDefinition} in {fieldDefinitions}:
- * Let {type} be the expected type of {fieldDefinition}.
- * Let {defaultValue} be the default value of {fieldDefinition}.
- * If {type} is Non-Null and {defaultValue} does not exist:
- * Let {fieldName} be the name of {fieldDefinition}.
- * Let {field} be the input field in {fields} named {fieldName}
- * {field} must exist.
- * Let {value} be the value of {field}.
- * {value} must not be the {null} literal.
+* For each Input Object in the document.
+ * Let {fields} be the fields provided by that Input Object.
+ * Let {fieldDefinitions} be the set of input field definitions of that Input Object.
+* For each {fieldDefinition} in {fieldDefinitions}:
+ * Let {type} be the expected type of {fieldDefinition}.
+ * Let {defaultValue} be the default value of {fieldDefinition}.
+ * If {type} is Non-Null and {defaultValue} does not exist:
+ * Let {fieldName} be the name of {fieldDefinition}.
+ * Let {field} be the input field in {fields} named {fieldName}
+ * {field} must exist.
+ * Let {value} be the value of {field}.
+ * {value} must not be the {null} literal.
**Explanatory Text**
Input object fields may be required. Much like a field may have required
arguments, an input object may have required fields. An input field is required
if it has a non-null type and does not have a default value. Otherwise, the
input object field is optional.
## Directives
### Directives Are Defined
**Formal Specification**
- * For every {directive} in a document.
- * Let {directiveName} be the name of {directive}.
- * Let {directiveDefinition} be the directive named {directiveName}.
- * {directiveDefinition} must exist.
+* For every {directive} in a document.
+* Let {directiveName} be the name of {directive}.
+* Let {directiveDefinition} be the directive named {directiveName}.
+* {directiveDefinition} must exist.
**Explanatory Text**
-GraphQL servers define what directives they support. For each
-usage of a directive, the directive must be available on that server.
+GraphQL services define what directives they support. For each
+usage of a directive, the directive must be available on that service.
### Directives Are In Valid Locations
**Formal Specification**
- * For every {directive} in a document.
- * Let {directiveName} be the name of {directive}.
- * Let {directiveDefinition} be the directive named {directiveName}.
- * Let {locations} be the valid locations for {directiveDefinition}.
- * Let {adjacent} be the AST node the directive affects.
- * {adjacent} must be represented by an item within {locations}.
+* For every {directive} in a document.
+* Let {directiveName} be the name of {directive}.
+* Let {directiveDefinition} be the directive named {directiveName}.
+* Let {locations} be the valid locations for {directiveDefinition}.
+* Let {adjacent} be the AST node the directive affects.
+* {adjacent} must be represented by an item within {locations}.
**Explanatory Text**
-GraphQL servers define what directives they support and where they support them.
+GraphQL services define what directives they support and where they support them.
For each usage of a directive, the directive must be used in a location that the
-server has declared support for.
+service has declared support for.
-For example the following query will not pass validation because `@skip` does
+For example the following document will not pass validation because `@skip` does
not provide `QUERY` as a valid location.
```graphql counter-example
query @skip(if: $foo) {
field
}
```
### Directives Are Unique Per Location
**Formal Specification**
- * For every {location} in the document for which Directives can apply:
- * Let {directives} be the set of Directives which apply to {location}.
- * For each {directive} in {directives}:
- * Let {directiveName} be the name of {directive}.
- * Let {namedDirectives} be the set of all Directives named {directiveName}
- in {directives}.
- * {namedDirectives} must be a set of one.
+* For every {location} in the document for which Directives can apply:
+ * Let {directives} be the set of Directives which apply to {location} and
+ are not repeatable.
+ * For each {directive} in {directives}:
+ * Let {directiveName} be the name of {directive}.
+ * Let {namedDirectives} be the set of all Directives named {directiveName}
+ in {directives}.
+ * {namedDirectives} must be a set of one.
**Explanatory Text**
Directives are used to describe some metadata or behavioral change on the
definition they apply to. When more than one directive of the same name is used,
the expected metadata or behavior becomes ambiguous, therefore only one of each
directive is allowed per location.
-For example, the following query will not pass validation because `@skip` has
+For example, the following document will not pass validation because `@skip` has
been used twice for the same field:
-```graphql counter-example
+```raw graphql counter-example
query ($foo: Boolean = true, $bar: Boolean = false) {
field @skip(if: $foo) @skip(if: $bar)
}
```
However the following example is valid because `@skip` has been used only once
-per location, despite being used twice in the query and on the same named field:
+per location, despite being used twice in the operation and on the same
+named field:
-```graphql example
+```raw graphql example
query ($foo: Boolean = true, $bar: Boolean = false) {
field @skip(if: $foo) {
subfieldA
}
field @skip(if: $bar) {
subfieldB
}
}
@@ -1479,33 +1521,33 @@ query ($foo: Boolean = true, $bar: Boolean = false) {
## Variables
### Variable Uniqueness
**Formal Specification**
- * For every {operation} in the document
- * For every {variable} defined on {operation}
- * Let {variableName} be the name of {variable}
- * Let {variables} be the set of all variables named {variableName} on
- {operation}
- * {variables} must be a set of one
+* For every {operation} in the document
+ * For every {variable} defined on {operation}
+ * Let {variableName} be the name of {variable}
+ * Let {variables} be the set of all variables named {variableName} on
+ {operation}
+ * {variables} must be a set of one
**Explanatory Text**
If any operation defines more than one variable with the same name, it is
ambiguous and invalid. It is invalid even if the type of the duplicate variable
is the same.
```graphql counter-example
query houseTrainedQuery($atOtherHomes: Boolean, $atOtherHomes: Boolean) {
dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
It is valid for multiple operations to define a variable with the same name. If
two operations reference the same fragment, it might actually be necessary:
@@ -1513,70 +1555,73 @@ two operations reference the same fragment, it might actually be necessary:
query A($atOtherHomes: Boolean) {
...HouseTrainedFragment
}
query B($atOtherHomes: Boolean) {
...HouseTrainedFragment
}
-fragment HouseTrainedFragment {
+fragment HouseTrainedFragment on Query {
dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
### Variables Are Input Types
**Formal Specification**
- * For every {operation} in a {document}
- * For every {variable} on each {operation}
- * Let {variableType} be the type of {variable}
- * {IsInputType(variableType)} must be {true}
+* For every {operation} in a {document}
+* For every {variable} on each {operation}
+ * Let {variableType} be the type of {variable}
+ * {IsInputType(variableType)} must be {true}
**Explanatory Text**
Variables can only be input types. Objects, unions, and interfaces cannot be
used as inputs.
-For these examples, consider the following typesystem additions:
+For these examples, consider the following type system additions:
```graphql example
-input ComplexInput { name: String, owner: String }
+input ComplexInput {
+ name: String
+ owner: String
+}
extend type Query {
findDog(complex: ComplexInput): Dog
booleanList(booleanListArg: [Boolean!]): Boolean
}
```
-The following queries are valid:
+The following operations are valid:
```graphql example
query takesBoolean($atOtherHomes: Boolean) {
dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
query takesComplexInput($complexInput: ComplexInput) {
findDog(complex: $complexInput) {
name
}
}
query TakesListOfBooleanBang($booleans: [Boolean!]) {
booleanList(booleanListArg: $booleans)
}
```
-The following queries are invalid:
+The following operations are invalid:
```graphql counter-example
query takesCat($cat: Cat) {
# ...
}
query takesDogBang($dog: Dog!) {
# ...
@@ -1591,268 +1636,270 @@ query takesCatOrDog($catOrDog: CatOrDog) {
}
```
### All Variable Uses Defined
**Formal Specification**
- * For each {operation} in a document
- * For each {variableUsage} in scope, variable must be in {operation}'s variable list.
- * Let {fragments} be every fragment referenced by that {operation} transitively
- * For each {fragment} in {fragments}
- * For each {variableUsage} in scope of {fragment}, variable must be in
- {operation}'s variable list.
+* For each {operation} in a document
+ * For each {variableUsage} in scope, variable must be in {operation}'s variable list.
+ * Let {fragments} be every fragment referenced by that {operation} transitively
+ * For each {fragment} in {fragments}
+ * For each {variableUsage} in scope of {fragment}, variable must be in
+ {operation}'s variable list.
**Explanatory Text**
Variables are scoped on a per-operation basis. That means that any variable
used within the context of an operation must be defined at the top level of that
operation
For example:
```graphql example
query variableIsDefined($atOtherHomes: Boolean) {
dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
is valid. ${atOtherHomes} is defined by the operation.
-By contrast the following query is invalid:
+By contrast the following document is invalid:
```graphql counter-example
query variableIsNotDefined {
dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
${atOtherHomes} is not defined by the operation.
Fragments complicate this rule. Any fragment transitively included by an
operation has access to the variables defined by that operation. Fragments
can appear within multiple operations and therefore variable usages
must correspond to variable definitions in all of those operations.
For example the following is valid:
```graphql example
query variableIsDefinedUsedInSingleFragment($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
-since {isHousetrainedFragment} is used within the context of the operation
+since {isHouseTrainedFragment} is used within the context of the operation
{variableIsDefinedUsedInSingleFragment} and the variable is defined by that
operation.
On the other hand, if a fragment is included within an operation that does
-not define a referenced variable, the query is invalid.
+not define a referenced variable, the document is invalid.
```graphql counter-example
query variableIsNotDefinedUsedInSingleFragment {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
This applies transitively as well, so the following also fails:
```graphql counter-example
query variableIsNotDefinedUsedInNestedFragment {
dog {
- ...outerHousetrainedFragment
+ ...outerHouseTrainedFragment
}
}
-fragment outerHousetrainedFragment on Dog {
- ...isHousetrainedFragment
+fragment outerHouseTrainedFragment on Dog {
+ ...isHouseTrainedFragment
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
Variables must be defined in all operations in which a fragment
is used.
```graphql example
-query housetrainedQueryOne($atOtherHomes: Boolean) {
+query houseTrainedQueryOne($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-query housetrainedQueryTwo($atOtherHomes: Boolean) {
+query houseTrainedQueryTwo($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
However the following does not validate:
```graphql counter-example
-query housetrainedQueryOne($atOtherHomes: Boolean) {
+query houseTrainedQueryOne($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-query housetrainedQueryTwoNotDefined {
+query houseTrainedQueryTwoNotDefined {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
-This is because {housetrainedQueryTwoNotDefined} does not define
-a variable ${atOtherHomes} but that variable is used by {isHousetrainedFragment}
+This is because {houseTrainedQueryTwoNotDefined} does not define
+a variable ${atOtherHomes} but that variable is used by {isHouseTrainedFragment}
which is included in that operation.
### All Variables Used
**Formal Specification**
- * For every {operation} in the document.
- * Let {variables} be the variables defined by that {operation}
- * Each {variable} in {variables} must be used at least once in either
- the operation scope itself or any fragment transitively referenced by that
- operation.
+* For every {operation} in the document.
+* Let {variables} be the variables defined by that {operation}
+* Each {variable} in {variables} must be used at least once in either
+ the operation scope itself or any fragment transitively referenced by that
+ operation.
**Explanatory Text**
All variables defined by an operation must be used in that operation or a
fragment transitively included by that operation. Unused variables cause
a validation error.
For example the following is invalid:
```graphql counter-example
query variableUnused($atOtherHomes: Boolean) {
dog {
- isHousetrained
+ isHouseTrained
}
}
```
because ${atOtherHomes} is not referenced.
These rules apply to transitive fragment spreads as well:
```graphql example
query variableUsedInFragment($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
-The above is valid since ${atOtherHomes} is used in {isHousetrainedFragment}
+The above is valid since ${atOtherHomes} is used in {isHouseTrainedFragment}
which is included by {variableUsedInFragment}.
If that fragment did not have a reference to ${atOtherHomes} it would be not valid:
```graphql counter-example
query variableNotUsedWithinFragment($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedWithoutVariableFragment
+ ...isHouseTrainedWithoutVariableFragment
}
}
-fragment isHousetrainedWithoutVariableFragment on Dog {
- isHousetrained
+fragment isHouseTrainedWithoutVariableFragment on Dog {
+ isHouseTrained
}
```
All operations in a document must use all of their variables.
As a result, the following document does not validate.
```graphql counter-example
query queryWithUsedVar($atOtherHomes: Boolean) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
query queryWithExtraVar($atOtherHomes: Boolean, $extra: Int) {
dog {
- ...isHousetrainedFragment
+ ...isHouseTrainedFragment
}
}
-fragment isHousetrainedFragment on Dog {
- isHousetrained(atOtherHomes: $atOtherHomes)
+fragment isHouseTrainedFragment on Dog {
+ isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
This document is not valid because {queryWithExtraVar} defines
an extraneous variable.
### All Variable Usages are Allowed
**Formal Specification**
- * For each {operation} in {document}:
- * Let {variableUsages} be all usages transitively included in the {operation}.
- * For each {variableUsage} in {variableUsages}:
- * Let {variableName} be the name of {variableUsage}.
- * Let {variableDefinition} be the {VariableDefinition} named {variableName}
- defined within {operation}.
- * {IsVariableUsageAllowed(variableDefinition, variableUsage)} must be {true}.
+* For each {operation} in {document}:
+* Let {variableUsages} be all usages transitively included in the {operation}.
+* For each {variableUsage} in {variableUsages}:
+ * Let {variableName} be the name of {variableUsage}.
+ * Let {variableDefinition} be the {VariableDefinition} named {variableName}
+ defined within {operation}.
+ * {IsVariableUsageAllowed(variableDefinition, variableUsage)} must be {true}.
IsVariableUsageAllowed(variableDefinition, variableUsage):
+
* Let {variableType} be the expected type of {variableDefinition}.
* Let {locationType} be the expected type of the {Argument}, {ObjectField},
or {ListValue} entry where {variableUsage} is located.
* If {locationType} is a non-null type AND {variableType} is NOT a non-null type:
* Let {hasNonNullVariableDefaultValue} be {true} if a default value exists
for {variableDefinition} and is not the value {null}.
* Let {hasLocationDefaultValue} be {true} if a default value exists for
the {Argument} or {ObjectField} where {variableUsage} is located.
* If {hasNonNullVariableDefaultValue} is NOT {true} AND
{hasLocationDefaultValue} is NOT {true}, return {false}.
* Let {nullableLocationType} be the unwrapped nullable type of {locationType}.
* Return {AreTypesCompatible(variableType, nullableLocationType)}.
* Return {AreTypesCompatible(variableType, locationType)}.
AreTypesCompatible(variableType, locationType):
+
* If {locationType} is a non-null type:
* If {variableType} is NOT a non-null type, return {false}.
* Let {nullableLocationType} be the unwrapped nullable type of {locationType}.
* Let {nullableVariableType} be the unwrapped nullable type of {variableType}.
* Return {AreTypesCompatible(nullableVariableType, nullableLocationType)}.
* Otherwise, if {variableType} is a non-null type:
* Let {nullableVariableType} be the nullable type of {variableType}.
* Return {AreTypesCompatible(nullableVariableType, locationType)}.
@@ -1877,17 +1924,17 @@ Types must match:
```graphql counter-example
query intCannotGoIntoBoolean($intArg: Int) {
arguments {
booleanArgField(booleanArg: $intArg)
}
}
```
-${intArg} typed as {Int} cannot be used as a argument to {booleanArg}, typed as {Boolean}.
+${intArg} typed as {Int} cannot be used as an argument to {booleanArg}, typed as {Boolean}.
List cardinality must also be the same. For example, lists cannot be passed into singular
values.
```graphql counter-example
query booleanListCannotGoIntoBoolean($booleanListArg: [Boolean]) {
arguments {
booleanArgField(booleanArg: $booleanListArg)
@@ -1933,33 +1980,37 @@ This would fail validation because a `[T]` cannot be passed to a `[T]!`.
Similarly a `[T]` cannot be passed to a `[T!]`.
**Allowing optional variables when default values exist**
A notable exception to typical variable type compatibility is allowing a
variable definition with a nullable type to be provided to a non-null location
as long as either that variable or that location provides a default value.
+In the example below, an optional variable `$booleanArg` is allowed to be used
+in the non-null argument `optionalBooleanArg` because the field argument is
+optional since it provides a default value in the schema.
+
```graphql example
query booleanArgQueryWithDefault($booleanArg: Boolean) {
arguments {
optionalNonNullBooleanArgField(optionalBooleanArg: $booleanArg)
}
}
```
-In the example above, an optional variable is allowed to be used in an non-null argument which provides a default value.
+In the example below, an optional variable `$booleanArg` is allowed to be used
+in the non-null argument (`nonNullBooleanArg`) because the variable provides
+a default value in the operation. This behavior is explicitly supported for
+compatibility with earlier editions of this specification. GraphQL authoring
+tools may wish to report this as a warning with the suggestion to replace
+`Boolean` with `Boolean!` to avoid ambiguity.
```graphql example
query booleanArgQueryWithDefault($booleanArg: Boolean = true) {
arguments {
nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
}
}
```
-In the example above, a variable provides a default value and can be used in a
-non-null argument. This behavior is explicitly supported for compatibility with
-earlier editions of this specification. GraphQL authoring tools may wish to
-report this is a warning with the suggestion to replace `Boolean` with `Boolean!`.
-
-Note: The value {null} could still be provided to a such a variable at runtime.
-A non-null argument must produce a field error if provided a {null} value.
+Note: The value {null} could still be provided to such a variable at runtime.
+A non-null argument must raise a field error if provided a {null} value.
~~~
spec/Section 6 -- Execution.md
~~~diff
@@ -36,20 +36,20 @@ ExecuteRequest(schema, document, operationName, variableValues, initialValue):
* Otherwise if {operation} is a subscription operation:
* Return {Subscribe(operation, schema, coercedVariableValues, initialValue)}.
GetOperation(document, operationName):
* If {operationName} is {null}:
* If {document} contains exactly one operation.
* Return the Operation contained in the {document}.
- * Otherwise produce a query error requiring {operationName}.
+ * Otherwise raise a request error requiring {operationName}.
* Otherwise:
* Let {operation} be the Operation named {operationName} in {document}.
- * If {operation} was not found, produce a query error.
+ * If {operation} was not found, raise a request error.
* Return {operation}.
### Validating Requests
As explained in the Validation section, only requests which pass all validation
rules should be executed. If validation errors are known, they should be
reported in the list of "errors" in the response and the request must fail
@@ -66,17 +66,17 @@ For example: the request may be validated during development, provided it does
not later change, or a service may validate a request once and memoize the
result to avoid validating the same request again in the future.
### Coercing Variable Values
If the operation has defined any variables, then the values for
those variables need to be coerced using the input coercion rules
-of variable's declared type. If a query error is encountered during
+of variable's declared type. If a request error is encountered during
input coercion of variable values, then the operation fails without
execution.
CoerceVariableValues(schema, operation, variableValues):
* Let {coercedValues} be an empty unordered Map.
* Let {variableDefinitions} be the variables defined by {operation}.
* For each {variableDefinition} in {variableDefinitions}:
@@ -87,62 +87,63 @@ CoerceVariableValues(schema, operation, variableValues):
* Let {hasValue} be {true} if {variableValues} provides a value for the
name {variableName}.
* Let {value} be the value provided in {variableValues} for the
name {variableName}.
* If {hasValue} is not {true} and {defaultValue} exists (including {null}):
* Add an entry to {coercedValues} named {variableName} with the
value {defaultValue}.
* Otherwise if {variableType} is a Non-Nullable type, and either {hasValue}
- is not {true} or {value} is {null}, throw a query error.
+ is not {true} or {value} is {null}, raise a request error.
* Otherwise if {hasValue} is true:
* If {value} is {null}:
* Add an entry to {coercedValues} named {variableName} with the
value {null}.
* Otherwise:
* If {value} cannot be coerced according to the input coercion
- rules of {variableType}, throw a query error.
+ rules of {variableType}, raise a request error.
* Let {coercedValue} be the result of coercing {value} according to the
input coercion rules of {variableType}.
* Add an entry to {coercedValues} named {variableName} with the
value {coercedValue}.
* Return {coercedValues}.
Note: This algorithm is very similar to {CoerceArgumentValues()}.
## Executing Operations
The type system, as described in the "Type System" section of the spec, must
-provide a query root object type. If mutations or subscriptions are supported,
-it must also provide a mutation or subscription root object type, respectively.
+provide a query root operation type. If mutations or subscriptions are supported,
+it must also provide a mutation or subscription root operation type, respectively.
### Query
If the operation is a query, the result of the operation is the result of
-executing the query’s top level selection set with the query root object type.
+executing the operation’s top level selection set with the query root
+operation type.
-An initial value may be provided when executing a query.
+An initial value may be provided when executing a query operation.
ExecuteQuery(query, schema, variableValues, initialValue):
* Let {queryType} be the root Query type in {schema}.
* Assert: {queryType} is an Object type.
* Let {selectionSet} be the top level Selection Set in {query}.
* Let {data} be the result of running
{ExecuteSelectionSet(selectionSet, queryType, initialValue, variableValues)}
*normally* (allowing parallelization).
* Let {errors} be any *field errors* produced while executing the
selection set.
* Return an unordered map containing {data} and {errors}.
### Mutation
If the operation is a mutation, the result of the operation is the result of
-executing the mutation’s top level selection set on the mutation root
+executing the operation’s top level selection set on the mutation root
object type. This selection set should be executed serially.
It is expected that the top level fields in a mutation operation perform
side-effects on the underlying data system. Serial execution of the provided
mutations ensures against race conditions during these side-effects.
ExecuteMutation(mutation, schema, variableValues, initialValue):
@@ -157,18 +158,18 @@ ExecuteMutation(mutation, schema, variableValues, initialValue):
* Return an unordered map containing {data} and {errors}.
### Subscription
If the operation is a subscription, the result is an event stream called the
"Response Stream" where each event in the event stream is the result of
executing the operation for each new event on an underlying "Source Stream".
-Executing a subscription creates a persistent function on the server that
-maps an underlying Source Stream to a returned Response Stream.
+Executing a subscription operation creates a persistent function on the service
+that maps an underlying Source Stream to a returned Response Stream.
Subscribe(subscription, schema, variableValues, initialValue):
* Let {sourceStream} be the result of running {CreateSourceEventStream(subscription, schema, variableValues, initialValue)}.
* Let {responseStream} be the result of running {MapSourceToResponseEvent(sourceStream, subscription, schema, variableValues)}
* Return {responseStream}.
Note: In large scale subscription systems, the {Subscribe()} and
@@ -216,48 +217,49 @@ events or may complete at any point. Event streams may complete in response to
an error or simply because no more events will occur. An observer may at any
point decide to stop observing an event stream by cancelling it, after which it
must receive no more events from that event stream.
**Supporting Subscriptions at Scale**
Supporting subscriptions is a significant change for any GraphQL service. Query
and mutation operations are stateless, allowing scaling via cloning of GraphQL
-server instances. Subscriptions, by contrast, are stateful and require
+service instances. Subscriptions, by contrast, are stateful and require
maintaining the GraphQL document, variables, and other context over the lifetime
of the subscription.
Consider the behavior of your system when state is lost due to the failure of a
single machine in a service. Durability and availability may be improved by
having separate dedicated services for managing subscription state and client
connectivity.
**Delivery Agnostic**
GraphQL subscriptions do not require any specific serialization format or
transport mechanism. Subscriptions specifies algorithms for the creation of a
stream, the content of each payload on that stream, and the closing of that
-stream. There are intentionally no specifications for message acknoledgement,
+stream. There are intentionally no specifications for message acknowledgement,
buffering, resend requests, or any other quality of service (QoS) details.
Message serialization, transport mechanisms, and quality of service details
should be chosen by the implementing service.
#### Source Stream
A Source Stream represents the sequence of events, each of which will
trigger a GraphQL execution corresponding to that event. Like field value
resolution, the logic to create a Source Stream is application-specific.
CreateSourceEventStream(subscription, schema, variableValues, initialValue):
* Let {subscriptionType} be the root Subscription type in {schema}.
* Assert: {subscriptionType} is an Object type.
+ * Let {selectionSet} be the top level Selection Set in {subscription}.
* Let {groupedFieldSet} be the result of
{CollectFields(subscriptionType, selectionSet, variableValues)}.
- * If {groupedFieldSet} does not have exactly one entry, throw a query error.
+ * If {groupedFieldSet} does not have exactly one entry, raise a request error.
* Let {fields} be the value of the first entry in {groupedFieldSet}.
* Let {fieldName} be the name of the first entry in {fields}.
Note: This value is unaffected if an alias is used.
* Let {field} be the first entry in {fields}.
* Let {argumentValues} be the result of {CoerceArgumentValues(subscriptionType, field, variableValues)}
* Let {fieldStream} be the result of running {ResolveFieldEventStream(subscriptionType, initialValue, fieldName, argumentValues)}.
* Return {fieldStream}.
@@ -325,41 +327,41 @@ ExecuteSelectionSet(selectionSet, objectType, objectValue, variableValues):
* Let {groupedFieldSet} be the result of
{CollectFields(objectType, selectionSet, variableValues)}.
* Initialize {resultMap} to an empty ordered map.
* For each {groupedFieldSet} as {responseKey} and {fields}:
* Let {fieldName} be the name of the first entry in {fields}.
Note: This value is unaffected if an alias is used.
* Let {fieldType} be the return type defined for the field {fieldName} of {objectType}.
* If {fieldType} is defined:
- * Let {responseValue} be {ExecuteField(objectType, objectValue, fields, fieldType, variableValues)}.
+ * Let {responseValue} be {ExecuteField(objectType, objectValue, fieldType, fields, variableValues)}.
* Set {responseValue} as the value for {responseKey} in {resultMap}.
* Return {resultMap}.
-Note: {resultMap} is ordered by which fields appear first in the query. This
+Note: {resultMap} is ordered by which fields appear first in the operation. This
is explained in greater detail in the Field Collection section below.
**Errors and Non-Null Fields**
-If during {ExecuteSelectionSet()} a field with a non-null {fieldType} throws a
+If during {ExecuteSelectionSet()} a field with a non-null {fieldType} raises a
field error then that error must propagate to this entire selection set, either
resolving to {null} if allowed or further propagated to a parent field.
If this occurs, any sibling fields which have not yet executed or have not yet
yielded a value may be cancelled to avoid unnecessary work.
-See the [Errors and Non-Nullability](#sec-Errors-and-Non-Nullability) section
-of Field Execution for more about this behavior.
+Note: See [Handling Field Errors](#sec-Handling-Field-Errors) for more about
+this behavior.
### Normal and Serial Execution
Normally the executor can execute the entries in a grouped field set in whatever
order it chooses (normally in parallel). Because the resolution of fields other
than top-level mutation fields must always be side effect-free and idempotent,
-the execution order must not affect the result, and hence the server has the
+the execution order must not affect the result, and hence the service has the
freedom to execute the field entries in whatever order it deems optimal.
For example, given the following grouped field set to be executed normally:
```graphql example
{
birthday {
month
@@ -446,18 +448,18 @@ A correct executor must generate the following result for that selection set:
```
### Field Collection
Before execution, the selection set is converted to a grouped field set by
calling {CollectFields()}. Each entry in the grouped field set is a list of
fields that share a response key (the alias if defined, otherwise the field
-name). This ensures all fields with the same response key included via
-referenced fragments are executed at the same time.
+name). This ensures all fields with the same response key (including those
+in referenced fragments) are executed at the same time.
As an example, collecting the fields of this selection set would collect two
instances of the field `a` and one of field `b`:
```graphql example
{
a {
subfield1
@@ -474,17 +476,17 @@ fragment ExampleFragment on Query {
```
The depth-first-search order of the field groups produced by {CollectFields()}
is maintained through execution, ensuring that fields appear in the executed
response in a stable and predictable order.
CollectFields(objectType, selectionSet, variableValues, visitedFragments):
- * If {visitedFragments} if not provided, initialize it to the empty set.
+ * If {visitedFragments} is not provided, initialize it to the empty set.
* Initialize {groupedFields} to an empty ordered map of lists.
* For each {selection} in {selectionSet}:
* If {selection} provides the directive `@skip`, let {skipDirective} be that directive.
* If {skipDirective}'s {if} argument is {true} or is a variable in {variableValues} with the value {true}, continue with the next
{selection} in {selectionSet}.
* If {selection} provides the directive `@include`, let {includeDirective} be that directive.
* If {includeDirective}'s {if} argument is not {true} and is not a variable in {variableValues} with the value {true}, continue with the next
{selection} in {selectionSet}.
@@ -502,17 +504,17 @@ CollectFields(objectType, selectionSet, variableValues, visitedFragments):
{fragmentSpreadName}.
* If no such {fragment} exists, continue with the next {selection} in
{selectionSet}.
* Let {fragmentType} be the type condition on {fragment}.
* If {DoesFragmentTypeApply(objectType, fragmentType)} is false, continue
with the next {selection} in {selectionSet}.
* Let {fragmentSelectionSet} be the top-level selection set of {fragment}.
* Let {fragmentGroupedFieldSet} be the result of calling
- {CollectFields(objectType, fragmentSelectionSet, visitedFragments)}.
+ {CollectFields(objectType, fragmentSelectionSet, variableValues, visitedFragments)}.
* For each {fragmentGroup} in {fragmentGroupedFieldSet}:
* Let {responseKey} be the response key shared by all fields in {fragmentGroup}.
* Let {groupForResponseKey} be the list in {groupedFields} for
{responseKey}; if no such list exists, create it as an empty list.
* Append all items in {fragmentGroup} to {groupForResponseKey}.
* If {selection} is an {InlineFragment}:
* Let {fragmentType} be the type condition on {selection}.
* If {fragmentType} is not {null} and {DoesFragmentTypeApply(objectType, fragmentType)} is false, continue
@@ -530,16 +532,19 @@ DoesFragmentTypeApply(objectType, fragmentType):
* If {fragmentType} is an Object Type:
* if {objectType} and {fragmentType} are the same type, return {true}, otherwise return {false}.
* If {fragmentType} is an Interface Type:
* if {objectType} is an implementation of {fragmentType}, return {true} otherwise return {false}.
* If {fragmentType} is a Union:
* if {objectType} is a possible type of {fragmentType}, return {true} otherwise return {false}.
+Note: The steps in {CollectFields()} evaluating the `@skip` and `@include`
+directives may be applied in either order since they apply commutatively.
+
## Executing Fields
Each field requested in the grouped field set that is defined on the selected
objectType will result in an entry in the response map. Field execution first
coerces any provided argument values, then resolves a value for the field, and
finally completes that value either by recursively executing another selection
set or coercing a scalar value.
@@ -553,18 +558,18 @@ ExecuteField(objectType, objectValue, fieldType, fields, variableValues):
### Coercing Field Arguments
Fields may include arguments which are provided to the underlying runtime in
order to correctly produce a value. These arguments are defined by the field in
the type system to have a specific input type.
-At each argument position in a query may be a literal {Value}, or a {Variable}
-to be provided at runtime.
+At each argument position in an operation may be a literal {Value}, or a
+{Variable} to be provided at runtime.
CoerceArgumentValues(objectType, field, variableValues):
* Let {coercedValues} be an empty unordered Map.
* Let {argumentValues} be the argument values provided in {field}.
* Let {fieldName} be the name of {field}.
* Let {argumentDefinitions} be the arguments defined by {objectType} for the
field named {fieldName}.
* For each {argumentDefinition} in {argumentDefinitions}:
@@ -581,35 +586,35 @@ CoerceArgumentValues(objectType, field, variableValues):
name {variableName}.
* Let {value} be the value provided in {variableValues} for the
name {variableName}.
* Otherwise, let {value} be {argumentValue}.
* If {hasValue} is not {true} and {defaultValue} exists (including {null}):
* Add an entry to {coercedValues} named {argumentName} with the
value {defaultValue}.
* Otherwise if {argumentType} is a Non-Nullable type, and either {hasValue}
- is not {true} or {value} is {null}, throw a field error.
+ is not {true} or {value} is {null}, raise a field error.
* Otherwise if {hasValue} is true:
* If {value} is {null}:
* Add an entry to {coercedValues} named {argumentName} with the
value {null}.
* Otherwise, if {argumentValue} is a {Variable}:
* Add an entry to {coercedValues} named {argumentName} with the
value {value}.
* Otherwise:
* If {value} cannot be coerced according to the input coercion
- rules of {variableType}, throw a field error.
+ rules of {argumentType}, raise a field error.
* Let {coercedValue} be the result of coercing {value} according to the
- input coercion rules of {variableType}.
+ input coercion rules of {argumentType}.
* Add an entry to {coercedValues} named {argumentName} with the
value {coercedValue}.
* Return {coercedValues}.
Note: Variable values are not coerced because they are expected to be coerced
-before executing the operation in {CoerceVariableValues()}, and valid queries
+before executing the operation in {CoerceVariableValues()}, and valid operations
must only allow usage of variables of appropriate types.
### Value Resolution
While nearly all of GraphQL execution can be described generically, ultimately
the internal system exposing the GraphQL interface must provide values.
This is exposed via {ResolveFieldValue}, which produces a value for a given
@@ -636,37 +641,58 @@ After resolving the value for a field, it is completed by ensuring it adheres
to the expected return type. If the return type is another Object type, then
the field execution process continues recursively.
CompleteValue(fieldType, fields, result, variableValues):
* If the {fieldType} is a Non-Null type:
* Let {innerType} be the inner type of {fieldType}.
* Let {completedResult} be the result of calling
{CompleteValue(innerType, fields, result, variableValues)}.
- * If {completedResult} is {null}, throw a field error.
+ * If {completedResult} is {null}, raise a field error.
* Return {completedResult}.
* If {result} is {null} (or another internal value similar to {null} such as
- {undefined} or {NaN}), return {null}.
+ {undefined}), return {null}.
* If {fieldType} is a List type:
- * If {result} is not a collection of values, throw a field error.
+ * If {result} is not a collection of values, raise a field error.
* Let {innerType} be the inner type of {fieldType}.
* Return a list where each list item is the result of calling
{CompleteValue(innerType, fields, resultItem, variableValues)}, where
{resultItem} is each item in {result}.
* If {fieldType} is a Scalar or Enum type:
- * Return the result of "coercing" {result}, ensuring it is a legal value of
- {fieldType}, otherwise {null}.
+ * Return the result of {CoerceResult(fieldType, result)}.
* If {fieldType} is an Object, Interface, or Union type:
* If {fieldType} is an Object type.
* Let {objectType} be {fieldType}.
* Otherwise if {fieldType} is an Interface or Union type.
* Let {objectType} be {ResolveAbstractType(fieldType, result)}.
* Let {subSelectionSet} be the result of calling {MergeSelectionSets(fields)}.
* Return the result of evaluating {ExecuteSelectionSet(subSelectionSet, objectType, result, variableValues)} *normally* (allowing for parallelization).
+**Coercing Results**
+
+The primary purpose of value completion is to ensure that the values returned by
+field resolvers are valid according to the GraphQL type system and a service's
+schema. This "dynamic type checking" allows GraphQL to provide consistent
+guarantees about returned types atop any service's internal runtime.
+
+See the Scalars [Result Coercion and Serialization](#sec-Scalars.Result-Coercion-and-Serialization)
+sub-section for more detailed information about how GraphQL's built-in scalars
+coerce result values.
+
+CoerceResult(leafType, value):
+ * Assert {value} is not {null}.
+ * Return the result of calling the internal method provided by the type
+ system for determining the "result coercion" of {leafType} given the value
+ {value}. This internal method must return a valid value for the
+ type and not {null}. Otherwise throw a field error.
+
+Note: If a field resolver returns {null} then it is handled within
+{CompleteValue()} before {CoerceResult()} is called. Therefore both the input
+and output of {CoerceResult()} must not be {null}.
+
**Resolving Abstract Types**
When completing a field with an abstract return type, that is an Interface or
Union return type, first the abstract type must be resolved to a relevant Object
type. This determination is made by the internal system using whatever
means appropriate.
Note: A common method of determining the Object type for an {objectValue} in
@@ -675,21 +701,21 @@ the {objectValue}.
ResolveAbstractType(abstractType, objectValue):
* Return the result of calling the internal method provided by the type
system for determining the Object type of {abstractType} given the
value {objectValue}.
**Merging Selection Sets**
-When more than one fields of the same name are executed in parallel, their
+When more than one field of the same name is executed in parallel, their
selection sets are merged together when completing the value in order to
continue execution of the sub-selection sets.
-An example query illustrating parallel fields with the same name with
+An example operation illustrating parallel fields with the same name with
sub-selections.
```graphql example
{
me {
firstName
}
me {
@@ -705,36 +731,44 @@ MergeSelectionSets(fields):
* Let {selectionSet} be an empty list.
* For each {field} in {fields}:
* Let {fieldSelectionSet} be the selection set of {field}.
* If {fieldSelectionSet} is null or empty, continue to the next field.
* Append all selections in {fieldSelectionSet} to {selectionSet}.
* Return {selectionSet}.
-### Errors and Non-Nullability
+### Handling Field Errors
+
+["Field errors"](#sec-Errors.Field-errors) are raised from a particular field
+during value resolution or coercion. While these errors should be reported in
+the response, they are "handled" by producing a partial response.
+
+Note: This is distinct from ["request errors"](#sec-Errors.Request-errors) which
+are raised before execution begins. If a request error is encountered, execution
+does not begin and no data is returned in the response.
-If an error is thrown while resolving a field, it should be treated as though
-the field returned {null}, and an error must be added to the {"errors"} list
-in the response.
+If a field error is raised while resolving a field, it is handled as though the
+field returned {null}, and the error must be added to the {"errors"} list in
+the response.
If the result of resolving a field is {null} (either because the function to
-resolve the field returned {null} or because an error occurred), and that
-field is of a `Non-Null` type, then a field error is thrown. The
+resolve the field returned {null} or because a field error was raised), and that
+field is of a `Non-Null` type, then a field error is raised. The
error must be added to the {"errors"} list in the response.
-If the field returns {null} because of an error which has already been added to
-the {"errors"} list in the response, the {"errors"} list must not be
+If the field returns {null} because of a field error which has already been
+added to the {"errors"} list in the response, the {"errors"} list must not be
further affected. That is, only one error should be added to the errors list per
field.
Since `Non-Null` type fields cannot be {null}, field errors are propagated to be
handled by the parent field. If the parent field may be {null} then it resolves
to {null}, otherwise if it is a `Non-Null` type, the field error is further
-propagated to it's parent field.
+propagated to its parent field.
If a `List` type wraps a `Non-Null` type, and one of the elements of that list
resolves to {null}, then the entire list must resolve to {null}.
If the `List` type is also wrapped in a `Non-Null`, the field error continues
to propagate upwards.
If all fields from the root of the request to the source of the field error
return `Non-Null` types, then the {"data"} entry in the response should
~~~
spec/Section 7 -- Response.md
~~~diff
@@ -1,76 +1,98 @@
# Response
-When a GraphQL server receives a request, it must return a well-formed
-response. The server's response describes the result of executing the requested
-operation if successful, and describes any errors encountered during the
-request.
-
-A response may contain both a partial response as well as encountered errors in
-the case that a field error occurred on a field which was replaced with {null}.
+When a GraphQL service receives a request, it must return a well-formed
+response. The service's response describes the result of executing the requested
+operation if successful, and describes any errors raised during the request.
+A response may contain both a partial response as well as any field errors in
+the case that a field error was raised on a field and was replaced with {null}.
## Response Format
-A response to a GraphQL operation must be a map.
+A response to a GraphQL request must be a map.
-If the operation encountered any errors, the response map must contain an
+If the request raised any errors, the response map must contain an
entry with key `errors`. The value of this entry is described in the "Errors"
-section. If the operation completed without encountering any errors, this entry
+section. If the request completed without raising any errors, this entry
must not be present.
-If the operation included execution, the response map must contain an entry
+If the request included execution, the response map must contain an entry
with key `data`. The value of this entry is described in the "Data" section. If
-the operation failed before execution, due to a syntax error, missing
+the request failed before execution, due to a syntax error, missing
information, or validation error, this entry must not be present.
The response map may also contain an entry with key `extensions`. This entry,
if set, must have a map as its value. This entry is reserved for implementors
to extend the protocol however they see fit, and hence there are no additional
restrictions on its contents.
-To ensure future changes to the protocol do not break existing servers and
+To ensure future changes to the protocol do not break existing services and
clients, the top level response map must not contain any entries other than the
three described above.
Note: When `errors` is present in the response, it may be helpful for it to
appear first when serialized to make it more clear when errors are present
in a response during debugging.
### Data
The `data` entry in the response will be the result of the execution of the
requested operation. If the operation was a query, this output will be an
-object of the schema's query root type; if the operation was a mutation, this
-output will be an object of the schema's mutation root type.
+object of the query root operation type; if the operation was a
+mutation, this output will be an object of the mutation root operation type.
-If an error was encountered before execution begins, the `data` entry should
+If an error was raised before execution begins, the `data` entry should
not be present in the result.
-If an error was encountered during the execution that prevented a valid
+If an error was raised during the execution that prevented a valid
response, the `data` entry in the response should be `null`.
### Errors
The `errors` entry in the response is a non-empty list of errors, where each
error is a map.
-If no errors were encountered during the requested operation, the `errors`
-entry should not be present in the result.
+If no errors were raised during the request, the `errors` entry should
+not be present in the result.
If the `data` entry in the response is not present, the `errors`
entry in the response must not be empty. It must contain at least one error.
The errors it contains should indicate why no data was able to be returned.
If the `data` entry in the response is present (including if it is the value
-{null}), the `errors` entry in the response may contain any errors that
-occurred during execution. If errors occurred during execution, it should
-contain those errors.
+{null}), the `errors` entry in the response may contain any field errors that
+were raised during execution. If field errors were raised during execution, it
+should contain those errors.
+
+**Request errors**
+
+Request errors are raised before execution begins. This may occur due to a parse
+grammar or validation error in the requested document, an inability to determine
+which operation to execute, or invalid input values for variables.
+
+Request errors are typically the fault of the requesting client.
+
+If a request error is raised, execution does not begin and the `data` entry in
+the response must not be present. The `errors` entry must include the error.
+
+**Field errors**
+
+Field errors are raised during execution from a particular field. This may occur
+due to an internal error during value resolution or failure to coerce the
+resulting value.
+
+Field errors are typically the fault of GraphQL service.
+
+If a field error is raised, execution attempts to continue and a partial result
+is produced (see [Handling Field Errors](#sec-Handling-Field-Errors)).
+The `data` entry in the response must be present. The `errors` entry should
+include all raised field errors.
**Error result format**
Every error must contain an entry with the key `message` with a string
description of the error intended for the developer as a guide to understand
and correct the error.
If an error can be associated to a particular point in the requested GraphQL
@@ -84,20 +106,20 @@ must contain an entry with the key `path` that details the path of the
response field which experienced the error. This allows clients to identify
whether a `null` result is intentional or caused by a runtime error.
This field should be a list of path segments starting at the root of the
response and ending with the field associated with the error. Path segments
that represent fields should be strings, and path segments that
represent list indices should be 0-indexed integers. If the error happens
in an aliased field, the path to the error should use the aliased name, since
-it represents a path in the response, not in the query.
+it represents a path in the response, not in the request.
For example, if fetching one of the friends' names fails in the following
-query:
+operation:
```graphql example
{
hero(episode: $episode) {
name
heroFriends: friends {
id
name
@@ -108,18 +130,18 @@ query:
The response might look like:
```json example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
- "locations": [ { "line": 6, "column": 7 } ],
- "path": [ "hero", "heroFriends", 1, "name" ]
+ "locations": [{ "line": 6, "column": 7 }],
+ "path": ["hero", "heroFriends", 1, "name"]
}
],
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
@@ -137,29 +159,29 @@ The response might look like:
}
}
}
```
If the field which experienced an error was declared as `Non-Null`, the `null`
result will bubble up to the next nullable field. In that case, the `path`
for the error should include the full path to the result field where the error
-occurred, even if that field is not present in the response.
+was raised, even if that field is not present in the response.
For example, if the `name` field from above had declared a `Non-Null` return
type in the schema, the result would look different but the error reported would
be the same:
```json example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
- "locations": [ { "line": 6, "column": 7 } ],
- "path": [ "hero", "heroFriends", 1, "name" ]
+ "locations": [{ "line": 6, "column": 7 }],
+ "path": ["hero", "heroFriends", 1, "name"]
}
],
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
@@ -181,18 +203,18 @@ This entry, if set, must have a map as its value. This entry is reserved for
implementors to add additional information to errors however they see fit, and
there are no additional restrictions on its contents.
```json example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
- "locations": [ { "line": 6, "column": 7 } ],
- "path": [ "hero", "heroFriends", 1, "name" ],
+ "locations": [{ "line": 6, "column": 7 }],
+ "path": ["hero", "heroFriends", 1, "name"],
"extensions": {
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
}
]
}
```
@@ -205,18 +227,18 @@ Note: Previous versions of this spec did not describe the `extensions` entry
for error formatting. While non-specified entries are not violations, they are
still discouraged.
```json counter-example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
- "locations": [ { "line": 6, "column": 7 } ],
- "path": [ "hero", "heroFriends", 1, "name" ],
+ "locations": [{ "line": 6, "column": 7 }],
+ "path": ["hero", "heroFriends", 1, "name"],
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
]
}
```
@@ -269,18 +291,18 @@ values should be used to encode the related GraphQL values:
Note: For consistency and ease of notation, examples of responses are given in
JSON format throughout this document.
### Serialized Map Ordering
Since the result of evaluating a selection set is ordered, the serialized Map of
results should preserve this order by writing the map entries in the same order
-as those fields were requested as defined by query execution. Producing a
-serialized response where fields are represented in the same order in which
+as those fields were requested as defined by selection set execution. Producing
+a serialized response where fields are represented in the same order in which
they appear in the request improves human readability during debugging and
enables more efficient parsing of responses if the order of properties can
be anticipated.
Serialization formats which represent an ordered map should preserve the
order of requested fields as defined by {CollectFields()} in the Execution
section. Serialization formats which only represent unordered maps but where
order is still implicit in the serialization's textual order (such as JSON)
~~~
spec/Appendix A -- Notation Conventions.md
~~~diff
@@ -17,119 +17,133 @@ its right-hand side.
Starting from a single goal non-terminal symbol, a context-free grammar
describes a language: the set of possible sequences of characters that can be
described by repeatedly replacing any non-terminal in the goal sequence with one
of the sequences it is defined by, until all non-terminal symbols have been
replaced by terminal characters.
Terminals are represented in this document in a monospace font in two forms: a
-specific Unicode character or sequence of Unicode characters (ex. {`=`} or {`terminal`}), and a pattern of Unicode characters defined by a regular expression
-(ex {/[0-9]+/}).
+specific Unicode character or sequence of Unicode characters (ie. {`=`} or
+{`terminal`}), and prose typically describing a specific Unicode code-point
+{"Space (U+0020)"}. Sequences of Unicode characters only appear in syntactic
+grammars and represent a {Name} token of that specific sequence.
Non-terminal production rules are represented in this document using the
following notation for a non-terminal with a single definition:
NonTerminalWithSingleDefinition : NonTerminal `terminal`
While using the following notation for a production with a list of definitions:
NonTerminalWithManyDefinitions :
- OtherNonTerminal `terminal`
- `terminal`
A definition may refer to itself, which describes repetitive sequences,
for example:
ListOfLetterA :
- - `a`
- ListOfLetterA `a`
+ - `a`
## Lexical and Syntactical Grammar
The GraphQL language is defined in a syntactic grammar where terminal symbols
are tokens. Tokens are defined in a lexical grammar which matches patterns of
-source characters. The result of parsing a sequence of source Unicode characters
-produces a GraphQL AST.
+source characters. The result of parsing a source text sequence of Unicode
+characters first produces a sequence of lexical tokens according to the lexical
+grammar which then produces abstract syntax tree (AST) according to the
+syntactical grammar.
-A Lexical grammar production describes non-terminal "tokens" by
+A lexical grammar production describes non-terminal "tokens" by
patterns of terminal Unicode characters. No "whitespace" or other ignored
characters may appear between any terminal Unicode characters in the lexical
grammar production. A lexical grammar production is distinguished by a two colon
`::` definition.
-Word :: /[A-Za-z]+/
+Word :: Letter+
A Syntactical grammar production describes non-terminal "rules" by patterns of
-terminal Tokens. Whitespace and other ignored characters may appear before or
-after any terminal Token. A syntactical grammar production is distinguished by a
-one colon `:` definition.
+terminal Tokens. {WhiteSpace} and other {Ignored} sequences may appear before or
+after any terminal {Token}. A syntactical grammar production is distinguished by
+a one colon `:` definition.
-Sentence : Noun Verb
+Sentence : Word+ `.`
## Grammar Notation
This specification uses some additional notation to describe common patterns,
such as optional or repeated patterns, or parameterized alterations of the
definition of a non-terminal. This section explains these short-hand notations
and their expanded definitions in the context-free grammar.
**Constraints**
A grammar production may specify that certain expansions are not permitted by
using the phrase "but not" and then indicating the expansions to be excluded.
-For example, the production:
-
-SafeName : Name but not SevenCarlinWords
+For example, the following production means that the non-terminal {SafeWord} may
+be replaced by any sequence of characters that could replace {Word} provided
+that the same sequence of characters could not replace {SevenCarlinWords}.
-means that the nonterminal {SafeName} may be replaced by any sequence of
-characters that could replace {Name} provided that the same sequence of
-characters could not replace {SevenCarlinWords}.
+SafeWord : Word but not SevenCarlinWords
A grammar may also list a number of restrictions after "but not" separated
by "or".
For example:
NonBooleanName : Name but not `true` or `false`
+**Lookahead Restrictions**
+
+A grammar production may specify that certain characters or tokens are not
+permitted to follow it by using the pattern {[lookahead != NotAllowed]}.
+Lookahead restrictions are often used to remove ambiguity from the grammar.
+
+The following example makes it clear that {Letter+} must be greedy, since {Word}
+cannot be followed by yet another {Letter}.
+
+Word :: Letter+ [lookahead != Letter]
+
+
**Optionality and Lists**
A subscript suffix "{Symbol?}" is shorthand for two possible sequences, one
including that symbol and one excluding it.
As an example:
Sentence : Noun Verb Adverb?
is shorthand for
Sentence :
- - Noun Verb
- Noun Verb Adverb
+ - Noun Verb
-A subscript suffix "{Symbol+}" is shorthand for a list of
-one or more of that symbol.
+A subscript suffix "{Symbol+}" is shorthand for a list of one or more of that
+symbol, represented as an additional recursive production.
As an example:
Book : Cover Page+ Cover
is shorthand for
Book : Cover Page_list Cover
Page_list :
- - Page
- Page_list Page
+ - Page
**Parameterized Grammar Productions**
A symbol definition subscript suffix parameter in braces "{Symbol[Param]}"
is shorthand for two symbol definitions, one appended with that parameter name,
the other without. The same subscript suffix on a symbol is shorthand for that
variant of the definition. If the parameter starts with "?", that
@@ -182,19 +196,19 @@ StringValue :: `"` StringCharacter+ `"`
This specification describes some algorithms used by the static and runtime
semantics, they're defined in the form of a function-like syntax with the
algorithm's name and the arguments it accepts along with a list of algorithmic
steps to take in the order listed. Each step may establish references to other
values, check various conditions, call other algorithms, and eventually return
a value representing the outcome of the algorithm for the provided arguments.
-For example, the following example describes an algorithm named {Fibonacci} which
-accepts a single argument {number}. The algoritm's steps produce the next number
-in the Fibonacci sequence:
+For example, the following example describes an algorithm named {Fibonacci}
+which accepts a single argument {number}. The algorithm's steps produce the next
+number in the Fibonacci sequence:
Fibonacci(number):
* If {number} is {0}:
* Return {1}.
* If {number} is {1}:
* Return {2}.
* Let {previousNumber} be {number} - {1}.
* Let {previousPreviousNumber} be {number} - {2}.
~~~
spec/Appendix B -- Grammar Summary.md
~~~diff
@@ -1,11 +1,17 @@
# B. Appendix: Grammar Summary
-SourceCharacter :: /[\u0009\u000A\u000D\u0020-\uFFFF]/
+## Source Text
+
+SourceCharacter ::
+ - "U+0009"
+ - "U+000A"
+ - "U+000D"
+ - "U+0020–U+FFFF"
## Ignored Tokens
Ignored ::
- UnicodeBOM
- WhiteSpace
- LineTerminator
@@ -15,103 +21,122 @@ Ignored ::
UnicodeBOM :: "Byte Order Mark (U+FEFF)"
WhiteSpace ::
- "Horizontal Tab (U+0009)"
- "Space (U+0020)"
LineTerminator ::
- "New Line (U+000A)"
- - "Carriage Return (U+000D)" [ lookahead ! "New Line (U+000A)" ]
+ - "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
- "Carriage Return (U+000D)" "New Line (U+000A)"
-Comment :: `#` CommentChar*
+Comment :: `#` CommentChar* [lookahead != CommentChar]
CommentChar :: SourceCharacter but not LineTerminator
Comma :: ,
## Lexical Tokens
Token ::
- Punctuator
- Name
- IntValue
- FloatValue
- StringValue
-Punctuator :: one of ! $ ( ) ... : = @ [ ] { | }
+Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
+
+Name ::
+ - NameStart NameContinue* [lookahead != NameContinue]
+
+NameStart ::
+ - Letter
+ - `_`
-Name :: /[_A-Za-z][_0-9A-Za-z]*/
+NameContinue ::
+ - Letter
+ - Digit
+ - `_`
-IntValue :: IntegerPart
+Letter :: one of
+ - `A` `B` `C` `D` `E` `F` `G` `H` `I` `J` `K` `L` `M`
+ - `N` `O` `P` `Q` `R` `S` `T` `U` `V` `W` `X` `Y` `Z`
+ - `a` `b` `c` `d` `e` `f` `g` `h` `i` `j` `k` `l` `m`
+ - `n` `o` `p` `q` `r` `s` `t` `u` `v` `w` `x` `y` `z`
+
+Digit :: one of
+ - `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
+
+IntValue :: IntegerPart [lookahead != {Digit, `.`, NameStart}]
IntegerPart ::
- NegativeSign? 0
- NegativeSign? NonZeroDigit Digit*
NegativeSign :: -
-Digit :: one of 0 1 2 3 4 5 6 7 8 9
-
NonZeroDigit :: Digit but not `0`
FloatValue ::
- - IntegerPart FractionalPart
- - IntegerPart ExponentPart
- - IntegerPart FractionalPart ExponentPart
+ - IntegerPart FractionalPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
+ - IntegerPart FractionalPart [lookahead != {Digit, `.`, NameStart}]
+ - IntegerPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
FractionalPart :: . Digit+
ExponentPart :: ExponentIndicator Sign? Digit+
ExponentIndicator :: one of `e` `E`
Sign :: one of + -
StringValue ::
- - `"` StringCharacter* `"`
+ - `""` [lookahead != `"`]
+ - `"` StringCharacter+ `"`
- `"""` BlockStringCharacter* `"""`
StringCharacter ::
- - SourceCharacter but not `"` or \ or LineTerminator
- - \u EscapedUnicode
- - \ EscapedCharacter
+ - SourceCharacter but not `"` or `\` or LineTerminator
+ - `\u` EscapedUnicode
+ - `\` EscapedCharacter
EscapedUnicode :: /[0-9A-Fa-f]{4}/
-EscapedCharacter :: one of `"` \ `/` b f n r t
+EscapedCharacter :: one of `"` `\` `/` `b` `f` `n` `r` `t`
BlockStringCharacter ::
- SourceCharacter but not `"""` or `\"""`
- `\"""`
Note: Block string values are interpreted to exclude blank initial and trailing
lines and uniform indentation with {BlockStringValue()}.
-## Document
+## Document Syntax
Document : Definition+
Definition :
- ExecutableDefinition
- - TypeSystemDefinition
- - TypeSystemExtension
+ - TypeSystemDefinitionOrExtension
+
+ExecutableDocument : ExecutableDefinition+
ExecutableDefinition :
- OperationDefinition
- FragmentDefinition
OperationDefinition :
- - SelectionSet
- OperationType Name? VariableDefinitions? Directives? SelectionSet
+ - SelectionSet
-OperationType : one of query mutation subscription
+OperationType : one of `query` `mutation` `subscription`
SelectionSet : { Selection+ }
Selection :
- Field
- FragmentSpread
- InlineFragment
@@ -157,17 +182,17 @@ ListValue[Const] :
ObjectValue[Const] :
- { }
- { ObjectField[?Const]+ }
ObjectField[Const] : Name : Value[?Const]
VariableDefinitions : ( VariableDefinition+ )
-VariableDefinition : Variable : Type DefaultValue?
+VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
Variable : $ Name
DefaultValue : = Value[Const]
Type :
- NamedType
- ListType
@@ -180,32 +205,40 @@ ListType : [ Type ]
NonNullType :
- NamedType !
- ListType !
Directives[Const] : Directive[?Const]+
Directive[Const] : @ Name Arguments[?Const]?
+TypeSystemDocument : TypeSystemDefinition+
+
TypeSystemDefinition :
- SchemaDefinition
- TypeDefinition
- DirectiveDefinition
+TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+
+
+TypeSystemDefinitionOrExtension :
+ - TypeSystemDefinition
+ - TypeSystemExtension
+
TypeSystemExtension :
- SchemaExtension
- TypeExtension
-SchemaDefinition : schema Directives[Const]? { OperationTypeDefinition+ }
+SchemaDefinition : Description? schema Directives[Const]? { RootOperationTypeDefinition+ }
SchemaExtension :
- - extend schema Directives[Const]? { OperationTypeDefinition+ }
- - extend schema Directives[Const]
+ - extend schema Directives[Const]? { RootOperationTypeDefinition+ }
+ - extend schema Directives[Const] [lookahead != `{`]
-OperationTypeDefinition : OperationType : NamedType
+RootOperationTypeDefinition : OperationType : NamedType
Description : StringValue
TypeDefinition :
- ScalarTypeDefinition
- ObjectTypeDefinition
- InterfaceTypeDefinition
- UnionTypeDefinition
@@ -220,92 +253,102 @@ TypeExtension :
- EnumTypeExtension
- InputObjectTypeExtension
ScalarTypeDefinition : Description? scalar Name Directives[Const]?
ScalarTypeExtension :
- extend scalar Name Directives[Const]
-ObjectTypeDefinition : Description? type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
+ObjectTypeDefinition :
+ - Description? type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
+ - Description? type Name ImplementsInterfaces? Directives[Const]? [lookahead != `{`]
ObjectTypeExtension :
- extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
- - extend type Name ImplementsInterfaces? Directives[Const]
- - extend type Name ImplementsInterfaces
+ - extend type Name ImplementsInterfaces? Directives[Const] [lookahead != `{`]
+ - extend type Name ImplementsInterfaces [lookahead != `{`]
ImplementsInterfaces :
- - implements `&`? NamedType
- ImplementsInterfaces & NamedType
+ - implements `&`? NamedType
FieldsDefinition : { FieldDefinition+ }
FieldDefinition : Description? Name ArgumentsDefinition? : Type Directives[Const]?
ArgumentsDefinition : ( InputValueDefinition+ )
InputValueDefinition : Description? Name : Type DefaultValue? Directives[Const]?
-InterfaceTypeDefinition : Description? interface Name Directives[Const]? FieldsDefinition?
+InterfaceTypeDefinition :
+ - Description? interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
+ - Description? interface Name ImplementsInterfaces? Directives[Const]? [lookahead != `{`]
InterfaceTypeExtension :
- - extend interface Name Directives[Const]? FieldsDefinition
- - extend interface Name Directives[Const]
+ - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
+ - extend interface Name ImplementsInterfaces? Directives[Const] [lookahead != `{`]
+ - extend interface Name ImplementsInterfaces [lookahead != `{`]
UnionTypeDefinition : Description? union Name Directives[Const]? UnionMemberTypes?
UnionMemberTypes :
- - = `|`? NamedType
- UnionMemberTypes | NamedType
+ - = `|`? NamedType
UnionTypeExtension :
- extend union Name Directives[Const]? UnionMemberTypes
- extend union Name Directives[Const]
-EnumTypeDefinition : Description? enum Name Directives[Const]? EnumValuesDefinition?
+EnumTypeDefinition :
+ - Description? enum Name Directives[Const]? EnumValuesDefinition
+ - Description? enum Name Directives[Const]? [lookahead != `{`]
EnumValuesDefinition : { EnumValueDefinition+ }
EnumValueDefinition : Description? EnumValue Directives[Const]?
EnumTypeExtension :
- extend enum Name Directives[Const]? EnumValuesDefinition
- - extend enum Name Directives[Const]
+ - extend enum Name Directives[Const] [lookahead != `{`]
-InputObjectTypeDefinition : Description? input Name Directives[Const]? InputFieldsDefinition?
+InputObjectTypeDefinition :
+ - Description? input Name Directives[Const]? InputFieldsDefinition
+ - Description? input Name Directives[Const]? [lookahead != `{`]
InputFieldsDefinition : { InputValueDefinition+ }
InputObjectTypeExtension :
- extend input Name Directives[Const]? InputFieldsDefinition
- - extend input Name Directives[Const]
+ - extend input Name Directives[Const] [lookahead != `{`]
-DirectiveDefinition : Description? directive @ Name ArgumentsDefinition? on DirectiveLocations
+DirectiveDefinition : Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
DirectiveLocations :
- - `|`? DirectiveLocation
- DirectiveLocations | DirectiveLocation
+ - `|`? DirectiveLocation
DirectiveLocation :
- ExecutableDirectiveLocation
- TypeSystemDirectiveLocation
ExecutableDirectiveLocation : one of
- `QUERY`
- `MUTATION`
- `SUBSCRIPTION`
- `FIELD`
- `FRAGMENT_DEFINITION`
- `FRAGMENT_SPREAD`
- `INLINE_FRAGMENT`
+ - `QUERY`
+ - `MUTATION`
+ - `SUBSCRIPTION`
+ - `FIELD`
+ - `FRAGMENT_DEFINITION`
+ - `FRAGMENT_SPREAD`
+ - `INLINE_FRAGMENT`
+ - `VARIABLE_DEFINITION`
TypeSystemDirectiveLocation : one of
- `SCHEMA`
- `SCALAR`
- `OBJECT`
- `FIELD_DEFINITION`
- `ARGUMENT_DEFINITION`
- `INTERFACE`
- `UNION`
- `ENUM`
- `ENUM_VALUE`
- `INPUT_OBJECT`
- `INPUT_FIELD_DEFINITION`
+ - `SCHEMA`
+ - `SCALAR`
+ - `OBJECT`
+ - `FIELD_DEFINITION`
+ - `ARGUMENT_DEFINITION`
+ - `INTERFACE`
+ - `UNION`
+ - `ENUM`
+ - `ENUM_VALUE`
+ - `INPUT_OBJECT`
+ - `INPUT_FIELD_DEFINITION`
~~~
Generated with:
```sh
git diff June2018..HEAD --minimal -U8 -- spec
```
================================================
FILE: changelogs/September2025.md
================================================
# September 2025 Changelog
This describes the set of changes since the last edition of the GraphQL
specification, [October2021](https://spec.graphql.org/October2021/) (see
[prior changelog](./October2021.md)). It's intended to ease the review of
changes since the last edition for reviewers or curious readers, but is not
normative. Please read the
[specification document](https://spec.graphql.org/September2025/) itself for
full detail and context.
## Thank you, contributors!
The last few years have seen GraphQL reach a wide breath of use, powering
internal and external APIs at startups and enterprises, and integrating with
various platforms and tools. Work contributed to GraphQL has fit into two
priorities:
1. **Maintain a stable base.** With a huge ecosystem built atop GraphQL, it's
our responsibility to put stability above all else. The vast majority of work
has fixed inconsistencies, improved behavior in edge and corner cases,
improve security, and avoid performance pitfalls.
2. **Provide a productive expressive query language.** GraphQL was designed with
API consumers in mind, and this spec release includes significant
improvements to the GraphQL query engine and language.
Significant contributions have been made since the last edition of the spec, and
both of these priories remain active areas of investment with many exciting RFCs
still in the works.
Over 100 commits made to the GraphQL spec since the last edition, most of which
have dozens or hundreds of comments! It's a huge amount of work to champion
these changes, and a great responsibility to evolve the technical foundations
for this community and ecosystem.
Thank you!
- [@leebyron](https://github.com/leebyron), Editor
## Contributors
Anyone is welcome to join working group meetings and contribute to GraphQL. See
[Contributing.md](https://github.com/graphql/graphql-spec/blob/main/CONTRIBUTING.md)
for more information. Thank you to these community members for their technical
contribution to this edition of the GraphQL specification.
| Author | Github |
| ------------------ | -------------------------------------------------------- |
| Alex Reilly | [@twof](https://github.com/twof) |
| Andreas Marek | [@andimarek](https://github.com/andimarek) |
| Ben Kraft | [@benjaminjkraft](https://github.com/benjaminjkraft) |
| Benedikt Franke | [@spawnia](https://github.com/spawnia) |
| Benjie | [@benjie](https://github.com/benjie) |
| Benoit 'BoD' Lubek | [@BoD](https://github.com/BoD) |
| dondonz | [@dondonz](https://github.com/dondonz) |
| dugenkui | [@dugenkui03](https://github.com/dugenkui03) |
| Glen | [@glen-84](https://github.com/glen-84) |
| Ivan Goncharov | [@IvanGoncharov](https://github.com/IvanGoncharov) |
| Ivan Maximov | [@sungam3r](https://github.com/sungam3r) |
| James Bellenger | [@jbellenger](https://github.com/jbellenger) |
| Jan Melcher | [@Yogu](https://github.com/Yogu) |
| Jason Dent | [@Jason3S](https://github.com/Jason3S) |
| Jeff Auriemma | [@bignimbus](https://github.com/bignimbus) |
| Jovi De Croock | [@JoviDeCroock](https://github.com/JoviDeCroock) |
| Kevin Smithson | [@smitt04](https://github.com/smitt04) |
| Lee Byron | [@leebyron](https://github.com/leebyron) |
| Mark Larah | [@magicmark](https://github.com/magicmark) |
| Martin Bonnin | [@martinbonnin](https://github.com/martinbonnin) |
| Michael Staib | [@michaelstaib](https://github.com/michaelstaib) |
| Mike Solomon | [@msolomon](https://github.com/msolomon) |
| PascalSenn | [@PascalSenn](https://github.com/PascalSenn) |
| Renée | [@goto-bus-stop](https://github.com/goto-bus-stop) |
| Rob Richard | [@robrichard](https://github.com/robrichard) |
| Roman Ivantsov | [@rivantsov](https://github.com/rivantsov) |
| Shane Krueger | [@Shane32](https://github.com/Shane32) |
| Stephen Spalding | [@fotoetienne](https://github.com/fotoetienne) |
| Thomas Heyenbrock | [@thomasheyenbrock](https://github.com/thomasheyenbrock) |
| Yaacov Rydzinski | [@yaacovCR](https://github.com/yaacovCR) |
Generated with:
```sh
node scripts/generate-contributor-list.mjs October2021..HEAD
```
## Notable contributions
A few notable changes in this edition:
- OneOf input objects, aka "input unions"
([#825](https://github.com/graphql/graphql-spec/pull/825))
- Schema coordinates as a standard for GraphQL tooling and reporting
([#794](https://github.com/graphql/graphql-spec/pull/794))
- Descriptions on documents, in motivation for AI-consumers
([#1170](https://github.com/graphql/graphql-spec/pull/1170))
- Broader support for deprecation across a GraphQL Schema
([#805](https://github.com/graphql/graphql-spec/pull/805)
[#1040](https://github.com/graphql/graphql-spec/pull/1040)
[#1053](https://github.com/graphql/graphql-spec/pull/1053)
[#1142](https://github.com/graphql/graphql-spec/pull/1142))
- GraphQL language has been updated to support the full Unicode range
([#849](https://github.com/graphql/graphql-spec/pull/849))
- Countless editorial improvements! The spec is much easier to read and
contribute to, with fewer ambiguities.
## Changeset
- [Github: all Accepted RFC PRs merged since last spec cut](https://github.com/graphql/graphql-spec/pulls?q=is%3Apr+is%3Amerged+base%3Amain+merged%3A2021-10-01..2025-09-03+label%3A%22%F0%9F%8F%81+Accepted+%28RFC+3%29%22)
- [Github: all Editorial PRs merged since last spec cut](https://github.com/graphql/graphql-spec/pulls?page=1&q=is%3Apr+is%3Amerged+base%3Amain+merged%3A2021-10-01..2025-09-03+label%3A%22%E2%9C%8F%EF%B8%8F+Editorial%22)
- [Github: all changes since last spec cut](https://github.com/graphql/graphql-spec/compare/October2021...f29fbcd2ab5af763fce7ad62896eb62465a669b3)
Listed in reverse-chronological order (latest commit on top).
| Hash | Change | Authors |
| -------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [f29fbcd](https://github.com/graphql/graphql-spec/commit/f29fbcd2ab5af763fce7ad62896eb62465a669b3) | gpt guided style guide improvements (#1190) | Lee Byron |
| [0157a79](https://github.com/graphql/graphql-spec/commit/0157a79b8873f0f575d254270ee700f205b0f7b2) | Fix link format for input coercion rules (#1189) | Yaacov Rydzinski |
| [f70abe2](https://github.com/graphql/graphql-spec/commit/f70abe24469d0c8bd374211c8eb621cd421b69f9) | Recommend that order of unordered collections is maintained where possible (#1092) | Benjie Lee Byron |
| [11c6664](https://github.com/graphql/graphql-spec/commit/11c6664e2ea65eddd812bac73b9e7341b4610292) | condense the spec defs appendix (#1186) | Lee Byron |
| [10331b0](https://github.com/graphql/graphql-spec/commit/10331b0f303a1df76d465f153704bc106ed449e6) | RFC: OneOf Input Objects (#825) | Benjie Michael Staib Shane Krueger Yaacov Rydzinski Glen Lee Byron |
| [cd0b8bd](https://github.com/graphql/graphql-spec/commit/cd0b8bd14c0a741ee2ea92d24b97985c6522e5c9) | Pull copyright and license into repo-level LICENSE.md (#1172) | Lee Byron |
| [1e29b8a](https://github.com/graphql/graphql-spec/commit/1e29b8aea0e098663e43dba4693e88c6b033f6f8) | Schema Coordinates (#794) | Mark Larah Benjie Gillam Lee Byron Lee Byron Martin Bonnin Benoit 'BoD' Lubek |
| [bc4ddea](https://github.com/graphql/graphql-spec/commit/bc4ddea0c9121a3e4effd4bbdcc0ebdfaae6e79a) | Fix CoerceArgumentValues() hasValue (#1056) | Benjie |
| [b1a23de](https://github.com/graphql/graphql-spec/commit/b1a23de96f86865b2bc7f8bd613900d4afd10cc0) | Fix typo in merged "executable documents" PR (#1176) | Benjie Lee Byron |
| [9939469](https://github.com/graphql/graphql-spec/commit/9939469d1315dc2f7458c885699996e2a78516f1) | Minor editorial tweaks following the merge of #1039 (#1175) | Benjie |
| [468d848](https://github.com/graphql/graphql-spec/commit/468d84881646b9da00511c7ce68febdfc9a1875b) | editorial: move normative clause above example in descriptions (#1173) | Lee Byron |
| [7eb8983](https://github.com/graphql/graphql-spec/commit/7eb8983955fbbc047da301d6a58c450a175cea5b) | [RFC] Default value coercion rules (#793) | Benjie Lee Byron |
| [fca6653](https://github.com/graphql/graphql-spec/commit/fca66537c5e1f0743eb67de76d520cde0892e6dc) | Add descriptions to executable documents 2025 Update (#1170) | Stephen Spalding Ivan Goncharov Glen Lee Byron |
| [17e2a47](https://github.com/graphql/graphql-spec/commit/17e2a4739c2189b653de3f7c721bf1fad7ac721a) | Replace `ExecuteSelectionSet` with `ExecuteCollectedFields` (#1039) | Benjie Yaacov Rydzinski Lee Byron |
| [e71805e](https://github.com/graphql/graphql-spec/commit/e71805e0745b61239824475fdfff8f0fd116b83f) | Fixed unclear wording in the validation section (#1096) | PascalSenn Benjie Lee Byron |
| [ccf23e3](https://github.com/graphql/graphql-spec/commit/ccf23e304bc4314511cb0712b0787d2528783337) | Define Data Collections used in the spec (#1102) | Benjie Lee Byron |
| [ac483bd](https://github.com/graphql/graphql-spec/commit/ac483bdda45b8fe09fabaa50940e4cdfa24a0372) | Add validation rule that operation types exist (#955) | Ben Kraft Benjie Gillam Shane Krueger |
| [883c759](https://github.com/graphql/graphql-spec/commit/883c7592d88579fbd13070d5c17f25f621abb8d7) | Do not exclude schema keyword if schema has description (#1167) | Benjie |
| [df9f4f8](https://github.com/graphql/graphql-spec/commit/df9f4f8330109e1aca06af27b78ac1055a27e3a0) | Define "execution result" and "request error result" (#1159) | Rob Richard Benjie |
| [9d0710e](https://github.com/graphql/graphql-spec/commit/9d0710e050f10c1ff5d0c659ec289713a6ebe8b9) | Add Appendix C - Specified Type System Definitions (#1037) | Martin Bonnin Benjie Lee Byron |
| [a44d4ec](https://github.com/graphql/graphql-spec/commit/a44d4ec24e4c1263ab984a201cf9207c5a4dd549) | [RFC] Prevent @skip and @include on root subscription selection set (#860) | Benjie |
| [3b0d8e6](https://github.com/graphql/graphql-spec/commit/3b0d8e606609b196d9ae643f4e0f0707700d46cc) | Add 'Assert' consistency to algorithm format check (#1168) | Benjie |
| [646f937](https://github.com/graphql/graphql-spec/commit/646f937c5cf005fec43a4a3749e0c5b5406cb601) | make `includeDeprecated` non nullable (#1142) | Martin Bonnin |
| [73d8b26](https://github.com/graphql/graphql-spec/commit/73d8b2624270b3cc22255125479ad10a7591f6d3) | Implementations may not deprecate a field that the interface hasn't deprecated (#1053) | Benjie Lee Byron |
| [1f690d4](https://github.com/graphql/graphql-spec/commit/1f690d48149505c8a0fa94f2f9a525b69d876ace) | Clarify 'Values of Correct Type' rule relates to literals (#1118) | Benjie |
| [a1884bb](https://github.com/graphql/graphql-spec/commit/a1884bb9386b6105ff81ad628a08b57e1237ccd5) | 3.13 Directive validation edits (#1089) | James Bellenger Benjie |
| [c855454](https://github.com/graphql/graphql-spec/commit/c855454d518ecd7fd2eaf8e7d696f0c94104709a) | 3.10 Input Objects - clarify lists are permitted as Input fields (#1068) | Mike Solomon Benjie |
| [586cbed](https://github.com/graphql/graphql-spec/commit/586cbedc4e38d27512965bd641a0588658dd6d99) | Add 'extensions' to request (#976) | Benjie Lee Byron |
| [4390617](https://github.com/graphql/graphql-spec/commit/43906173238470b76173948053bfcb2a2bfc2e99) | Consistently use 'response name' not 'response key' (#1147) | Benjie Lee Byron |
| [521ef5b](https://github.com/graphql/graphql-spec/commit/521ef5b200e61db7edc6c72dd4619af15eaa872a) | Rename field error to execution error; define response position (#1152) | Benjie Lee Byron |
| [4abd86e](https://github.com/graphql/graphql-spec/commit/4abd86e33c70bd880e6f5605422c59343f3aa30c) | Consistently use result map when referring to objectTypes selection set result (#1148) | Rob Richard |
| [0ab3d4b](https://github.com/graphql/graphql-spec/commit/0ab3d4b80103beb265777f95d202312a7dd7d63d) | Ensure algorithm steps are always wrapped in braces (#1146) | Benjie |
| [b7c57ea](https://github.com/graphql/graphql-spec/commit/b7c57ea7f218474701c52fd9d1ee383600414343) | Fix choice of language used in algorithm (#1134) | Benjie |
| [b41339a](https://github.com/graphql/graphql-spec/commit/b41339a0fd22fb3ca9a1a6110334a7094f1f7643) | Editorial: Add response stream to Response Section (#1135) | Rob Richard |
| [a1c025f](https://github.com/graphql/graphql-spec/commit/a1c025f3b2d9c69ad208dfc5a28639746c60ee09) | Field merging validation: clarify pair members are distinct (#1136) | Andreas Marek |
| [e9ac8c8](https://github.com/graphql/graphql-spec/commit/e9ac8c8ce46fda6759a29adc61949b9b9356344a) | Editorial: move "Path" to it's own section (#1129) | Rob Richard Benjie |
| [2073bc8](https://github.com/graphql/graphql-spec/commit/2073bc888d92692cd465db47cc544109db9c4181) | Change 'original' to 'previous' to clarify multiple extensions (#1123) | Benjie |
| [5bf400e](https://github.com/graphql/graphql-spec/commit/5bf400e3b2867e6366fc27616b777ccd3388bbfd) | "data" and "errors" appear in the "response", not the "result" (#1130) | Benjie |
| [c37a4a4](https://github.com/graphql/graphql-spec/commit/c37a4a400e2ff61ade66c1c5947e0845a6b85c9f) | Editorial changes for Event Streams (#1099) | Lee Byron |
| [34730e8](https://github.com/graphql/graphql-spec/commit/34730e86a151643f51106ac1a1f35985d3da8caa) | Make the reason argument in `@deprecated` non-nullable (#1040) | Martin Bonnin |
| [df1acea](https://github.com/graphql/graphql-spec/commit/df1acea882d28df22184a7c12449864b239b9dd7) | Fix coercion table for list (#1057) | Benjie |
| [7073e3a](https://github.com/graphql/graphql-spec/commit/7073e3a096c0a3fea9e605079a885d988a59d79f) | enhance(ResolveFieldValue): note that list items may be async (#1066) | Yaacov Rydzinski |
| [e5bddd9](https://github.com/graphql/graphql-spec/commit/e5bddd9c31fcd6796698e4fd059514a5a035bf20) | Fix punctuation in grammar rule (#1084) | Benjie |
| [a80f9ff](https://github.com/graphql/graphql-spec/commit/a80f9ff13b7ef9957d93ebe09e2dc4f2c93aede4) | Consistently spell 'implementers' (#1087) | Benjie |
| [497e333](https://github.com/graphql/graphql-spec/commit/497e333c35a29dbe1ab3d1f24c31a59789843fb6) | Fix reference mistake in subscription execution (#994) | Jan Melcher |
| [7485a34](https://github.com/graphql/graphql-spec/commit/7485a342b1c5f0ba4dbbe270d1f6863f55f7180b) | Reformat the parts of an execution request (#1090) | Benjie |
| [8076f1e](https://github.com/graphql/graphql-spec/commit/8076f1e32a305188b41a71c2fec382b72186b424) | chore: add clarifying note for composite and expand term (#1078) | Jovi De Croock Benjie |
| [b5ecff0](https://github.com/graphql/graphql-spec/commit/b5ecff0b7dfd1ebe8bb8a3de7e9180b93da73c53) | Add definition of "selection set" and clarify serial execution examples (#1032) | Benjie |
| [32d24f6](https://github.com/graphql/graphql-spec/commit/32d24f6fda912253d395639561a88deae0863a8e) | Be strict about error paths format (#1073) | Martin Bonnin Benjie |
| [4ab71e3](https://github.com/graphql/graphql-spec/commit/4ab71e330a41742d0b169be94d42055fad26b130) | Add missing . (#1088) | Benjie |
| [a5da8bb](https://github.com/graphql/graphql-spec/commit/a5da8bb39e3ae44e38e7744e0b7bcb2dba453f97) | ID must always serialize as String would (#1086) | Benjie |
| [0ba7cdf](https://github.com/graphql/graphql-spec/commit/0ba7cdf781125779498df3b45a5b97a697a439ba) | Enforce consistent punctuation in algorithms (#1069) | Benjie |
| [8682a86](https://github.com/graphql/graphql-spec/commit/8682a86cd66cdc6d054c678a3f3ef7a32457e5fc) | Fix 'response error' -> 'request error' (#1016) | Benjie |
| [feac5a5](https://github.com/graphql/graphql-spec/commit/feac5a54c6a95c1d4f7804bfaeb268c8bd206f2c) | Fix punctuation in some algorithms (#1067) | Yaacov Rydzinski |
| [56d6107](https://github.com/graphql/graphql-spec/commit/56d61073137caac3dbea6ec8c3652cc3c8b90d86) | Fix heading level for Required Arguments validation rule (#1055) | Renée |
| [3adfcca](https://github.com/graphql/graphql-spec/commit/3adfcca73644234fbbbb062a5cec9e7703419a9f) | Add explicit definition for BlockString (#1042) | Benjie |
| [6b7c2c4](https://github.com/graphql/graphql-spec/commit/6b7c2c45d7136f7fa727cd64b994d2573d4e8014) | Remove "subscriptions is a significant change" sentence (#983) | Roman Ivantsov Benjie Roman Ivantsov |
| [a5cc6ea](https://github.com/graphql/graphql-spec/commit/a5cc6ea0c9f3850f58ef1947d8eda8c6e593b95a) | Clarify that selection sets cannot be empty (#1025) | Benjie |
| [afc0a35](https://github.com/graphql/graphql-spec/commit/afc0a35d271ba9502c3c68aeda6e6c6fbc223774) | Add links to contributed custom scalar specs at scalars.graphql.org (#1009) | dondonz <13839920+dondonz@users.noreply.github.com> Benjie |
| [4e93488](https://github.com/graphql/graphql-spec/commit/4e93488096479c3fcfcc905126c8d157ad2e8c4c) | Fix ambiguity around when schema definition may be omitted (#987) | Benjie Lee Byron |
| [12b7ad7](https://github.com/graphql/graphql-spec/commit/12b7ad7f0fe6ac3996fd5a2bc564357cd2dcb0bc) | add explanation about argument name uniqueness. (#891) | dugenkui Benjie Gillam |
| [559063c](https://github.com/graphql/graphql-spec/commit/559063cb37c14ed74050f73efd3971ee13ff134d) | Change 'server' to 'service' (#1005) | Benjie |
| [cbb8354](https://github.com/graphql/graphql-spec/commit/cbb83545cf8eeb5106856dda8f9d0b750f553124) | Fix broken license link (#1007) | Lee Byron |
| [e736f78](https://github.com/graphql/graphql-spec/commit/e736f78b3cb5c8abb1d6b2ec5e5102de455f98ed) | Add a style guide to the specification (#1003) | Benjie |
| [193fba3](https://github.com/graphql/graphql-spec/commit/193fba3912a43a5e2c9741f4269d279a018e2829) | field merging - field TYPES must not differ (#979) | Roman Ivantsov Roman Ivantsov Benjie Lee Byron |
| [3d03cab](https://github.com/graphql/graphql-spec/commit/3d03cab7a8ffc455387f486fbab5155c2a7c7f3e) | P30: Fixed the explanatory text for algorithm checking uniqueness of non-repeatable directives (#975) | Roman Ivantsov Roman Ivantsov Lee Byron |
| [342b838](https://github.com/graphql/graphql-spec/commit/342b8381ee0ebd25071697e2cc7e784d539700c7) | P34: implementing field type is either exact match or of covariant type (#974) | Roman Ivantsov Roman Ivantsov Lee Byron |
| [ab865f9](https://github.com/graphql/graphql-spec/commit/ab865f95c000b48bdbe9134c4e53573f1996a5c1) | Provide explicit ref to Value Completion section (#982) | Roman Ivantsov Benjie Gillam Roman Ivantsov |
| [edda836](https://github.com/graphql/graphql-spec/commit/edda83613a999c562afbfec6fba2accb96f058a4) | Changed 'must NOT BE' to 'must not be' (#980) | Roman Ivantsov Roman Ivantsov |
| [3aa021f](https://github.com/graphql/graphql-spec/commit/3aa021fb3651710508a37e13c71b7268189982f9) | separate out IsSubType from IsValidImplementationFieldType (#977) | Yaacov Rydzinski |
| [47a6bfd](https://github.com/graphql/graphql-spec/commit/47a6bfdba35ad9b96cba501a52593d3f04c8c5e9) | Editorial: Clarify intro for inline fragments (#969) | Lee Byron |
| [3885a64](https://github.com/graphql/graphql-spec/commit/3885a64f3dc0f9cb64b43aaf1f77e661d98f4dca) | Editorial: Error Terminology (#966) | Lee Byron Roman Ivantsov Benjie Gillam |
| [9a96fc4](https://github.com/graphql/graphql-spec/commit/9a96fc40f2307af15eecc3a257f85ec49adf50d9) | Editorial: Clarity about subject being a GraphQL service or system (#965) | Lee Byron Roman Ivantsov |
| [57bd86d](https://github.com/graphql/graphql-spec/commit/57bd86d779482e9167d2113a9ba926e2ecb74dcc) | Editorial: Fix reference to object in interface introspection (#964) | Lee Byron Roman Ivantsov |
| [299ce69](https://github.com/graphql/graphql-spec/commit/299ce69388aa7fb0012246e377950cf3fd41439d) | Editorial: Remove inaccurate statement about line terminator within tokens (#963) | Lee Byron Roman Ivantsov |
| [1b8fe7a](https://github.com/graphql/graphql-spec/commit/1b8fe7a68f3552a614ab467ea1313947e9f7b1fc) | Leaf field selections clarification (#958) | Benjie Gillam Roman Ivantsov Lee Byron |
| [a91e158](https://github.com/graphql/graphql-spec/commit/a91e15832682931585b8dad46fc8996df1d29735) | Light editorial around delivery agnostic subscriptions (#959) | Benjie Gillam Roman Ivantsov |
| [bb95060](https://github.com/graphql/graphql-spec/commit/bb950608a161af5fc255835bcce10bc160ae1bc8) | Move punctuation outside quotation marks (#962) | Benedikt Franke |
| [4de8782](https://github.com/graphql/graphql-spec/commit/4de87822d2bc807b746c6a37f6a99bb54d705185) | Clarify query shorthand relationship with directives (#873) | Benjie Gillam Lee Byron |
| [94f73f4](https://github.com/graphql/graphql-spec/commit/94f73f48c12a6bdafa9dc729a89482aa7917303f) | Allow deprecation of input values (field args, directive args, input fields) (#805) | Ivan Goncharov Kevin Smithson Lee Byron Ivan Maximov Stephen Spalding |
| [6b69577](https://github.com/graphql/graphql-spec/commit/6b69577b6e854099ce2595849e0b887c13fe300c) | use `findDog` query from example schema only after defining it (#927) | Thomas Heyenbrock |
| [7dd73e7](https://github.com/graphql/graphql-spec/commit/7dd73e7607665bc84dbac504ae83058c996cffcf) | Define request, note it is transport independent (#949) | Benjie Gillam |
| [84ec339](https://github.com/graphql/graphql-spec/commit/84ec33914393bb46e49e5d7a7fb05e0626daa7a8) | RFC: Allow full unicode range (#849) | Lee Byron Andreas Marek |
| [00b88f0](https://github.com/graphql/graphql-spec/commit/00b88f05c4ae049f649e163a4914c94e58a784bf) | Fix formatting | Lee Byron |
| [a61e35d](https://github.com/graphql/graphql-spec/commit/a61e35d301229c494b8bbd69ab0140b33884a95a) | consistent indentation and punctuation (#925) | Thomas Heyenbrock |
| [266fcca](https://github.com/graphql/graphql-spec/commit/266fcca386518d4b05ed58f2d8f57f1fdaaad7f1) | Rename VariableDefinitions to VariablesDefinition (#916) | Ivan Maximov |
| [7908822](https://github.com/graphql/graphql-spec/commit/7908822d4cbae5ad7136437ca577e8bc56f7efab) | Format the spec with prettier (#727) | Benjie Gillam |
| [c18590c](https://github.com/graphql/graphql-spec/commit/c18590c477d6817d73e19b6c248c9fcc62b03017) | Fix typo in Type System section (#905) | Benoit Lubek |
| [60323c9](https://github.com/graphql/graphql-spec/commit/60323c90dc2699266be7da7f0d4ab293953cd733) | fix typo (#896) | Alex Reilly |
| [357bb72](https://github.com/graphql/graphql-spec/commit/357bb727b5a9dc5b1ed132cc4ffa27c309fb8258) | Start next working draft | Lee Byron |
Generated with:
```sh
git log October2021..f29fbcd2ab5af763fce7ad62896eb62465a669b3 --format="| [%h](https://github.com/graphql/graphql-spec/commit/%H) | %s | %an <%ae> %(trailers:key=Co-authored-by,valueonly,separator=%x20)" -- spec
```
## Diff
[Github: diff from last spec cut](https://github.com/graphql/graphql-spec/compare/October2021...f29fbcd2ab5af763fce7ad62896eb62465a669b3?w=1)
================================================
FILE: cspell.yml
================================================
language: en-US
ignoreRegExpList:
# Posessives
- /[a-z]{2,}'s/
words:
# Terms of art
- endianness
- interoperation
- monospace
- openwebfoundation
- parallelization
- structs
- subselection
# Fictional characters / examples
- alderaan
- hagrid
- leia
- newhope
- othername
- skywalker
- tatooine
- zuck
- zuckerberg
- brontie
- oneOf
# Forbid Alternative spellings
flagWords:
- implementor
- implementors
================================================
FILE: package.json
================================================
{
"name": "graphql-spec",
"private": true,
"contributors": [
"Lee Byron (http://leebyron.com/)",
"Nicholas Schrock ",
"Daniel Schafer "
],
"license": "OWFa-1.0",
"homepage": "https://spec.graphql.org/",
"repository": {
"type": "git",
"url": "http://github.com/graphql/graphql-spec.git"
},
"scripts": {
"test": "npm run test:spelling && npm run test:format && npm run test:build",
"test:spelling": "cspell \"spec/**/*.md\" README.md LICENSE.md",
"format": "prettier --write \"**/*.{md,yml,yaml,json}\"",
"test:format": "prettier --check \"**/*.{md,yml,yaml,json}\" || npm run suggest:format",
"test:algorithm-format": "node .github/algorithm-format-check.mjs",
"suggest:format": "echo \"\nTo resolve this, run: $(tput bold)npm run format$(tput sgr0)\" && exit 1",
"build": "./build.sh",
"test:build": "spec-md --metadata spec/metadata.json spec/GraphQL.md > /dev/null",
"watch": "nodemon -e json,md --exec \"npm run build\"",
"update-appendix-specified-definitions": "node scripts/update-appendix-specified-definitions.mjs; prettier --write \"spec/Appendix D -- Specified Definitions.md\""
},
"devDependencies": {
"cspell": "5.9.1",
"nodemon": "2.0.20",
"prettier": "2.8.2",
"spec-md": "3.1.0",
"graphql": "^17.0.0-alpha.9"
},
"prettier": {
"proseWrap": "always",
"trailingComma": "none"
}
}
================================================
FILE: scripts/generate-contributor-list.mjs
================================================
import { execFileSync } from "node:child_process";
import https from "node:https";
// ---------- flags / utils
const RAW_ARGS = process.argv.slice(2);
const DEBUG = RAW_ARGS.includes("--debug");
const logd = (...xs) => { if (DEBUG) console.error(...xs) };
const collator = new Intl.Collator("en", { sensitivity: "base" });
const cmp = (a, b) => collator.compare(a, b);
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const toLower = (s) => (s || "").toLowerCase();
function die(msg, code = 1) { console.error(msg); process.exit(code) }
function normalizeName(s) {
return (s || "")
.normalize("NFKD")
.replace(/\p{Diacritic}/gu, "")
.replace(/\s+/g, " ")
.trim()
.toLowerCase();
}
function pickBetterName(current, candidate) {
if (!current) return (candidate || "").trim();
const c = current.trim();
const d = (candidate || "").trim();
if (!c && d) return d;
const spaceC = /\s/.test(c), spaceD = /\s/.test(d);
if (spaceD && !spaceC) return d;
if (d.length > c.length) return d;
return c;
}
function sanitizeDisplayName(raw, fallback) {
const s = (raw || "").trim();
if (!s) return fallback;
if (/moved\s+to\s+@/i.test(s)) return fallback;
if (/@/.test(s)) return fallback;
if (/^\s*[-–—]+\s*$/.test(s)) return fallback;
return s;
}
function parseArgs() {
const args = RAW_ARGS.filter(x => x !== "--debug");
if (args.length === 0) die("Usage: node scripts/generate-contributor-list.mjs [-- ] [--debug]");
const dd = args.indexOf("--");
return { range: args[0], paths: dd === -1 ? [] : args.slice(dd + 1) };
}
function execGit(argv, opts = {}) {
try {
return execFileSync("git", argv, { encoding: "utf8", maxBuffer: 1024 * 1024 * 400, ...opts });
} catch (e) {
die(`git ${argv.join(" ")} failed: ${e.message}`);
}
}
function repoNameWithOwner() {
let url = "";
try { url = execGit(["remote", "get-url", "origin"]).trim(); } catch { }
const m = url.match(/github\.com[:/](?[^/]+)\/(?[^/]+?)(?:\.git)?$/i);
if (m?.groups) return `${m.groups.owner}/${m.groups.repo}`;
die("Could not determine GitHub repo from 'origin' remote.");
}
function revList(range, paths) {
const args = ["rev-list", range];
if (paths.length) args.push("--", ...paths);
const out = execGit(args);
return out.split(/\r?\n/).filter(Boolean);
}
function parseCoAuthorLines(message) {
const out = [];
const re = /^[ \t]*Co-authored-by:\s*(.+?)\s*<([^>]+)>/gim;
let m;
while ((m = re.exec(message))) out.push({ name: m[1].trim(), email: m[2].trim() });
return out;
}
function loginFromNoreply(email) {
const m = email.toLowerCase().match(/^(?:\d+\+)?([a-z0-9-]+)@users\.noreply\.github\.com$/i);
return m ? m[1] : "";
}
function candidateHandlesFromEmailAndName(email, name) {
const cands = new Set();
const local = email.split("@")[0];
const bare = local.replace(/[._]/g, "");
const bareNoDigits = bare.replace(/\d+$/, "");
cands.add(bare); cands.add(bareNoDigits);
const parts = local.split(/[._-]+/).filter(Boolean);
if (parts.length >= 2) {
const first = parts[0], last = parts[parts.length - 1];
cands.add(`${first}${last}`);
cands.add(`${first}-${last}`);
cands.add(`${first}_${last}`);
cands.add(`${first[0]}${last}`);
if (last.length >= 3) cands.add(`${first}${last.slice(0, 3)}`);
}
const nameParts = name.split(/\s+/).filter(Boolean);
if (nameParts.length >= 2) {
const f = nameParts[0].replace(/[^A-Za-z0-9-]/g, "");
const l = nameParts[nameParts.length - 1].replace(/[^A-Za-z0-9-]/g, "");
if (f && l) {
cands.add(`${f}${l}`);
cands.add(`${f}-${l}`);
cands.add(`${f[0]}${l}`);
}
}
const q = name.match(/'([^']{1,39})'/); if (q) cands.add(q[1]);
const p = name.match(/\(([^) ]{1,39})\)/); if (p) cands.add(p[1]);
return Array.from(cands).filter(s => /^[A-Za-z0-9-]{2,39}$/.test(s));
}
// ---------- GraphQL
const REPO = repoNameWithOwner();
const [OWNER, NAME] = REPO.split("/");
function getToken() {
const env = process.env.GITHUB_TOKEN || process.env.GH_TOKEN || "";
if (env) return env;
try { return execFileSync("gh", ["auth", "token"], { encoding: "utf8" }).trim(); } catch { return ""; }
}
const TOKEN = getToken();
if (!TOKEN) console.error("Warning: no GITHUB_TOKEN/GH_TOKEN (or gh auth token). Resolution will be limited.");
async function graphql(query, variables) {
const body = JSON.stringify(variables ? { query, variables } : { query });
const options = {
hostname: "api.github.com",
path: "/graphql",
method: "POST",
headers: {
"User-Agent": "contributors-table-graphql",
"Authorization": `Bearer ${TOKEN}`,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body),
},
};
return await new Promise((resolve) => {
const req = https.request(options, (res) => {
let data = "";
res.setEncoding("utf8");
res.on("data", (c) => (data += c));
res.on("end", () => {
try {
const json = JSON.parse(data || "{}");
if (json.errors && DEBUG) console.error("GraphQL errors:", JSON.stringify(json.errors, null, 2));
resolve(json);
} catch { resolve({}); }
});
});
req.on("error", () => resolve({}));
req.write(body);
req.end();
});
}
// Batch fetch commit author + message for SHAs; count primary-author occurrences per login
async function fetchCommitsByOidBatch(oids) {
const out = new Map(); // oid -> { login | "", name, email, message }
const authorCount = new Map(); // login -> # of primary authored commits in range
const chunkSize = 40;
for (let i = 0; i < oids.length; i += chunkSize) {
const chunk = oids.slice(i, i + chunkSize);
const fields = chunk.map((oid, idx) => `
c${idx}: object(oid: "${oid}") {
... on Commit {
message
author { user { login } name email }
}
}`).join("\n");
const q = `query($owner:String!, $name:String!) { repository(owner:$owner, name:$name) { ${fields} } }`;
const res = await graphql(q, { owner: OWNER, name: NAME });
const repo = res?.data?.repository || {};
for (let idx = 0; idx < chunk.length; idx++) {
const node = repo[`c${idx}`];
if (!node) continue;
const info = {
login: node?.author?.user?.login || "",
name: node?.author?.name || "",
email: node?.author?.email || "",
message: node?.message || "",
};
out.set(chunk[idx], info);
const L = info.login;
if (L) authorCount.set(L, (authorCount.get(L) || 0) + 1);
}
}
return { commitInfo: out, authorCount };
}
// GraphQL user search helpers (users only)
async function searchUsersByNameExact(name) {
if (!TOKEN) return "";
const queryStr = `"${name.replace(/"/g, '\\"')}" in:name type:user`;
const q = `query($q:String!){ search(type: USER, query: $q, first: 25) { nodes { ... on User { login name } } } }`;
const r = await graphql(q, { q: queryStr });
const nodes = r?.data?.search?.nodes ?? [];
const target = normalizeName(name);
for (const it of nodes) {
if (!it?.login) continue;
if (normalizeName(it.name || "") === target) return it.login;
}
return "";
}
async function searchUsersByLoginToken(token) {
if (!TOKEN) return "";
const q = `query($q:String!){ search(type: USER, query: $q, first: 5) { nodes { ... on User { login name } } } }`;
const r = await graphql(q, { q: `${token} in:login type:user` });
const items = r?.data?.search?.nodes ?? [];
if (items.length === 1) return items[0]?.login || "";
return "";
}
async function fetchProfileNames(logins) {
const out = new Map();
const chunkSize = 40;
for (let i = 0; i < logins.length; i += chunkSize) {
const chunk = logins.slice(i, i + chunkSize);
const fields = chunk.map((login, idx) => `u${idx}: user(login: "${login}") { login name }`).join("\n");
const q = `query { ${fields} }`;
const r = await graphql(q);
const data = r?.data || {};
for (let idx = 0; idx < chunk.length; idx++) {
const u = data[`u${idx}`];
out.set(chunk[idx], (u?.name || "").trim());
}
}
return out;
}
// ---------- main
async function main() {
const { range, paths } = parseArgs();
const shas = revList(range, paths);
if (!shas.length) die("No commits in the specified range/path.");
// 1) Commit info + primary author counts
const { commitInfo, authorCount } = await fetchCommitsByOidBatch(shas);
// 2) Collect authors and co-authors
const loginBestName = new Map(); // login -> name hint
const pool = []; // [{ name, email }] to resolve (co-authors + primaries with missing login)
for (const sha of shas) {
const info = commitInfo.get(sha);
if (!info) continue;
const { login, name, email, message } = info;
if (login) {
loginBestName.set(login, pickBetterName(loginBestName.get(login) || "", name));
} else {
const guess = loginFromNoreply(email);
if (guess) loginBestName.set(guess, pickBetterName(loginBestName.get(guess) || "", name));
else pool.push({ name, email });
}
for (const ca of parseCoAuthorLines(message)) pool.push(ca);
}
// 3) Resolve pool (GraphQL users search only)
const emailToLogin = new Map(); // emailLower -> login
const concurrency = 8;
let idx = 0;
async function worker() {
while (idx < pool.length) {
const i = idx++;
const { name, email } = pool[i];
const ekey = toLower(email);
if (emailToLogin.has(ekey)) continue;
let login = loginFromNoreply(email);
if (!login) login = await searchUsersByNameExact(name);
if (!login) {
const cands = candidateHandlesFromEmailAndName(email, name);
for (const cand of cands) {
const solo = await searchUsersByLoginToken(cand);
if (solo) { login = solo; break; }
}
}
if (!login && DEBUG) logd(`Unresolved: "${name}" <${email}>`);
emailToLogin.set(ekey, login || "");
if (login) loginBestName.set(login, pickBetterName(loginBestName.get(login) || "", name));
if (i % 10 === 0) await sleep(60);
}
}
await Promise.all(Array.from({ length: concurrency }, worker));
// 4) Build candidate rows (resolved only), fetch profile names
const resolvedLogins = Array.from(loginBestName.keys());
const profileNames = await fetchProfileNames(resolvedLogins);
const candidates = resolvedLogins.map(login => {
const prof = (profileNames.get(login) || "").trim();
const hint = (loginBestName.get(login) || "").trim();
const display = sanitizeDisplayName(prof || hint || login, prof || login);
return { login, display, authorCommits: authorCount.get(login) || 0 };
});
// 5) Collapse duplicate people with the same display name
const byDisplay = new Map(); // normName -> best candidate
const score = (x) => (x.authorCommits > 0 ? 2 : 0) + (x.display.toLowerCase() !== x.login.toLowerCase() ? 1 : 0);
for (const c of candidates) {
const key = normalizeName(c.display);
if (!byDisplay.has(key)) { byDisplay.set(key, c); continue; }
const cur = byDisplay.get(key);
if (score(c) > score(cur) || (score(c) === score(cur) && c.login.toLowerCase() < cur.login.toLowerCase())) {
if (DEBUG) logd(`Collapsed duplicate "${c.display}": keeping ${c.login} over ${cur.login}`);
byDisplay.set(key, c);
}
}
const resolvedRows = Array.from(byDisplay.values())
.filter((v, i, arr) => arr.findIndex(x => x.login.toLowerCase() === v.login.toLowerCase()) === i)
.map(({ display, login }) => ({ name: display, gh: `[@${login}](https://github.com/${login})`, login }));
// 6) Unmatched → show email (dedupe by name+email)
const unmatched = [];
const seenUnk = new Set();
for (const { name, email } of pool) {
const login = emailToLogin.get(toLower(email));
if (login) continue;
const nm = sanitizeDisplayName(name || "(Unknown)", name || "(Unknown)");
const key = normalizeName(nm) + "|" + email.toLowerCase();
if (seenUnk.has(key)) continue;
seenUnk.add(key);
unmatched.push({ name: nm, gh: email, login: "" });
}
// 7) Merge, sort, output
const allRows = [...resolvedRows, ...unmatched];
allRows.sort((a, b) => cmp(a.name, b.name));
console.log("| Author | Github");
console.log("| ----------------------------- | ---------------------------------");
for (const r of allRows) {
console.log(`| ${r.name} | ${r.gh}`);
}
}
main().catch((e) => die(String(e)));
================================================
FILE: scripts/update-appendix-specified-definitions.mjs
================================================
import prettier from "prettier";
import { writeFile } from "node:fs/promises";
import {
printIntrospectionSchema,
buildSchema,
specifiedScalarTypes,
printType,
parse,
print,
visit,
Kind
} from "graphql";
function stripDescriptions(sdl) {
const ast = parse(sdl);
const noDescAst = visit(ast, {
enter: (node) => {
// Not in spec yet
if (node.name?.value === "FRAGMENT_VARIABLE_DEFINITION") {
return null
}
},
leave: (node) => ({ ...node, description: undefined })
});
return print(noDescAst);
}
function printSpecifiedScalars() {
return specifiedScalarTypes
.map((type) => stripDescriptions(printType(type)))
.join("\n\n");
}
const introspectionSchema = stripDescriptions(
printIntrospectionSchema(buildSchema(`type Query { i: Int }`))
);
const allSpecifiedTypesSDL = prettier
.format(printSpecifiedScalars() + "\n\n" + introspectionSchema, {
parser: "graphql"
})
.trimEnd();
await writeFile(
"./spec/Appendix D -- Specified Definitions.md",
`# D. Appendix: Type System Definitions
This appendix lists all type system definitions specified in this document.
The order of types, fields, arguments, values and directives is non-normative.
\`\`\`graphql
${allSpecifiedTypesSDL}
\`\`\`
`
);
================================================
FILE: signed-agreements/README.md
================================================
# Legacy Signed Agreements
The signed agreement found in this directory was contributed when this
specification was licensed under OWFa 1.0 on September 26, 2017 by Facebook.
Since then the [GraphQL Foundation](https://graphql.org/foundation/) was formed
in 2019, at which point the GraphQL Specification Project became a
[Joint Development Foundation](https://www.jointdevelopment.org/) project.
The charter and legal documents currently governing this specification and other
GraphQL projects can be found in the
[GraphQL Foundation repository](https://github.com/graphql/foundation).
================================================
FILE: spec/Appendix A -- Conformance.md
================================================
# A. Appendix: Conformance
A conforming implementation of GraphQL must fulfill all normative requirements.
Conformance requirements are described in this document via both descriptive
assertions and key words with clearly defined meanings.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative portions of
this document are to be interpreted as described in
[IETF RFC 2119](https://tools.ietf.org/html/rfc2119). These key words may appear
in lowercase and still retain their meaning unless explicitly declared as
non-normative.
A conforming implementation of GraphQL may provide additional functionality, but
must not do so where explicitly disallowed or where it would otherwise result in
non-conformance.
**Conforming Algorithms**
Algorithm steps phrased in imperative grammar (e.g. "Return the result of
calling resolver") are to be interpreted with the same level of requirement as
the algorithm it is contained within. Any algorithm referenced within an
algorithm step (e.g. "Let completedResult be the result of calling
CompleteValue()") is to be interpreted as having at least the same level of
requirement as the algorithm containing that step.
Conformance requirements expressed as algorithms and data collections can be
fulfilled by an implementation of this specification in any way as long as the
perceived result is equivalent. Algorithms described in this document are
written to be easy to understand. Implementers are encouraged to include
equivalent but optimized implementations.
See [Appendix A](#sec-Appendix-Notation-Conventions) for more details about the
definition of algorithms, data collections, and other notational conventions
used in this document.
**Non-Normative Portions**
All contents of this document are normative except portions explicitly declared
as non-normative.
Examples in this document are non-normative, and are presented to aid
understanding of introduced concepts and the behavior of normative portions of
the specification. Examples are either introduced explicitly in prose (e.g. "for
example") or are set apart in example or counterexample blocks, like this:
```example
This is an example of a non-normative example.
```
```counter-example
This is an example of a non-normative counterexample.
```
Notes in this document are non-normative, and are presented to clarify intent,
draw attention to potential edge cases and pitfalls, and answer common questions
that arise during implementation. Notes are either introduced explicitly in
prose (e.g. "Note: ") or are set apart in a note block, like this:
Note: This is an example of a non-normative note.
================================================
FILE: spec/Appendix B -- Notation Conventions.md
================================================
# B. Appendix: Notation Conventions
This specification document contains a number of notation conventions used to
describe technical concepts such as language grammar and semantics as well as
runtime algorithms.
This appendix seeks to explain these notations in greater detail to avoid
ambiguity.
## Context-Free Grammar
A context-free grammar consists of a number of productions. Each production has
an abstract symbol called a "non-terminal" as its left-hand side, and zero or
more possible sequences of non-terminal symbols and/or terminal characters as
its right-hand side.
Starting from a single goal non-terminal symbol, a context-free grammar
describes a language: the set of possible sequences of characters that can be
described by repeatedly replacing any non-terminal in the goal sequence with one
of the sequences it is defined by, until all non-terminal symbols have been
replaced by terminal characters.
Terminals are represented in this document in a monospace font in two forms: a
specific Unicode character or sequence of Unicode characters (i.e. {`=`} or
{`terminal`}), and prose typically describing a specific Unicode code point
{"Space (U+0020)"}. Sequences of Unicode characters only appear in syntactic
grammars and represent a {Name} token of that specific sequence.
Non-terminal production rules are represented in this document using the
following notation for a non-terminal with a single definition:
NonTerminalWithSingleDefinition : NonTerminal `terminal`
While using the following notation for a production with a list of definitions:
NonTerminalWithManyDefinitions :
- OtherNonTerminal `terminal`
- `terminal`
A definition may refer to itself, which describes repetitive sequences, for
example:
ListOfLetterA :
- ListOfLetterA `a`
- `a`
## Lexical and Syntactic Grammar
The GraphQL language is defined in a syntactic grammar where terminal symbols
are tokens. Tokens are defined in a lexical grammar which matches patterns of
source characters. The result of parsing a source text sequence of Unicode
characters first produces a sequence of lexical tokens according to the lexical
grammar which then produces abstract syntax tree (AST) according to the
syntactic grammar.
A lexical grammar production describes non-terminal "tokens" by patterns of
terminal Unicode characters. No "whitespace" or other ignored characters may
appear between any terminal Unicode characters in the lexical grammar
production. A lexical grammar production is distinguished by a two colon `::`
definition.
Word :: Letter+
A Syntactic grammar production describes non-terminal "rules" by patterns of
terminal Tokens. {Whitespace} and other {Ignored} sequences may appear before or
after any terminal {Token}. A syntactic grammar production is distinguished by a
one colon `:` definition.
Sentence : Word+ `.`
## Grammar Notation
This specification uses some additional notation to describe common patterns,
such as optional or repeated patterns, or parameterized alterations of the
definition of a non-terminal. This section explains these shorthand notations
and their expanded definitions in the context-free grammar.
**Constraints**
A grammar production may specify that certain expansions are not permitted by
using the phrase "but not" and then indicating the expansions to be excluded.
For example, the following production means that the non-terminal {SafeWord} may
be replaced by any sequence of characters that could replace {Word} provided
that the same sequence of characters could not replace {SevenCarlinWords}.
SafeWord : Word but not SevenCarlinWords
A grammar may also list a number of restrictions after "but not" separated by
"or".
For example:
NonBooleanName : Name but not `true` or `false`
**Lookahead Restrictions**
A grammar production may specify that certain characters or tokens are not
permitted to follow it by using the pattern {[lookahead != NotAllowed]}.
Lookahead restrictions are often used to remove ambiguity from the grammar.
The following example makes it clear that {Letter+} must be greedy, since {Word}
cannot be followed by yet another {Letter}.
Word :: Letter+ [lookahead != Letter]
**Optionality and Lists**
A subscript suffix "{Symbol?}" is shorthand for two possible sequences, one
including that symbol and one excluding it.
As an example:
Sentence : Noun Verb Adverb?
is shorthand for
Sentence :
- Noun Verb Adverb
- Noun Verb
A subscript suffix "{Symbol+}" is shorthand for a list of one or more of that
symbol, represented as an additional recursive production.
As an example:
Book : Cover Page+ Cover
is shorthand for
Book : Cover Page_list Cover
Page_list :
- Page_list Page
- Page
**Parameterized Grammar Productions**
A symbol definition subscript suffix parameter in braces "{Symbol[Param]}" is
shorthand for two symbol definitions, one appended with that parameter name, the
other without. The same subscript suffix on a symbol is shorthand for that
variant of the definition. If the parameter starts with "?", that form of the
symbol is used if in a symbol definition with the same parameter. Some possible
sequences can be included or excluded conditionally when respectively prefixed
with "\[+Param]" and "\[~Param]".
As an example:
Example[Param] :
- A
- B[Param]
- C[?Param]
- [+Param] D
- [~Param] E
is shorthand for
Example :
- A
- B_param
- C
- E
Example_param :
- A
- B_param
- C_param
- D
## Grammar Semantics
This specification describes the semantic value of many grammar productions in
the form of a list of algorithmic steps.
For example, this describes how a parser should interpret a string literal:
StringValue :: `""`
- Return an empty Unicode character sequence.
StringValue :: `"` StringCharacter+ `"`
- Return the Unicode character sequence of all {StringCharacter} Unicode
character values.
## Algorithms
This specification describes some algorithms used by the static and runtime
semantics, they're defined in the form of a function-like syntax with the
algorithm's name and the arguments it accepts along with a list of algorithmic
steps to take in the order listed. Each step may establish references to other
values, check various conditions, call other algorithms, and eventually return a
value representing the outcome of the algorithm for the provided arguments.
For example, the following example describes an algorithm named {Fibonacci}
which accepts a single argument {number}. The algorithm's steps produce the next
number in the Fibonacci sequence:
Fibonacci(number):
- If {number} is {0}:
- Return {1}.
- If {number} is {1}:
- Return {2}.
- Let {previousNumber} be {number} - {1}.
- Let {previousPreviousNumber} be {number} - {2}.
- Return {Fibonacci(previousNumber)} + {Fibonacci(previousPreviousNumber)}.
Note: Algorithms described in this document are written to be easy to
understand. Implementers are encouraged to include observably equivalent but
optimized implementations.
## Data Collections
Algorithms within this specification refer to abstract data collection types to
express normative structural, uniqueness, and ordering requirements. Temporary
data collections internal to an algorithm use these types to best describe
expected behavior, but implementers are encouraged to provide observably
equivalent but optimized implementations. Implementations may use any data
structure as long as the expected requirements are met.
**List**
:: A _list_ is an ordered collection of values which may contain duplicates. A
value added to a list is ordered after existing values.
**Set**
:: A _set_ is a collection of values which must not contain duplicates.
:: An _ordered set_ is a set which has a defined order. A value added to an
ordered set, which does not already contain that value, is ordered after
existing values.
**Map**
:: A _map_ is a collection of entries, each of which has a key and value. Each
entry has a unique key, and can be directly referenced by that key.
:: An _ordered map_ is a map which has a defined order. An entry added to an
ordered map, which does not have an entry with that key, is ordered after
existing entries.
Note: This specification defines ordered data collection only when strictly
required. When an order is observable, implementations should preserve it to
improve output legibility and stability. For example if applying a grammar to an
input string yields a _set_ of elements, serialization should emit those
elements in the same source order.
================================================
FILE: spec/Appendix C -- Grammar Summary.md
================================================
# C. Appendix: Grammar Summary
## Source Text
SourceCharacter :: "Any Unicode scalar value"
## Ignored Tokens
Ignored ::
- UnicodeBOM
- Whitespace
- LineTerminator
- Comment
- Comma
UnicodeBOM :: "Byte Order Mark (U+FEFF)"
Whitespace ::
- "Horizontal Tab (U+0009)"
- "Space (U+0020)"
LineTerminator ::
- "New Line (U+000A)"
- "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
- "Carriage Return (U+000D)" "New Line (U+000A)"
Comment :: `#` CommentChar\* [lookahead != CommentChar]
CommentChar :: SourceCharacter but not LineTerminator
Comma :: ,
## Lexical Tokens
Token ::
- Punctuator
- Name
- IntValue
- FloatValue
- StringValue
Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
Name ::
- NameStart NameContinue\* [lookahead != NameContinue]
NameStart ::
- Letter
- `_`
NameContinue ::
- Letter
- Digit
- `_`
Letter :: one of
- `A` `B` `C` `D` `E` `F` `G` `H` `I` `J` `K` `L` `M`
- `N` `O` `P` `Q` `R` `S` `T` `U` `V` `W` `X` `Y` `Z`
- `a` `b` `c` `d` `e` `f` `g` `h` `i` `j` `k` `l` `m`
- `n` `o` `p` `q` `r` `s` `t` `u` `v` `w` `x` `y` `z`
Digit :: one of
- `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
IntValue :: IntegerPart [lookahead != {Digit, `.`, NameStart}]
IntegerPart ::
- NegativeSign? 0
- NegativeSign? NonZeroDigit Digit\*
NegativeSign :: -
NonZeroDigit :: Digit but not `0`
FloatValue ::
- IntegerPart FractionalPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
- IntegerPart FractionalPart [lookahead != {Digit, `.`, NameStart}]
- IntegerPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
FractionalPart :: . Digit+
ExponentPart :: ExponentIndicator Sign? Digit+
ExponentIndicator :: one of `e` `E`
Sign :: one of + -
StringValue ::
- `""` [lookahead != `"`]
- `"` StringCharacter+ `"`
- BlockString
StringCharacter ::
- SourceCharacter but not `"` or `\` or LineTerminator
- `\u` EscapedUnicode
- `\` EscapedCharacter
EscapedUnicode ::
- `{` HexDigit+ `}`
- HexDigit HexDigit HexDigit HexDigit
HexDigit :: one of
- `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
- `A` `B` `C` `D` `E` `F`
- `a` `b` `c` `d` `e` `f`
EscapedCharacter :: one of `"` `\` `/` `b` `f` `n` `r` `t`
BlockString :: `"""` BlockStringCharacter\* `"""`
BlockStringCharacter ::
- SourceCharacter but not `"""` or `\"""`
- `\"""`
Note: Block string values are interpreted to exclude blank initial and trailing
lines and uniform indentation with {BlockStringValue()}.
## Document Syntax
Description : StringValue
Document : Definition+
Definition :
- ExecutableDefinition
- TypeSystemDefinitionOrExtension
ExecutableDocument : ExecutableDefinition+
ExecutableDefinition :
- OperationDefinition
- FragmentDefinition
OperationDefinition :
- Description? OperationType Name? VariablesDefinition? Directives? SelectionSet
- SelectionSet
OperationType : one of `query` `mutation` `subscription`
SelectionSet : { Selection+ }
Selection :
- Field
- FragmentSpread
- InlineFragment
Field : Alias? Name Arguments? Directives? SelectionSet?
Alias : Name :
Arguments[Const] : ( Argument[?Const]+ )
Argument[Const] : Name : Value[?Const]
FragmentSpread : ... FragmentName Directives?
InlineFragment : ... TypeCondition? Directives? SelectionSet
FragmentDefinition : Description? fragment FragmentName TypeCondition
Directives? SelectionSet
FragmentName : Name but not `on`
TypeCondition : on NamedType
Value[Const] :
- [~Const] Variable
- IntValue
- FloatValue
- StringValue
- BooleanValue
- NullValue
- EnumValue
- ListValue[?Const]
- ObjectValue[?Const]
BooleanValue : one of `true` `false`
NullValue : `null`
EnumValue : Name but not `true`, `false` or `null`
ListValue[Const] :
- [ ]
- [ Value[?Const]+ ]
ObjectValue[Const] :
- { }
- { ObjectField[?Const]+ }
ObjectField[Const] : Name : Value[?Const]
VariablesDefinition : ( VariableDefinition+ )
VariableDefinition : Description? Variable : Type DefaultValue?
Directives[Const]?
Variable : $ Name
DefaultValue : = Value[Const]
Type :
- NamedType
- ListType
- NonNullType
NamedType : Name
ListType : [ Type ]
NonNullType :
- NamedType !
- ListType !
Directives[Const] : Directive[?Const]+
Directive[Const] : @ Name Arguments[?Const]?
TypeSystemDocument : TypeSystemDefinition+
TypeSystemDefinition :
- SchemaDefinition
- TypeDefinition
- DirectiveDefinition
TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+
TypeSystemDefinitionOrExtension :
- TypeSystemDefinition
- TypeSystemExtension
TypeSystemExtension :
- SchemaExtension
- TypeExtension
SchemaDefinition : Description? schema Directives[Const]? {
RootOperationTypeDefinition+ }
SchemaExtension :
- extend schema Directives[Const]? { RootOperationTypeDefinition+ }
- extend schema Directives[Const] [lookahead != `{`]
RootOperationTypeDefinition : OperationType : NamedType
TypeDefinition :
- ScalarTypeDefinition
- ObjectTypeDefinition
- InterfaceTypeDefinition
- UnionTypeDefinition
- EnumTypeDefinition
- InputObjectTypeDefinition
TypeExtension :
- ScalarTypeExtension
- ObjectTypeExtension
- InterfaceTypeExtension
- UnionTypeExtension
- EnumTypeExtension
- InputObjectTypeExtension
ScalarTypeDefinition : Description? scalar Name Directives[Const]?
ScalarTypeExtension :
- extend scalar Name Directives[Const]
ObjectTypeDefinition :
- Description? type Name ImplementsInterfaces? Directives[Const]?
FieldsDefinition
- Description? type Name ImplementsInterfaces? Directives[Const]? [lookahead !=
`{`]
ObjectTypeExtension :
- extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
- extend type Name ImplementsInterfaces? Directives[Const] [lookahead != `{`]
- extend type Name ImplementsInterfaces [lookahead != `{`]
ImplementsInterfaces :
- ImplementsInterfaces & NamedType
- implements `&`? NamedType
FieldsDefinition : { FieldDefinition+ }
FieldDefinition : Description? Name ArgumentsDefinition? : Type
Directives[Const]?
ArgumentsDefinition : ( InputValueDefinition+ )
InputValueDefinition : Description? Name : Type DefaultValue? Directives[Const]?
InterfaceTypeDefinition :
- Description? interface Name ImplementsInterfaces? Directives[Const]?
FieldsDefinition
- Description? interface Name ImplementsInterfaces? Directives[Const]?
[lookahead != `{`]
InterfaceTypeExtension :
- extend interface Name ImplementsInterfaces? Directives[Const]?
FieldsDefinition
- extend interface Name ImplementsInterfaces? Directives[Const] [lookahead !=
`{`]
- extend interface Name ImplementsInterfaces [lookahead != `{`]
UnionTypeDefinition : Description? union Name Directives[Const]?
UnionMemberTypes?
UnionMemberTypes :
- UnionMemberTypes | NamedType
- = `|`? NamedType
UnionTypeExtension :
- extend union Name Directives[Const]? UnionMemberTypes
- extend union Name Directives[Const]
EnumTypeDefinition :
- Description? enum Name Directives[Const]? EnumValuesDefinition
- Description? enum Name Directives[Const]? [lookahead != `{`]
EnumValuesDefinition : { EnumValueDefinition+ }
EnumValueDefinition : Description? EnumValue Directives[Const]?
EnumTypeExtension :
- extend enum Name Directives[Const]? EnumValuesDefinition
- extend enum Name Directives[Const] [lookahead != `{`]
InputObjectTypeDefinition :
- Description? input Name Directives[Const]? InputFieldsDefinition
- Description? input Name Directives[Const]? [lookahead != `{`]
InputFieldsDefinition : { InputValueDefinition+ }
InputObjectTypeExtension :
- extend input Name Directives[Const]? InputFieldsDefinition
- extend input Name Directives[Const] [lookahead != `{`]
DirectiveDefinition : Description? directive @ Name ArgumentsDefinition?
`repeatable`? on DirectiveLocations
DirectiveLocations :
- DirectiveLocations | DirectiveLocation
- `|`? DirectiveLocation
DirectiveLocation :
- ExecutableDirectiveLocation
- TypeSystemDirectiveLocation
ExecutableDirectiveLocation : one of
- `QUERY`
- `MUTATION`
- `SUBSCRIPTION`
- `FIELD`
- `FRAGMENT_DEFINITION`
- `FRAGMENT_SPREAD`
- `INLINE_FRAGMENT`
- `VARIABLE_DEFINITION`
TypeSystemDirectiveLocation : one of
- `SCHEMA`
- `SCALAR`
- `OBJECT`
- `FIELD_DEFINITION`
- `ARGUMENT_DEFINITION`
- `INTERFACE`
- `UNION`
- `ENUM`
- `ENUM_VALUE`
- `INPUT_OBJECT`
- `INPUT_FIELD_DEFINITION`
## Schema Coordinate Syntax
Note: Schema coordinates must not contain {Ignored}.
SchemaCoordinateToken ::
- SchemaCoordinatePunctuator
- Name
SchemaCoordinatePunctuator :: one of ( ) . : @
SchemaCoordinate ::
- TypeCoordinate
- MemberCoordinate
- ArgumentCoordinate
- DirectiveCoordinate
- DirectiveArgumentCoordinate
TypeCoordinate :: Name
MemberCoordinate :: Name . Name
ArgumentCoordinate :: Name . Name ( Name : )
DirectiveCoordinate :: @ Name
DirectiveArgumentCoordinate :: @ Name ( Name : )
================================================
FILE: spec/Appendix D -- Specified Definitions.md
================================================
# D. Appendix: Type System Definitions
This appendix lists all type system definitions specified in this document.
The order of types, fields, arguments, values and directives is non-normative.
```graphql
scalar String
scalar Int
scalar Float
scalar Boolean
scalar ID
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
directive @deprecated(
reason: String! = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE
directive @specifiedBy(url: String!) on SCALAR
directive @oneOf on INPUT_OBJECT
type __Schema {
description: String
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}
type __Type {
kind: __TypeKind!
name: String
description: String
specifiedByURL: String
fields(includeDeprecated: Boolean! = false): [__Field!]
interfaces: [__Type!]
possibleTypes: [__Type!]
enumValues(includeDeprecated: Boolean! = false): [__EnumValue!]
inputFields(includeDeprecated: Boolean! = false): [__InputValue!]
ofType: __Type
isOneOf: Boolean
}
enum __TypeKind {
SCALAR
OBJECT
INTERFACE
UNION
ENUM
INPUT_OBJECT
LIST
NON_NULL
}
type __Field {
name: String!
description: String
args(includeDeprecated: Boolean! = false): [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
}
type __InputValue {
name: String!
description: String
type: __Type!
defaultValue: String
isDeprecated: Boolean!
deprecationReason: String
}
type __EnumValue {
name: String!
description: String
isDeprecated: Boolean!
deprecationReason: String
}
type __Directive {
name: String!
description: String
isRepeatable: Boolean!
locations: [__DirectiveLocation!]!
args(includeDeprecated: Boolean! = false): [__InputValue!]!
}
enum __DirectiveLocation {
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
VARIABLE_DEFINITION
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
}
```
================================================
FILE: spec/GraphQL.md
================================================
# GraphQL
_Current Working Draft_
**Introduction**
This is the specification for GraphQL, a query language and execution engine for
describing and performing the capabilities and requirements of data models for
client-server applications.
A conforming implementation of GraphQL must fulfill all normative requirements
described in this specification (see [Conformance](#sec-Appendix-Conformance)).
The GraphQL specification is provided under the OWFa 1.0 license (see
[Copyright and Licensing](#sec-Appendix-Copyright-and-Licensing)).
GraphQL was originally created in 2012 and the development of this open standard
started in 2015. It is a deliverable of the
[GraphQL Specification Project](https://graphql.org/community/), established in
2019 with the [Joint Development Foundation](https://www.jointdevelopment.org/).
The [GraphQL Foundation](https://graphql.org/foundation/) was formed in 2019 as
a neutral focal point for organizations who support development of the GraphQL
ecosystem. If your organization benefits from GraphQL, please consider
[becoming a member](https://graphql.org/foundation/join/#graphql-foundation).
This specification is developed on GitHub at
[graphql/graphql-spec](https://github.com/graphql/graphql-spec/). Contributions
are managed by the
[GraphQL Working Group](https://github.com/graphql/graphql-wg), hosted by the
[GraphQL Technical Steering Committee](https://github.com/graphql/graphql-wg/blob/main/GraphQL-TSC.md).
To learn more see the
[contribution guide](https://github.com/graphql/graphql-spec/blob/main/CONTRIBUTING.md).
GraphQL has evolved and may continue to evolve in future editions of this
specification. Previous editions of the GraphQL specification can be found at
permalinks that match their
[release tag](https://github.com/graphql/graphql-spec/releases). The latest
working draft release can be found at
[https://spec.graphql.org/draft](https://spec.graphql.org/draft).
# [Overview](Section%201%20--%20Overview.md)
# [Language](Section%202%20--%20Language.md)
# [Type System](Section%203%20--%20Type%20System.md)
# [Introspection](Section%204%20--%20Introspection.md)
# [Validation](Section%205%20--%20Validation.md)
# [Execution](Section%206%20--%20Execution.md)
# [Response](Section%207%20--%20Response.md)
# [Appendix: Conformance](Appendix%20A%20--%20Conformance.md)
# [Appendix: Notation Conventions](Appendix%20B%20--%20Notation%20Conventions.md)
# [Appendix: Grammar Summary](Appendix%20C%20--%20Grammar%20Summary.md)
# [Appendix: Specified Definitions](Appendix%20D%20--%20Specified%20Definitions.md)
# [Appendix: Licensing](../LICENSE.md)
================================================
FILE: spec/Section 1 -- Overview.md
================================================
# Overview
GraphQL is a query language designed to build client applications by providing
an intuitive and flexible syntax and system for describing their data
requirements and interactions.
For example, this GraphQL _request_ will receive the name of the user with id 4
from the Facebook implementation of GraphQL.
```graphql example
{
user(id: 4) {
name
}
}
```
Which produces the resulting data (in JSON):
```json example
{
"user": {
"name": "Mark Zuckerberg"
}
}
```
GraphQL is not a programming language capable of arbitrary computation, but is
instead a language used to make requests to application services that have
capabilities defined in this specification. GraphQL does not mandate a
particular programming language or storage system for application services that
implement it. Instead, application services take their capabilities and map them
to a uniform language, type system, and philosophy that GraphQL encodes. This
provides a unified interface friendly to product development and a powerful
platform for tool-building.
GraphQL has a number of design principles:
- **Product-centric**: GraphQL is unapologetically driven by the requirements of
views and the front-end engineers that write them. GraphQL starts with their
way of thinking and requirements and builds the language and runtime necessary
to enable that.
- **Hierarchical**: Most product development today involves the creation and
manipulation of view hierarchies. To achieve congruence with the structure of
these applications, a GraphQL request itself is structured hierarchically. The
request is shaped just like the data in its response. It is a natural way for
clients to describe data requirements.
- **Strong-typing**: Every GraphQL service defines an application-specific type
system. Requests are executed within the context of that type system. Given a
GraphQL operation, tools can ensure that it is both syntactically correct and
valid within that type system before execution, i.e. at development time, and
the service can make certain guarantees about the shape and nature of the
response.
- **Client-specified response**: Through its type system, a GraphQL service
publishes the capabilities that its clients are allowed to consume. It is the
client that is responsible for specifying exactly how it will consume those
published capabilities. These requests are specified at field-level
granularity. In the majority of client-server applications written without
GraphQL, the service determines the shape of data returned from its various
endpoints. A GraphQL response, on the other hand, contains exactly what a
client asks for and no more.
- **Self-describing**: GraphQL is self-describing and introspective. A GraphQL
service's type system can be queryable by the GraphQL language itself, which
includes readable documentation. GraphQL introspection serves as a powerful
platform for building common developer tools and client software libraries.
Because of these principles, GraphQL is a powerful and productive environment
for building client applications. Product developers and designers building
applications against working GraphQL services—supported with quality tools—can
quickly become productive without reading extensive documentation and with
little or no formal training. To enable that experience, there must be those
that build those services and tools.
The following formal specification serves as a reference for those builders. It
describes the language and its grammar, the type system and the introspection
system used to query it, and the execution and validation engines with the
algorithms to power them. The goal of this specification is to provide a
foundation and framework for an ecosystem of GraphQL tools, client libraries,
and service implementations—spanning both organizations and platforms—that has
yet to be built. We look forward to working with the community in order to do
that.
================================================
FILE: spec/Section 2 -- Language.md
================================================
# Language
Clients use the GraphQL query language to make requests to a GraphQL service. We
refer to these _request_ sources as documents. A document may contain operations
(queries, mutations, and subscriptions) as well as fragments, a common unit of
composition allowing for data requirement reuse.
A GraphQL document is defined as a syntactic grammar where terminal symbols are
tokens (indivisible lexical units). These tokens are defined in a lexical
grammar which matches patterns of source characters. In this document, syntactic
grammar productions are distinguished with a colon `:` while lexical grammar
productions are distinguished with a double-colon `::`.
The source text of a GraphQL document must be a sequence of {SourceCharacter}.
The character sequence must be described by a sequence of {Token} and {Ignored}
lexical grammars. The lexical token sequence, omitting {Ignored}, must be
described by a single {Document} syntactic grammar.
Note: See [Appendix A](#sec-Appendix-Notation-Conventions) for more information
about the lexical and syntactic grammar and other notational conventions used
throughout this document.
**Lexical Analysis & Syntactic Parse**
The source text of a GraphQL document is first converted into a sequence of
lexical tokens ({Token}) and ignored tokens ({Ignored}). The source text is
scanned from left to right, repeatedly taking the next possible sequence of code
points allowed by the lexical grammar productions as the next token. This
sequence of lexical tokens is then scanned from left to right to produce an
abstract syntax tree (AST) according to the {Document} syntactic grammar.
Lexical grammar productions in this document use _lookahead restrictions_ to
remove ambiguity and ensure a single valid lexical analysis. A lexical token is
only valid if not followed by a character in its lookahead restriction.
For example, an {IntValue} has the restriction {[lookahead != Digit]}, so cannot
be followed by a {Digit}. Because of this, the sequence {`123`} cannot represent
the tokens ({`12`}, {`3`}) since {`12`} is followed by the {Digit} {`3`} and so
must only represent a single token. Use {Whitespace} or other {Ignored} between
characters to represent multiple tokens.
Note: This typically has the same behavior as a
"[maximal munch](https://en.wikipedia.org/wiki/Maximal_munch)" longest possible
match, however some lookahead restrictions include additional constraints.
## Source Text
SourceCharacter :: "Any Unicode scalar value"
GraphQL documents are interpreted from a source text, which is a sequence of
{SourceCharacter}, each {SourceCharacter} being a _Unicode scalar value_ which
may be any Unicode code point from U+0000 to U+D7FF or U+E000 to U+10FFFF
(informally referred to as _"characters"_ through most of this specification).
A GraphQL document may be expressed only in the ASCII range to be as widely
compatible with as many existing tools, languages, and serialization formats as
possible and avoid display issues in text editors and source control. Non-ASCII
Unicode scalar values may appear within {StringValue} and {Comment}.
Note: An implementation which uses _UTF-16_ to represent GraphQL documents in
memory (for example, JavaScript or Java) may encounter a _surrogate pair_. This
encodes one _supplementary code point_ and is a single valid source character,
however an unpaired _surrogate code point_ is not a valid source character.
### White Space
Whitespace ::
- "Horizontal Tab (U+0009)"
- "Space (U+0020)"
Whitespace is used to improve legibility of source text and separates other
tokens. Any amount of whitespace may appear before or after any token.
Whitespace between tokens is not significant to the semantic meaning of a
GraphQL document, however whitespace characters may appear within a {String} or
{Comment} token.
Note: GraphQL intentionally does not consider Unicode "Zs" category characters
as whitespace, avoiding misinterpretation by text editors and source control
tools.
### Line Terminators
LineTerminator ::
- "New Line (U+000A)"
- "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
- "Carriage Return (U+000D)" "New Line (U+000A)"
Like whitespace, line terminators are used to improve the legibility of source
text and separate lexical tokens, any amount may appear before or after any
other token and have no significance to the semantic meaning of a GraphQL
Document.
Note: Any error reporting which provides the line number in the source of the
offending syntax should use the preceding amount of {LineTerminator} to produce
the line number.
### Comments
Comment :: `#` CommentChar\* [lookahead != CommentChar]
CommentChar :: SourceCharacter but not LineTerminator
GraphQL source documents may contain single-line comments, starting with the
{`#`} marker.
A comment may contain any {SourceCharacter} except {LineTerminator} so a comment
always consists of all {SourceCharacter} starting with the {`#`} character up to
but not including the {LineTerminator} (or end of the source).
Comments are {Ignored} like whitespace and may appear after any token, or before
a {LineTerminator}, and have no significance to the semantic meaning of a
GraphQL document.
### Insignificant Commas
Comma :: ,
Similar to whitespace and line terminators, commas ({`,`}) are used to improve
the legibility of source text and separate lexical tokens but are otherwise
syntactically and semantically insignificant within GraphQL documents.
Non-significant comma characters ensure that the absence or presence of a comma
does not meaningfully alter the interpreted syntax of the document, as this can
be a common user error in other languages. It also allows for the stylistic use
of either trailing commas or line terminators as list delimiters which are both
often desired for legibility and maintainability of source code.
### Lexical Tokens
Token ::
- Punctuator
- Name
- IntValue
- FloatValue
- StringValue
A GraphQL document is composed of several kinds of indivisible lexical tokens
defined here in a lexical grammar by patterns of source Unicode characters.
Lexical tokens may be separated by {Ignored} tokens.
Tokens are later used as terminal symbols in GraphQL syntactic grammar rules.
### Ignored Tokens
Ignored ::
- UnicodeBOM
- Whitespace
- LineTerminator
- Comment
- Comma
{Ignored} tokens are used to improve readability and provide separation between
lexical tokens, but are otherwise insignificant and not referenced in syntactic
grammar productions.
Any amount of {Ignored} may appear before and after every lexical token. No
ignored regions of a source document are significant, however {SourceCharacter}
which appear in {Ignored} may also appear within a lexical {Token} in a
significant way, for example a {StringValue} may contain whitespace characters.
No {Ignored} may appear _within_ a {Token}, for example no whitespace characters
are permitted between the characters defining a {FloatValue}.
**Byte Order Mark**
UnicodeBOM :: "Byte Order Mark (U+FEFF)"
The _Byte Order Mark_ is a special Unicode code point which may appear at the
beginning of a file which programs may use to determine the fact that the text
stream is Unicode, and what specific encoding has been used. As files are often
concatenated, a _Byte Order Mark_ may appear before or after any lexical token
and is {Ignored}.
### Punctuators
Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
GraphQL documents include punctuation in order to describe structure. GraphQL is
a data description language and not a programming language; therefore, GraphQL
lacks the punctuation often used to describe mathematical expressions.
### Names
Name ::
- NameStart NameContinue\* [lookahead != NameContinue]
NameStart ::
- Letter
- `_`
NameContinue ::
- Letter
- Digit
- `_`
Letter :: one of
- `A` `B` `C` `D` `E` `F` `G` `H` `I` `J` `K` `L` `M`
- `N` `O` `P` `Q` `R` `S` `T` `U` `V` `W` `X` `Y` `Z`
- `a` `b` `c` `d` `e` `f` `g` `h` `i` `j` `k` `l` `m`
- `n` `o` `p` `q` `r` `s` `t` `u` `v` `w` `x` `y` `z`
Digit :: one of
- `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
GraphQL documents are full of named things: operations, fields, arguments,
types, directives, fragments, and variables. All names must follow the same
grammatical form.
Names in GraphQL are case-sensitive. That is to say `name`, `Name`, and `NAME`
all refer to different names. Underscores are significant, which means
`other_name` and `othername` are two different names.
A {Name} must not be followed by a {NameContinue}. In other words, a {Name}
token is always the longest possible valid sequence. The source characters
{`a1`} cannot be interpreted as two tokens since {`a`} is followed by the
{NameContinue} {`1`}.
Note: Names in GraphQL are limited to the Latin ASCII subset
of {SourceCharacter} in order to support interoperation with as many other
systems as possible.
**Reserved Names**
Any {Name} within a GraphQL type system must not start with two underscores
{"\_\_"} unless it is part of the [introspection system](#sec-Introspection) as
defined by this specification.
## Descriptions
Description : StringValue
Documentation is a first-class feature of GraphQL by including written
descriptions on all named definitions in executable {Document} and GraphQL type
systems, which is also made available via introspection ensuring the
documentation of a GraphQL service remains consistent with its capabilities (see
[Type System Descriptions](#sec-Type-System-Descriptions)).
GraphQL descriptions are provided as Markdown (as specified by
[CommonMark](https://commonmark.org/)). Description strings (often
{BlockString}) occur immediately before the definition they describe.
Descriptions in GraphQL executable documents are purely for documentation
purposes. They MUST NOT affect the execution, validation, or response of a
GraphQL document. It is safe to remove all descriptions and comments from
executable documents without changing their behavior or results.
This is an example of a well-described operation:
```graphql example
"""
Request the current status of a time machine and its operator.
You can also check the status for a particular year.
**Warning:** certain years may trigger an anomaly in the space-time continuum.
"""
query GetTimeMachineStatus(
"The unique serial number of the time machine to inspect."
$machineId: ID!
"The year to check the status for."
$year: Int
) {
timeMachine(id: $machineId) {
...TimeMachineDetails
status(year: $year)
}
}
"Details about a time machine and its operator."
fragment TimeMachineDetails on TimeMachine {
id
model
lastMaintenance
operator {
name
licenseLevel
}
}
```
## Document
Document : Definition+
Definition :
- ExecutableDefinition
- TypeSystemDefinitionOrExtension
ExecutableDocument : ExecutableDefinition+
ExecutableDefinition :
- OperationDefinition
- FragmentDefinition
A GraphQL document describes a complete file or request string operated on by a
GraphQL service or client. A document contains multiple definitions, either
executable or representative of a GraphQL type system.
Documents are only executable by a GraphQL service if they are
{ExecutableDocument} and contain at least one {OperationDefinition}. A Document
which contains {TypeSystemDefinitionOrExtension} must not be executed; GraphQL
execution services which receive a Document containing these should return a
descriptive error.
GraphQL services which only seek to execute GraphQL requests and not construct a
new GraphQL schema may choose to only permit {ExecutableDocument}.
Documents which do not contain {OperationDefinition} or do contain
{TypeSystemDefinitionOrExtension} may still be parsed and validated to allow
client tools to represent many GraphQL uses which may appear across many
individual files.
If a Document contains only one operation, that operation may be unnamed. If
that operation is a query without variables or directives then it may also be
represented in the shorthand form, omitting both the {`query`} keyword as well
as the operation name. Otherwise, if a GraphQL document contains multiple
operations, each operation must be named. When submitting a Document with
multiple operations to a GraphQL service, the name of the desired operation to
be executed must also be provided.
## Operations
OperationDefinition :
- Description? OperationType Name? VariablesDefinition? Directives? SelectionSet
- SelectionSet
OperationType : one of `query` `mutation` `subscription`
There are three types of operations that GraphQL models:
- query - a read-only fetch.
- mutation - a write followed by a fetch.
- subscription - a long-lived request that fetches data in response to a
sequence of events over time.
Each operation is represented by an optional operation name and a _selection
set_.
For example, this mutation operation might "like" a story and then retrieve the
new number of likes:
```graphql example
"""
Mark story 12345 as "liked"
and return the updated number of likes on the story
"""
mutation {
likeStory(storyID: 12345) {
story {
likeCount
}
}
}
```
**Query Shorthand**
If a document contains only one operation and that operation is a query which
defines no variables and has no directives applied to it then that operation may
be represented in a shorthand form which omits the {`query`} keyword and
operation name.
For example, this unnamed query operation is written via query shorthand.
```graphql example
{
field
}
```
Descriptions are not permitted on query shorthand.
Note: many examples below will use the query shorthand syntax.
## Selection Sets
SelectionSet : { Selection+ }
Selection :
- Field
- FragmentSpread
- InlineFragment
An operation selects the set of information it needs, and will receive exactly
that information and nothing more, avoiding over-fetching and under-fetching
data.
:: A _selection set_ defines an ordered set of selections (fields, fragment
spreads and inline fragments) against an object, union or interface type.
```graphql example
{
id
firstName
lastName
}
```
In this query operation, the `id`, `firstName`, and `lastName` fields form a
_selection set_. Selection sets may also contain fragment references.
## Fields
Field : Alias? Name Arguments? Directives? SelectionSet?
A _selection set_ is primarily composed of fields. A field describes one
discrete piece of information available to request within a selection set.
Some fields describe complex data or relationships to other data. In order to
further explore this data, a field may itself contain a selection set, allowing
for deeply nested requests. All GraphQL operations must specify their selections
down to fields which return scalar values to ensure an unambiguously shaped
response.
For example, this operation selects fields of complex data and relationships
down to scalar values.
```graphql example
{
me {
id
firstName
lastName
birthday {
month
day
}
friends {
name
}
}
}
```
Fields in the top-level _selection set_ of an operation often represent some
information that is globally accessible to your application and its current
viewer. Some typical examples of these top fields include references to a
current logged-in viewer, or accessing certain types of data referenced by a
unique identifier.
```graphql example
# `me` could represent the currently logged in viewer.
{
me {
name
}
}
# `user` represents one of many users in a graph of data, referred to by a
# unique identifier.
{
user(id: 4) {
name
}
}
```
## Arguments
Arguments[Const] : ( Argument[?Const]+ )
Argument[Const] : Name : Value[?Const]
Fields are conceptually functions which return values, and occasionally accept
arguments which alter their behavior. These arguments often map directly to
function arguments within a GraphQL service's implementation.
In this example, we want to query a specific user (requested via the `id`
argument) and their profile picture of a specific `size`:
```graphql example
{
user(id: 4) {
id
name
profilePic(size: 100)
}
}
```
Many arguments can exist for a given field:
```graphql example
{
user(id: 4) {
id
name
profilePic(width: 100, height: 50)
}
}
```
**Arguments Are Unordered**
Arguments may be provided in any syntactic order and maintain identical semantic
meaning.
These two operations are semantically identical:
```graphql example
{
picture(width: 200, height: 100)
}
```
```graphql example
{
picture(height: 100, width: 200)
}
```
## Field Alias
Alias : Name :
:: A _response name_ is the key in the response object which correlates with a
field's result. By default the response name will use the field's name; however,
you can define a different response name by specifying an alias.
In this example, we can fetch two profile pictures of different sizes and ensure
the resulting response object will not have duplicate keys:
```graphql example
{
user(id: 4) {
id
name
smallPic: profilePic(size: 64)
bigPic: profilePic(size: 1024)
}
}
```
which returns the result:
```json example
{
"user": {
"id": 4,
"name": "Mark Zuckerberg",
"smallPic": "https://cdn.site.io/pic-4-64.jpg",
"bigPic": "https://cdn.site.io/pic-4-1024.jpg"
}
}
```
The fields at the top level of an operation can also be given an alias:
```graphql example
{
zuck: user(id: 4) {
id
name
}
}
```
which returns the result:
```json example
{
"zuck": {
"id": 4,
"name": "Mark Zuckerberg"
}
}
```
## Fragments
FragmentSpread : ... FragmentName Directives?
FragmentDefinition : Description? fragment FragmentName TypeCondition
Directives? SelectionSet
FragmentName : Name but not `on`
Fragments are the primary unit of composition in GraphQL.
Each data-consuming component (function, class, UI element, and so on) of a
client application should declare its data needs in a dedicated fragment. These
fragments may then be composed, following the usage of the components
themselves, to form a GraphQL operation to issue to the server.
For example, if we have some logic that requires `id`, `name`, and `profilePic`
to render a profile, and we want to apply that logic to the friends and mutual
friends of a user:
```graphql example
query noFragments {
user(id: 4) {
friends(first: 10) {
id
name
profilePic(size: 50)
}
mutualFriends(first: 10) {
id
name
profilePic(size: 50)
}
}
}
```
The fields required to render a profile can be extracted into a fragment and
composed by a parent fragment or operation.
```graphql example
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendProfile
}
mutualFriends(first: 10) {
...friendProfile
}
}
}
```
```graphql example
"Fields required to render a friend's profile"
fragment friendProfile on User {
id
name
profilePic(size: 50)
}
```
If the profile rendering logic no longer needs `name`, the `name` field can be
removed from the `friendProfile` fragment and it will no longer be fetched in
both locations the fragment is consumed.
Fragments are consumed by using the spread operator (`...`). All fields selected
by the fragment will be added to the field selection at the same level as the
fragment invocation. This happens through multiple levels of fragment spreads.
For example:
```graphql example
query withNestedFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
}
mutualFriends(first: 10) {
...friendFields
}
}
}
fragment friendFields on User {
id
name
...standardProfilePic
}
fragment standardProfilePic on User {
profilePic(size: 50)
}
```
The operations `noFragments`, `withFragments`, and `withNestedFragments` all
produce the same response object.
### Type Conditions
TypeCondition : on NamedType
Fragments must specify the type they apply to. In this example, `friendFields`
can be used in the context of querying a `User`.
Fragments cannot be specified on any input value (scalar, enumeration, or input
object).
Fragments can be specified on object types, interfaces, and unions.
Selections within fragments only return values when the concrete type of the
object it is operating on matches the type of the fragment.
For example in this operation using the Facebook data model:
```graphql example
query FragmentTyping {
profiles(handles: ["zuck", "coca-cola"]) {
handle
...userFragment
...pageFragment
}
}
fragment userFragment on User {
friends {
count
}
}
fragment pageFragment on Page {
likers {
count
}
}
```
The `profiles` root field returns a list where each element could be a `Page` or
a `User`. When the object in the `profiles` result is a `User`, `friends` will
be present and `likers` will not. Conversely when the result is a `Page`,
`likers` will be present and `friends` will not.
```json example
{
"profiles": [
{
"handle": "zuck",
"friends": { "count": 1234 }
},
{
"handle": "coca-cola",
"likers": { "count": 90234512 }
}
]
}
```
### Inline Fragments
InlineFragment : ... TypeCondition? Directives? SelectionSet
Fragments can also be defined inline within a _selection set_. This is useful
for conditionally including fields based on a type condition or applying a
directive to a selection set.
This feature of standard fragment inclusion was demonstrated in the
`query FragmentTyping` example above. We could accomplish the same thing using
inline fragments.
```graphql example
query inlineFragmentTyping {
profiles(handles: ["zuck", "coca-cola"]) {
handle
... on User {
friends {
count
}
}
... on Page {
likers {
count
}
}
}
}
```
Inline fragments may also be used to apply a directive to a group of fields. If
the TypeCondition is omitted, an inline fragment is considered to be of the same
type as the enclosing context.
```graphql example
query inlineFragmentNoType($expandedInfo: Boolean) {
user(handle: "zuck") {
id
name
... @include(if: $expandedInfo) {
firstName
lastName
birthday
}
}
}
```
## Input Values
Value[Const] :
- [~Const] Variable
- IntValue
- FloatValue
- StringValue
- BooleanValue
- NullValue
- EnumValue
- ListValue[?Const]
- ObjectValue[?Const]
Field and directive arguments accept input values of various literal primitives;
input values can be scalars, enumeration values, lists, or input objects.
If not defined as constant (for example, in {DefaultValue}), input values can be
specified as a variable. List and inputs objects may also contain variables
(unless defined to be constant).
### Int Value
IntValue :: IntegerPart [lookahead != {Digit, `.`, NameStart}]
IntegerPart ::
- NegativeSign? 0
- NegativeSign? NonZeroDigit Digit\*
NegativeSign :: -
NonZeroDigit :: Digit but not `0`
An {IntValue} is specified without a decimal point or exponent but may be
negative (e.g. {-123}). It must not have any leading {0}.
An {IntValue} must not be followed by a {Digit}. In other words, an {IntValue}
token is always the longest possible valid sequence. The source characters {12}
cannot be interpreted as two tokens since {1} is followed by the {Digit} {2}.
This also means the source {00} is invalid since it can neither be interpreted
as a single token nor two {0} tokens.
An {IntValue} must not be followed by a {`.`} or {NameStart}. If either {`.`} or
{ExponentIndicator} follows then the token must only be interpreted as a
possible {FloatValue}. No other {NameStart} character can follow. For example
the sequences `0x123` and `123L` have no valid lexical representations.
### Float Value
FloatValue ::
- IntegerPart FractionalPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
- IntegerPart FractionalPart [lookahead != {Digit, `.`, NameStart}]
- IntegerPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
FractionalPart :: . Digit+
ExponentPart :: ExponentIndicator Sign? Digit+
ExponentIndicator :: one of `e` `E`
Sign :: one of + -
A {FloatValue} includes either a decimal point (e.g. {1.0}) or an exponent (e.g.
{1e50}) or both (e.g. {6.0221413e23}) and may be negative. Like {IntValue}, it
also must not have any leading {0}.
A {FloatValue} must not be followed by a {Digit}. In other words, a {FloatValue}
token is always the longest possible valid sequence. The source characters
{1.23} cannot be interpreted as two tokens since {1.2} is followed by the
{Digit} {3}.
A {FloatValue} must not be followed by a {.}. For example, the sequence {1.23.4}
cannot be interpreted as two tokens ({1.2}, {3.4}).
A {FloatValue} must not be followed by a {NameStart}. For example the sequence
`0x1.2p3` has no valid lexical representation.
Note: The numeric literals {IntValue} and {FloatValue} both restrict being
immediately followed by a letter (or other {NameStart}) to reduce confusion or
unexpected behavior since GraphQL only supports decimal numbers.
### Boolean Value
BooleanValue : one of `true` `false`
The two keywords `true` and `false` represent the two boolean values.
### String Value
StringValue ::
- `""` [lookahead != `"`]
- `"` StringCharacter+ `"`
- BlockString
StringCharacter ::
- SourceCharacter but not `"` or `\` or LineTerminator
- `\u` EscapedUnicode
- `\` EscapedCharacter
EscapedUnicode ::
- `{` HexDigit+ `}`
- HexDigit HexDigit HexDigit HexDigit
HexDigit :: one of
- `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
- `A` `B` `C` `D` `E` `F`
- `a` `b` `c` `d` `e` `f`
EscapedCharacter :: one of `"` `\` `/` `b` `f` `n` `r` `t`
BlockString :: `"""` BlockStringCharacter\* `"""`
BlockStringCharacter ::
- SourceCharacter but not `"""` or `\"""`
- `\"""`
A {StringValue} is evaluated to a _Unicode text_ value, a sequence of _Unicode
scalar value_, by interpreting all escape sequences using the static semantics
defined below. Whitespace and other characters ignored between lexical tokens
are significant within a string value.
The empty string {`""`} must not be followed by another {`"`} otherwise it would
be interpreted as the beginning of a block string. As an example, the source
{`""""""`} can only be interpreted as a single empty block string and not three
empty strings.
**Escape Sequences**
In a single-quoted {StringValue}, any _Unicode scalar value_ may be expressed
using an escape sequence. GraphQL strings allow both C-style escape sequences
(for example `\n`) and two forms of Unicode escape sequences: one with a
fixed-width of 4 hexadecimal digits (for example `\u000A`) and one with a
variable-width most useful for representing a _supplementary character_ such as
an Emoji (for example `\u{1F4A9}`).
The hexadecimal number encoded by a Unicode escape sequence must describe a
_Unicode scalar value_, otherwise must result in a parse error. For example both
sources `"\uDEAD"` and `"\u{110000}"` should not be considered valid
{StringValue}.
Escape sequences are only meaningful within a single-quoted string. Within a
block string, they are simply that sequence of characters (for example
`"""\n"""` represents the _Unicode text_ [U+005C, U+006E]). Within a comment an
escape sequence is not a significant sequence of characters. They may not appear
elsewhere in a GraphQL document.
Since {StringCharacter} must not contain some code points directly (for example,
a {LineTerminator}), escape sequences must be used to represent them. All other
escape sequences are optional and unescaped non-ASCII Unicode characters are
allowed within strings. If using GraphQL within a system which only supports
ASCII, then escape sequences may be used to represent all Unicode characters
outside of the ASCII range.
For legacy reasons, a _supplementary character_ may be escaped by two
fixed-width unicode escape sequences forming a _surrogate pair_. For example the
input `"\uD83D\uDCA9"` is a valid {StringValue} which represents the same
_Unicode text_ as `"\u{1F4A9}"`. While this legacy form is allowed, it should be
avoided as a variable-width unicode escape sequence is a clearer way to encode
such code points.
When producing a {StringValue}, implementations should use escape sequences to
represent non-printable control characters (U+0000 to U+001F and U+007F to
U+009F). Other escape sequences are not necessary, however an implementation may
use escape sequences to represent any other range of code points (for example,
when producing ASCII-only output). If an implementation chooses to escape a
_supplementary character_, it should only use a variable-width unicode escape
sequence.
**Block Strings**
Block strings are sequences of characters wrapped in triple-quotes (`"""`).
Whitespace, line terminators, quote, and backslash characters may all be used
unescaped to enable verbatim text. Characters must all be valid
{SourceCharacter}.
Since block strings represent freeform text often used in indented positions,
the string value semantics of a block string excludes uniform indentation and
blank initial and trailing lines via {BlockStringValue()}.
For example, the following operation containing a block string:
```raw graphql example
mutation {
sendEmail(message: """
Hello,
World!
Yours,
GraphQL.
""")
}
```
Is identical to the standard quoted string:
```graphql example
mutation {
sendEmail(message: "Hello,\n World!\n\nYours,\n GraphQL.")
}
```
Since block string values strip leading and trailing empty lines, there is no
single canonical printed block string for a given value. Because block strings
typically represent freeform text, it is considered easier to read if they begin
and end with an empty line.
```graphql example
"""
This starts with and ends with an empty line,
which makes it easier to read.
"""
```
```graphql counter-example
"""This does not start with or end with any empty lines,
which makes it a little harder to read."""
```
Note: If non-printable ASCII characters are needed in a string value, a standard
quoted string with appropriate escape sequences must be used instead of a block
string.
**Static Semantics**
:: A {StringValue} describes a _Unicode text_ value, which is a sequence of
_Unicode scalar value_.
These semantics describe how to apply the {StringValue} grammar to a source text
to evaluate a _Unicode text_. Errors encountered during this evaluation are
considered a failure to apply the {StringValue} grammar to a source and must
result in a parsing error.
StringValue :: `""`
- Return an empty sequence.
StringValue :: `"` StringCharacter+ `"`
- Return the _Unicode text_ by concatenating the evaluation of all
{StringCharacter}.
StringCharacter :: SourceCharacter but not `"` or `\` or LineTerminator
- Return the _Unicode scalar value_ {SourceCharacter}.
StringCharacter :: `\u` EscapedUnicode
- Let {value} be the hexadecimal value represented by the sequence of {HexDigit}
within {EscapedUnicode}.
- Assert: {value} is a within the _Unicode scalar value_ range (>= 0x0000 and <=
0xD7FF or >= 0xE000 and <= 0x10FFFF).
- Return the _Unicode scalar value_ {value}.
StringCharacter :: `\u` HexDigit HexDigit HexDigit HexDigit `\u` HexDigit
HexDigit HexDigit HexDigit
- Let {leadingValue} be the hexadecimal value represented by the first sequence
of {HexDigit}.
- Let {trailingValue} be the hexadecimal value represented by the second
sequence of {HexDigit}.
- If {leadingValue} is >= 0xD800 and <= 0xDBFF (a _Leading Surrogate_):
- Assert: {trailingValue} is >= 0xDC00 and <= 0xDFFF (a _Trailing Surrogate_).
- Return ({leadingValue} - 0xD800) × 0x400 + ({trailingValue} - 0xDC00) +
0x10000.
- Otherwise:
- Assert: {leadingValue} is within the _Unicode scalar value_ range.
- Assert: {trailingValue} is within the _Unicode scalar value_ range.
- Return the sequence of the _Unicode scalar value_ {leadingValue} followed by
the _Unicode scalar value_ {trailingValue}.
Note: If both escape sequences encode a _Unicode scalar value_, then this
semantic is identical to applying the prior semantic on each fixed-width escape
sequence. A variable-width escape sequence must only encode a _Unicode scalar
value_.
StringCharacter :: `\` EscapedCharacter
- Return the _Unicode scalar value_ represented by {EscapedCharacter} according
to the table below.
| Escaped Character | Scalar Value | Character Name |
| ----------------- | ------------ | ---------------------------- |
| {`"`} | U+0022 | double quote |
| {`\`} | U+005C | reverse solidus (back slash) |
| {`/`} | U+002F | solidus (forward slash) |
| {`b`} | U+0008 | backspace |
| {`f`} | U+000C | form feed |
| {`n`} | U+000A | line feed (new line) |
| {`r`} | U+000D | carriage return |
| {`t`} | U+0009 | horizontal tab |
StringValue :: BlockString
- Return the _Unicode text_ by evaluating the {BlockString}.
BlockString :: `"""` BlockStringCharacter\* `"""`
- Let {rawValue} be the _Unicode text_ by concatenating the evaluation of all
{BlockStringCharacter} (which may be an empty sequence).
- Return the result of {BlockStringValue(rawValue)}.
BlockStringCharacter :: SourceCharacter but not `"""` or `\"""`
- Return the _Unicode scalar value_ {SourceCharacter}.
BlockStringCharacter :: `\"""`
- Return the character sequence `"""`.
BlockStringValue(rawValue):
- Let {lines} be the result of splitting {rawValue} by {LineTerminator}.
- Let {commonIndent} be {null}.
- For each {line} in {lines}:
- If {line} is the first item in {lines}, continue to the next {line}.
- Let {length} be the number of characters in {line}.
- Let {indent} be the number of leading consecutive {Whitespace} characters in
{line}.
- If {indent} is less than {length}:
- If {commonIndent} is {null} or {indent} is less than {commonIndent}:
- Let {commonIndent} be {indent}.
- If {commonIndent} is not {null}:
- For each {line} in {lines}:
- If {line} is the first item in {lines}, continue to the next line.
- Remove {commonIndent} characters from the beginning of {line}.
- While the first item {line} in {lines} contains only {Whitespace}:
- Remove the first item from {lines}.
- While the last item {line} in {lines} contains only {Whitespace}:
- Remove the last item from {lines}.
- Let {formatted} be the empty character sequence.
- For each {line} in {lines}:
- If {line} is the first item in {lines}:
- Append {formatted} with {line}.
- Otherwise:
- Append {formatted} with a line feed character (U+000A).
- Append {formatted} with {line}.
- Return {formatted}.
### Null Value
NullValue : `null`
Null values are represented as the keyword {null}.
GraphQL has two semantically different ways to represent the lack of a value:
- Explicitly providing the literal value: {null}.
- Implicitly not providing a value at all.
For example, these two field calls are similar, but are not identical:
```graphql example
{
field(arg: null)
field
}
```
The first has explicitly provided {null} to the argument "arg", while the second
has implicitly not provided a value to the argument "arg". These two forms may
be interpreted differently. For example, a mutation representing deleting a
field vs not altering a field, respectively. Neither form may be used for an
input expecting a Non-Null type.
Note: The same two methods of representing the lack of a value are possible via
variables by either providing the variable value as {null} or not providing a
variable value at all.
### Enum Value
EnumValue : Name but not `true`, `false` or `null`
Enum values are represented as unquoted names (e.g. `MOBILE_WEB`). It is
recommended that Enum values be "all caps". Enum values are only used in
contexts where the precise enumeration type is known. Therefore it is not
necessary to supply an enumeration type name in the literal.
### List Value
ListValue[Const] :
- [ ]
- [ Value[?Const]+ ]
Lists are ordered sequences of values wrapped in square brackets `[ ]`. The
values of a List literal may be any value literal or variable (e.g.
`[1, 2, 3]`).
Commas are optional throughout GraphQL so trailing commas are allowed and
repeated commas do not represent missing values.
**Semantics**
ListValue : [ ]
- Return a new empty list value.
ListValue : [ Value+ ]
- Let {inputList} be a new empty list value.
- For each {Value+}:
- Let {value} be the result of evaluating {Value}.
- Append {value} to {inputList}.
- Return {inputList}.
### Input Object Values
ObjectValue[Const] :
- { }
- { ObjectField[?Const]+ }
ObjectField[Const] : Name : Value[?Const]
Input object literal values are unordered lists of keyed input values wrapped in
curly braces `{ }`. The values of an object literal may be any input value
literal or variable (e.g. `{ name: "Hello world", score: 1.0 }`). We refer to
literal representation of input objects as "object literals."
**Input Object Fields Are Unordered**
Input object fields may be provided in any syntactic order and maintain
identical semantic meaning.
These two operations are semantically identical:
```graphql example
{
nearestThing(location: { lon: 12.43, lat: -53.211 })
}
```
```graphql example
{
nearestThing(location: { lat: -53.211, lon: 12.43 })
}
```
**Semantics**
ObjectValue : { }
- Return a new input object value with no fields.
ObjectValue : { ObjectField+ }
- Let {inputObject} be a new input object value with no fields.
- For each {field} in {ObjectField+}:
- Let {name} be {Name} in {field}.
- Let {value} be the result of evaluating {Value} in {field}.
- Add a field to {inputObject} of name {name} containing value {value}.
- Return {inputObject}.
## Variables
Variable : $ Name
VariablesDefinition : ( VariableDefinition+ )
VariableDefinition : Description? Variable : Type DefaultValue?
Directives[Const]?
DefaultValue : = Value[Const]
A GraphQL operation can be parameterized with variables, maximizing reuse, and
avoiding costly string building in clients at runtime.
If not defined as constant (for example, in {DefaultValue}), a {Variable} can be
supplied for an input value.
Variables must be defined at the top of an operation and are in scope throughout
the execution of that operation. Values for those variables are provided to a
GraphQL service as part of a request so they may be substituted in during
execution.
In this example, we want to fetch a profile picture size based on the size of a
particular device:
```graphql example
query getZuckProfile(
"The size of the profile picture to fetch."
$devicePicSize: Int
) {
user(id: 4) {
id
name
profilePic(size: $devicePicSize)
}
}
```
If providing JSON for the variables' values, we could request a `profilePic` of
size `60`:
```json example
{
"devicePicSize": 60
}
```
**Variable Use Within Fragments**
Variables can be used within fragments. Variables have global scope with a given
operation, so a variable used within a fragment must be declared in any
top-level operation that transitively consumes that fragment. If a variable is
referenced in a fragment and is included by an operation that does not define
that variable, that operation is invalid (see
[All Variable Uses Defined](#sec-All-Variable-Uses-Defined)).
## Type References
Type :
- NamedType
- ListType
- NonNullType
NamedType : Name
ListType : [ Type ]
NonNullType :
- NamedType !
- ListType !
GraphQL describes the types of data expected by arguments and variables. Input
types may be lists of another input type, or a non-null variant of any other
input type.
**Semantics**
Type : Name
- Let {name} be the string value of {Name}.
- Let {type} be the type defined in the Schema named {name}.
- {type} must exist.
- Return {type}.
Type : [ Type ]
- Let {itemType} be the result of evaluating {Type}.
- Let {type} be a List type where {itemType} is the contained type.
- Return {type}.
Type : Type !
- Let {nullableType} be the result of evaluating {Type}.
- Let {type} be a Non-Null type where {nullableType} is the contained type.
- Return {type}.
## Directives
Directives[Const] : Directive[?Const]+
Directive[Const] : @ Name Arguments[?Const]?
Directives provide a way to describe alternate runtime execution and type
validation behavior in a GraphQL document.
In some cases, you need to provide options to alter GraphQL's execution behavior
in ways field arguments will not suffice, such as conditionally including or
skipping a field. Directives provide this by describing additional information
to the executor.
Directives have a name along with a list of arguments which may accept values of
any input type.
Directives can be used to describe additional information for types, fields,
fragments and operations.
As future versions of GraphQL adopt new configurable execution capabilities,
they may be exposed via directives. GraphQL services and tools may also provide
any additional _custom directive_ beyond those described here.
**Directive Order Is Significant**
Directives may be provided in a specific syntactic order which may have semantic
interpretation.
These two type definitions may have different semantic meaning:
```graphql example
type Person
@addExternalFields(source: "profiles")
@excludeField(name: "photo") {
name: String
}
```
```graphql example
type Person
@excludeField(name: "photo")
@addExternalFields(source: "profiles") {
name: String
}
```
## Schema Coordinates
SchemaCoordinate ::
- TypeCoordinate
- MemberCoordinate
- ArgumentCoordinate
- DirectiveCoordinate
- DirectiveArgumentCoordinate
TypeCoordinate :: Name
MemberCoordinate :: Name . Name
ArgumentCoordinate :: Name . Name ( Name : )
DirectiveCoordinate :: @ Name
DirectiveArgumentCoordinate :: @ Name ( Name : )
:: A _schema coordinate_ is a human readable string that uniquely identifies a
_schema element_ within a GraphQL Schema, intended to be used by tools to
reference types, fields, and other _schema element_. Examples include:
references within documentation to refer to types and fields in a schema, a
lookup key that can be used in logging tools to track how often particular
fields are queried in production.
:: A _schema element_ can be a named type, a field, an input field, an enum
value, a field argument, a directive, or a directive argument defined within a
schema (including built-in types and directives).
Note: Meta-fields are not defined within a schema, and thus are not _schema
element_. By extension, an introspection type is not a _schema element_.
:: The _containing element_ of a _schema element_ is the schema element with one
fewer {Name} token that syntactically contains it. Specifically:
- The containing element of an {ArgumentCoordinate} is a {MemberCoordinate}.
- The containing element of a {MemberCoordinate} is a {TypeCoordinate}.
- The containing element of a {DirectiveArgumentCoordinate} is a
{DirectiveCoordinate}.
- {TypeCoordinate} and {DirectiveCoordinate} have no containing element.
A _schema coordinate_ is always unique. Each _schema element_ can be referenced
by exactly one possible schema coordinate.
A _schema coordinate_ may refer to either a defined or built-in _schema
element_. For example, `String` and `@deprecated(reason:)` are both valid schema
coordinates which refer to built-in schema elements.
Note: A union member references a type in the schema. A type in the schema is
identified by a {TypeCoordinate}. There is no schema coordinate which indicates
a union member; this preserves the uniqueness property of a _schema coordinate_
as stated above.
**Parsing a Schema Coordinate**
SchemaCoordinateToken ::
- SchemaCoordinatePunctuator
- Name
SchemaCoordinatePunctuator :: one of ( ) . : @
A {SchemaCoordinate} is a self-contained grammar with its own set of lexical
tokens, it is not contained within a {Document}. The source text of a
SchemaCoordinate must be a sequence of {SourceCharacter}.
Unlike other [GraphQL documents](#sec-Language), {SchemaCoordinate} must not
contain {Whitespace} or other {Ignored} grammars within the character sequence.
This ensures that every schema coordinates has a single unambiguous and unique
lexical form.
**Resolving a Schema Coordinate**
To refer to a _schema element_, a _schema coordinate_ must be interpreted in the
context of a GraphQL {schema}.
If the _schema element_ cannot be found, the resolve function will not yield a
value (without raising an error). However, an error will be raised if any
non-leaf nodes within a _schema coordinate_ cannot be found in the {schema}.
Note: Although it is syntactically possible to describe a meta-field or element
of the introspection schema with a schema coordinate (e.g. `Business.__typename`
or `__Type.fields(includeDeprecated:)`), they are not _schema element_ and
therefore resolving such coordinates does not have a defined behavior.
TypeCoordinate :: Name
1. Let {typeName} be the value of {Name}.
2. Return the type in {schema} named {typeName} if it exists.
MemberCoordinate :: Name . Name
1. Let {typeName} be the value of the first {Name}.
2. Let {type} be the type in {schema} named {typeName}.
3. Assert: {type} must exist, and must be an Enum, Input Object, Object or
Interface type.
4. If {type} is an Enum type:
1. Let {enumValueName} be the value of the second {Name}.
2. Return the enum value of {type} named {enumValueName} if it exists.
5. Otherwise, if {type} is an Input Object type:
1. Let {inputFieldName} be the value of the second {Name}.
2. Return the input field of {type} named {inputFieldName} if it exists.
6. Otherwise:
1. Let {fieldName} be the value of the second {Name}.
2. Return the field of {type} named {fieldName} if it exists.
ArgumentCoordinate :: Name . Name ( Name : )
1. Let {typeName} be the value of the first {Name}.
2. Let {type} be the type in {schema} named {typeName}.
3. Assert: {type} must exist, and be an Object or Interface type.
4. Let {fieldName} be the value of the second {Name}.
5. Let {field} be the field of {type} named {fieldName}.
6. Assert: {field} must exist.
7. Let {fieldArgumentName} be the value of the third {Name}.
8. Return the argument of {field} named {fieldArgumentName} if it exists.
DirectiveCoordinate :: @ Name
1. Let {directiveName} be the value of {Name}.
2. Return the directive in {schema} named {directiveName} if it exists.
DirectiveArgumentCoordinate :: @ Name ( Name : )
1. Let {directiveName} be the value of the first {Name}.
2. Let {directive} be the directive in {schema} named {directiveName}.
3. Assert: {directive} must exist.
4. Let {directiveArgumentName} be the value of the second {Name}.
5. Return the argument of {directive} named {directiveArgumentName} if it
exists.
**Examples**
| Element Kind | Schema Coordinate | Schema Element |
| ------------------ | --------------------------------- | --------------------------------------------------------------------- |
| Named Type | `Business` | `Business` type |
| Field | `Business.name` | `name` field on the `Business` type |
| Input Field | `SearchCriteria.filter` | `filter` input field on the `SearchCriteria` input object type |
| Enum Value | `SearchFilter.OPEN_NOW` | `OPEN_NOW` value of the `SearchFilter` enum |
| Field Argument | `Query.searchBusiness(criteria:)` | `criteria` argument on the `searchBusiness` field on the `Query` type |
| Directive | `@private` | `@private` directive |
| Directive Argument | `@private(scope:)` | `scope` argument on the `@private` directive |
The table above shows an example of a _schema coordinate_ for every kind of
_schema element_ based on the schema below.
```graphql
type Query {
searchBusiness(criteria: SearchCriteria!): [Business]
}
input SearchCriteria {
name: String
filter: SearchFilter
}
enum SearchFilter {
OPEN_NOW
DELIVERS_TAKEOUT
VEGETARIAN_MENU
}
type Business {
id: ID
name: String
email: String @private(scope: "loggedIn")
}
directive @private(scope: String!) on FIELD_DEFINITION
```
================================================
FILE: spec/Section 3 -- Type System.md
================================================
# Type System
The GraphQL Type system describes the capabilities of a GraphQL service and is
used to determine if a requested operation is valid, to guarantee the type of
response results, and describes the input types of variables to determine if
values provided at request time are valid.
TypeSystemDocument : TypeSystemDefinition+
TypeSystemDefinition :
- SchemaDefinition
- TypeDefinition
- DirectiveDefinition
The GraphQL language includes an
[IDL](https://en.wikipedia.org/wiki/Interface_description_language) used to
describe a GraphQL service's type system. Tools may use this definition language
to provide utilities such as client code generation or service bootstrapping.
GraphQL tools or services which only seek to execute GraphQL requests and not
construct a new GraphQL schema may choose not to allow {TypeSystemDefinition}.
Tools which only seek to produce schema and not execute requests may choose to
only allow {TypeSystemDocument} and not allow {ExecutableDefinition} or
{TypeSystemExtension} but should provide a descriptive error if present.
Note: The type system definition language is used throughout the remainder of
this specification document when illustrating example type systems.
## Type System Extensions
TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+
TypeSystemDefinitionOrExtension :
- TypeSystemDefinition
- TypeSystemExtension
TypeSystemExtension :
- SchemaExtension
- TypeExtension
Type system extensions are used to represent a GraphQL type system which has
been extended from some previous type system. For example, this might be used by
a local service to represent data a GraphQL client only accesses locally, or by
a GraphQL service which is itself an extension of another GraphQL service.
Tools which only seek to produce and extend schema and not execute requests may
choose to only allow {TypeSystemExtensionDocument} and not allow
{ExecutableDefinition} but should provide a descriptive error if present.
## Type System Descriptions
Documentation is a first-class feature of GraphQL type systems, written
immediately alongside definitions in a {TypeSystemDocument} and made available
via introspection.
[Descriptions](#sec-Descriptions) allow GraphQL service designers to easily
provide documentation which remains consistent with the capabilities of a
GraphQL service. Descriptions should be provided as Markdown (as specified by
[CommonMark](https://commonmark.org/)) for every definition in a type system.
GraphQL schema and all other definitions (e.g. types, fields, arguments, etc.)
which can be described should provide a {Description} unless they are considered
self descriptive.
As an example, this simple GraphQL schema is well described:
```raw graphql example
"""
A simple GraphQL schema which is well described.
"""
schema {
query: Query
}
"""
Root type for all your query operations
"""
type Query {
"""
Translates a string from a given language into a different language.
"""
translate(
"The original language that `text` is provided in."
fromLanguage: Language
"The translated language to be returned."
toLanguage: Language
"The text to be translated."
text: String
): String
}
"""
The set of languages supported by `translate`.
"""
enum Language {
"English"
EN
"French"
FR
"Chinese"
CH
}
```
## Schema
SchemaDefinition : Description? schema Directives[Const]? {
RootOperationTypeDefinition+ }
RootOperationTypeDefinition : OperationType : NamedType
A GraphQL service's collective type system capabilities are referred to as that
service's "schema". A schema is defined in terms of the types and directives it
supports as well as the _root operation type_ for each kind of operation: query,
mutation, and subscription; this determines the place in the type system where
those operations begin.
A GraphQL schema must itself be internally valid. This section describes the
rules for this validation process where relevant.
All types within a GraphQL schema must have unique names. No two provided types
may have the same name. No provided type may have a name which conflicts with
any built in types (including Scalar and Introspection types).
All directives within a GraphQL schema must have unique names.
All types and directives defined within a schema must not have a name which
begins with {"\_\_"} (two underscores), as this is used exclusively by GraphQL's
introspection system.
### Root Operation Types
:: A schema defines the initial _root operation type_ for each kind of operation
it supports: query, mutation, and subscription; this determines the place in the
type system where those operations begin.
The {`query`} _root operation type_ must be provided and must be an Object type.
The {`mutation`} _root operation type_ is optional; if it is not provided, the
service does not support mutations. If it is provided, it must be an Object
type.
Similarly, the {`subscription`} _root operation type_ is also optional; if it is
not provided, the service does not support subscriptions. If it is provided, it
must be an Object type.
The {`query`}, {`mutation`}, and {`subscription`} root types must all be
different types if provided.
The fields on the {`query`} _root operation type_ indicate what fields are
available at the top level of a GraphQL query operation.
For example, this example operation:
```graphql example
query {
myName
}
```
is only valid when the {`query`} _root operation type_ has a field named
"myName":
```graphql example
type Query {
myName: String
}
```
Similarly, the following mutation is only valid if the {`mutation`} _root
operation type_ has a field named "setName".
```graphql example
mutation {
setName(name: "Zuck") {
newName
}
}
```
When using the type system definition language, a document must include at most
one {`schema`} definition.
In this example, a GraphQL schema is defined with both a query and mutation
_root operation type_:
```graphql example
schema {
query: MyQueryRootType
mutation: MyMutationRootType
}
type MyQueryRootType {
someField: String
}
type MyMutationRootType {
setSomeField(to: String): String
}
```
**Default Root Operation Type Names**
:: The _default root type name_ for each {`query`}, {`mutation`}, and
{`subscription`} _root operation type_ are {"Query"}, {"Mutation"}, and
{"Subscription"} respectively.
The type system definition language can omit the schema definition when each
_root operation type_ uses its respective _default root type name_, no other
type uses any _default root type name_, and the schema does not have a
description.
Likewise, when representing a GraphQL schema using the type system definition
language, a schema definition should be omitted if each _root operation type_
uses its respective _default root type name_, no other type uses any _default
root type name_, and the schema does not have a description.
This example describes a valid complete GraphQL schema, despite not explicitly
including a {`schema`} definition. The {"Query"} type is presumed to be the
{`query`} _root operation type_ of the schema.
```graphql example
type Query {
someField: String
}
```
This example describes a valid GraphQL schema without a {`mutation`} _root
operation type_, even though it contains a type named {"Mutation"}. The schema
definition must be included, otherwise the {"Mutation"} type would be
incorrectly presumed to be the {`mutation`} _root operation type_ of the schema.
```graphql example
schema {
query: Query
}
type Query {
latestVirus: Virus
}
type Virus {
name: String
mutations: [Mutation]
}
type Mutation {
name: String
}
```
This example describes a valid GraphQL schema with a description and both a
{`query`} and {`mutation`} operation type:
```graphql example
"""
Example schema
"""
schema {
query: Query
mutation: Mutation
}
type Query {
someField: String
}
type Mutation {
someMutation: String
}
```
### Schema Extension
SchemaExtension :
- extend schema Directives[Const]? { RootOperationTypeDefinition+ }
- extend schema Directives[Const] [lookahead != `{`]
Schema extensions are used to represent a schema which has been extended from a
previous schema. For example, this might be used by a GraphQL service which adds
additional operation types, or additional directives to an existing schema.
Note: Schema extensions without additional operation type definitions must not
be followed by a {`{`} (such as a query shorthand) to avoid parsing ambiguity.
The same limitation applies to the type definitions and extensions below.
**Schema Validation**
Schema extensions have the potential to be invalid if incorrectly defined.
1. The Schema must already be defined.
2. Any non-repeatable directives provided must not already apply to the previous
Schema.
## Types
TypeDefinition :
- ScalarTypeDefinition
- ObjectTypeDefinition
- InterfaceTypeDefinition
- UnionTypeDefinition
- EnumTypeDefinition
- InputObjectTypeDefinition
The fundamental unit of any GraphQL Schema is the type. There are six kinds of
named type definitions in GraphQL, and two wrapping types.
The most basic type is a `Scalar`. A scalar represents a primitive value, like a
string or an integer. Oftentimes, the possible responses for a scalar field are
enumerable. GraphQL offers an `Enum` type in those cases, where the type
specifies the space of valid responses.
Scalars and Enums form the leaves in response trees; the intermediate levels are
`Object` types, which define a set of fields, where each field is another type
in the system, allowing the definition of arbitrary type hierarchies.
GraphQL supports two abstract types: interfaces and unions.
An `Interface` defines a list of fields; `Object` types and other Interface
types which implement this Interface are guaranteed to implement those fields.
Whenever a field claims it will return an Interface type, it will return a valid
implementing Object type during execution.
A `Union` defines a list of possible types; similar to interfaces, whenever the
type system claims a union will be returned, one of the possible types will be
returned.
Finally, oftentimes it is useful to provide complex structs as inputs to GraphQL
field arguments or variables; the `Input Object` type allows the schema to
define exactly what data is expected.
### Wrapping Types
All of the types so far are assumed to be both nullable and singular: e.g. a
scalar string returns either null or a singular string.
A GraphQL schema may describe that a field represents a list of another type;
the `List` type is provided for this reason, and wraps another type.
Similarly, the `Non-Null` type wraps another type, and denotes that the
resulting value will never be {null} (and that an _execution error_ cannot
result in a {null} value).
These two types are referred to as "wrapping types"; non-wrapping types are
referred to as "named types". A wrapping type has an underlying named type,
found by continually unwrapping the type until a named type is found.
### Input and Output Types
Types are used throughout GraphQL to describe both the values accepted as input
to arguments and variables as well as the values output by fields. These two
uses categorize types as _input types_ and _output types_. Some kinds of types,
like Scalar and Enum types, can be used as both input types and output types;
other kinds of types can only be used in one or the other. Input Object types
can only be used as input types. Object, Interface, and Union types can only be
used as output types. Lists and Non-Null types may be used as input types or
output types depending on how the wrapped type may be used.
IsInputType(type):
- If {type} is a List type or Non-Null type:
- Let {unwrappedType} be the unwrapped type of {type}.
- Return {IsInputType(unwrappedType)}.
- If {type} is a Scalar, Enum, or Input Object type:
- Return {true}.
- Return {false}.
IsOutputType(type):
- If {type} is a List type or Non-Null type:
- Let {unwrappedType} be the unwrapped type of {type}.
- Return {IsOutputType(unwrappedType)}.
- If {type} is a Scalar, Object, Interface, Union, or Enum type:
- Return {true}.
- Return {false}.
### Type Extensions
TypeExtension :
- ScalarTypeExtension
- ObjectTypeExtension
- InterfaceTypeExtension
- UnionTypeExtension
- EnumTypeExtension
- InputObjectTypeExtension
Type extensions are used to represent a GraphQL type which has been extended
from some previous type. For example, this might be used by a local service to
represent additional fields a GraphQL client only accesses locally.
## Scalars
ScalarTypeDefinition : Description? scalar Name Directives[Const]?
Scalar types represent primitive leaf values in a GraphQL type system. GraphQL
responses take the form of a hierarchical tree; the leaves of this tree are
typically GraphQL Scalar types (but may also be Enum types or {null} values).
GraphQL provides a number of built-in scalars which are fully defined in the
sections below, however type systems may also add additional custom scalars to
introduce additional semantic meaning.
**Built-in Scalars**
GraphQL specifies a basic set of well-defined Scalar types: {Int}, {Float},
{String}, {Boolean}, and {ID}. A GraphQL framework should support all of these
types, and a GraphQL service which provides a type by these names must adhere to
the behavior described for them in this document. As an example, a service must
not include a type called {Int} and use it to represent 64-bit numbers,
internationalization information, or anything other than what is defined in this
document.
When returning the set of types from the `__Schema` introspection type, all
referenced built-in scalars must be included. If a built-in scalar type is not
referenced anywhere in a schema (there is no field, argument, or input field of
that type) then it must not be included.
When representing a GraphQL schema using the type system definition language,
all built-in scalars must be omitted for brevity.
**Custom Scalars**
GraphQL services may use custom scalar types in addition to the built-in
scalars. For example, a GraphQL service could define a scalar called `UUID`
which, while serialized as a string, conforms to
[RFC 4122](https://tools.ietf.org/html/rfc4122). When querying a field of type
`UUID`, you can then rely on the ability to parse the result with an RFC 4122
compliant parser. Another example of a potentially useful custom scalar is
`URL`, which serializes as a string, but is guaranteed by the service to be a
valid URL.
:: When defining a custom scalar, GraphQL services should provide a _scalar
specification URL_ via the `@specifiedBy` directive or the `specifiedByURL`
introspection field. This URL must link to a human-readable specification of the
data format, serialization, and coercion rules for the scalar.
For example, a GraphQL service providing a `UUID` scalar may link to RFC 4122,
or some custom document defining a reasonable subset of that RFC. If a _scalar
specification URL_ is present, systems and tools that are aware of it should
conform to its described rules.
```graphql example
scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
scalar DateTime
@specifiedBy(url: "https://scalars.graphql.org/andimarek/date-time")
```
Custom *scalar specification URL*s should provide a single, stable format to
avoid ambiguity. If the linked specification is in flux, the service should link
to a fixed version rather than to a resource which might change.
Note: Some community-maintained custom scalar specifications are hosted at
[scalars.graphql.org](https://scalars.graphql.org/).
Custom *scalar specification URL*s should not be changed once defined. Doing so
would likely disrupt tooling or could introduce breaking changes within the
linked specification's contents.
Built-in scalar types must not provide a _scalar specification URL_ as they are
specified by this document.
Note: Custom scalars should also summarize the specified format and provide
examples in their description; see the GraphQL scalars
[implementation guide](https://scalars.graphql.org/implementation-guide) for
more guidance.
**Result Coercion and Serialization**
A GraphQL service, when preparing a field of a given scalar type, must uphold
the contract the scalar type describes, either by coercing the value or
producing an _execution error_ if a value cannot be coerced or if coercion may
result in data loss.
A GraphQL service may decide to allow coercing different internal types to the
expected return type. For example when coercing a field of type {Int} a boolean
{true} value may produce {1} or a string value {"123"} may be parsed as base-10
{123}. However if internal type coercion cannot be reasonably performed without
losing information, then it must raise an _execution error_.
Since this coercion behavior is not observable to clients of the GraphQL
service, the precise rules of coercion are left to the implementation. The only
requirement is that the service must yield values which adhere to the expected
Scalar type.
GraphQL scalars are serialized according to the serialization format being used.
There may be a most appropriate serialized primitive for each given scalar type,
and the service should produce each primitive where appropriate.
See [Serialization Format](#sec-Serialization-Format) for more detailed
information on the serialization of scalars in common JSON and other formats.
**Input Coercion**
If a GraphQL service expects a scalar type as input to an argument, coercion is
observable and the rules must be well defined. If an input value does not match
a coercion rule, a _request error_ must be raised (input values are validated
before execution begins).
GraphQL has different constant literals to represent integer and floating-point
input values, and coercion rules may apply differently depending on which type
of input value is encountered. GraphQL may be parameterized by variables, the
values of which are often serialized when sent over a transport like HTTP. Since
some common serializations (e.g. JSON) do not discriminate between integer and
floating-point values, they are interpreted as an integer input value if they
have an empty fractional part (e.g. `1.0`) and otherwise as floating-point input
value.
For all types below, with the exception of Non-Null, if the explicit value
{null} is provided, then the result of input coercion is {null}.
### Int
The Int scalar type represents a signed 32-bit numeric non-fractional value.
Response formats that support a 32-bit integer or a number type should use that
type to represent this scalar.
**Result Coercion**
Fields returning the type {Int} expect to encounter 32-bit integer internal
values.
GraphQL services may coerce non-integer internal values to integers when
reasonable without losing information, otherwise they must raise an _execution
error_. Examples of this may include returning `1` for the floating-point number
`1.0`, or returning `123` for the string `"123"`. In scenarios where coercion
may lose data, raising an execution error is more appropriate. For example, a
floating-point number `1.2` should raise an execution error instead of being
truncated to `1`.
If the integer internal value represents a value less than -231 or
greater than or equal to 231, an _execution error_ should be raised.
**Input Coercion**
When expected as an input type, only integer input values are accepted. All
other input values, including strings with numeric content, must raise a request
error indicating an incorrect type. If the integer input value represents a
value less than -231 or greater than or equal to 231, a
_request error_ should be raised.
Note: Numeric integer values larger than 32-bit should either use String or a
custom-defined Scalar type, as not all platforms and transports support encoding
integer numbers larger than 32-bit.
### Float
The Float scalar type represents signed double-precision finite values as
specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
Response formats that support an appropriate double-precision number type should
use that type to represent this scalar.
**Result Coercion**
Fields returning the type {Float} expect to encounter double-precision
floating-point internal values.
GraphQL services may coerce non-floating-point internal values to {Float} when
reasonable without losing information, otherwise they must raise an _execution
error_. Examples of this may include returning `1.0` for the integer number `1`,
or `123.0` for the string `"123"`.
Non-finite floating-point internal values ({NaN} and {Infinity}) cannot be
coerced to {Float} and must raise an _execution error_.
**Input Coercion**
When expected as an input type, both integer and float input values are
accepted. Integer input values are coerced to Float by adding an empty
fractional part, for example `1.0` for the integer input value `1`. All other
input values, including strings with numeric content, must raise a _request
error_ indicating an incorrect type. If the input value otherwise represents a
value not representable by finite IEEE 754 (e.g. {NaN}, {Infinity}, or a value
outside the available precision), a _request error_ must be raised.
### String
The String scalar type represents textual data, represented as a sequence of
Unicode code points. The String type is most often used by GraphQL to represent
free-form human-readable text. How the String is encoded internally (for example
UTF-8) is left to the service implementation. All response serialization formats
must support a string representation (for example, JSON Unicode strings), and
that representation must be used to serialize this type.
**Result Coercion**
Fields returning the type {String} expect to encounter Unicode string values.
GraphQL services may coerce non-string raw values to {String} when reasonable
without losing information, otherwise they must raise an _execution error_.
Examples of this may include returning the string `"true"` for a boolean true
value, or the string `"1"` for the integer `1`.
**Input Coercion**
When expected as an input type, only valid Unicode string input values are
accepted. All other input values must raise a _request error_ indicating an
incorrect type.
### Boolean
The Boolean scalar type represents `true` or `false`. Response formats should
use a built-in boolean type if supported; otherwise, they should use their
representation of the integers `1` and `0`.
**Result Coercion**
Fields returning the type {Boolean} expect to encounter boolean internal values.
GraphQL services may coerce non-boolean raw values to {Boolean} when reasonable
without losing information, otherwise they must raise an _execution error_.
Examples of this may include returning `true` for non-zero numbers.
**Input Coercion**
When expected as an input type, only boolean input values are accepted. All
other input values must raise a _request error_ indicating an incorrect type.
### ID
The ID scalar type represents a unique identifier, often used to refetch an
object or as the key for a cache. The ID type is serialized in the same way as a
{String}; however, it is not intended to be human-readable. While it is often
numeric, it must always serialize as a {String}.
**Result Coercion**
GraphQL is agnostic to ID format, and serializes to string to ensure consistency
across many formats ID could represent, from small auto-increment numbers, to
large 128-bit random numbers, to base64 encoded values, or string values of a
format like [GUID](https://en.wikipedia.org/wiki/Globally_unique_identifier).
GraphQL services should coerce as appropriate given the ID formats they expect.
When coercion is not possible they must raise an _execution error_.
**Input Coercion**
When expected as an input type, any string (such as `"4"`) or integer (such as
`4` or `-4`) input value should be coerced to ID as appropriate for the ID
formats a given GraphQL service expects. Any other input value, including float
input values (such as `4.0`), must raise a _request error_ indicating an
incorrect type.
### Scalar Extensions
ScalarTypeExtension :
- extend scalar Name Directives[Const]
Scalar type extensions are used to represent a scalar type which has been
extended from some previous scalar type. For example, this might be used by a
GraphQL tool or service which adds directives to an existing scalar.
**Type Validation**
Scalar type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be a Scalar type.
2. Any non-repeatable directives provided must not already apply to the previous
Scalar type.
## Objects
ObjectTypeDefinition :
- Description? type Name ImplementsInterfaces? Directives[Const]?
FieldsDefinition
- Description? type Name ImplementsInterfaces? Directives[Const]? [lookahead !=
`{`]
ImplementsInterfaces :
- ImplementsInterfaces & NamedType
- implements `&`? NamedType
FieldsDefinition : { FieldDefinition+ }
FieldDefinition : Description? Name ArgumentsDefinition? : Type
Directives[Const]?
GraphQL operations are hierarchical and composed, describing a tree of
information. While Scalar types describe the leaf values of these hierarchical
operations, Objects describe the intermediate levels.
GraphQL Objects represent a list of named fields, each of which yields a value
of a specific type. Object values should be serialized as ordered maps, where
the selected field names (or aliases) are the keys and the result of evaluating
the field is the value, ordered by the order in which they appear in the
_selection set_.
All fields defined within an Object type must not have a name which begins with
{"\_\_"} (two underscores), as this is used exclusively by GraphQL's
introspection system.
For example, a type `Person` could be described as:
```graphql example
type Person {
name: String
age: Int
picture: Url
}
```
Where `name` is a field that will yield a {String} value, and `age` is a field
that will yield an {Int} value, and `picture` is a field that will yield a `Url`
value.
A query of an object value must select at least one field. This selection of
fields will yield an ordered map containing exactly the subset of the object
queried, which should be represented in the order in which they were queried.
Only fields that are declared on the object type may validly be queried on that
object.
For example, selecting all the fields of `Person`:
```graphql example
{
name
age
picture
}
```
Would yield the object:
```json example
{
"name": "Mark Zuckerberg",
"age": 30,
"picture": "http://some.cdn/picture.jpg"
}
```
While selecting a subset of fields:
```graphql example
{
age
name
}
```
Must only yield exactly that subset:
```json example
{
"age": 30,
"name": "Mark Zuckerberg"
}
```
A field of an Object type may be a Scalar, Enum, another Object type, an
Interface, or a Union. Additionally, it may be any wrapping type whose
underlying base type is one of those five.
For example, the `Person` type might include a `relationship`:
```graphql example
type Person {
name: String
age: Int
picture: Url
relationship: Person
}
```
Valid operations must supply a _selection set_ for every field whose return type
is an object type, so this operation is not valid:
```graphql counter-example
{
name
relationship
}
```
However, this example is valid:
```graphql example
{
name
relationship {
name
}
}
```
And will yield the subset of each object type queried:
```json example
{
"name": "Mark Zuckerberg",
"relationship": {
"name": "Priscilla Chan"
}
}
```
**Field Ordering**
When querying an Object, the resulting mapping of fields are conceptually
ordered in the same order in which they were encountered during execution,
excluding fragments for which the type does not apply and fields or fragments
that are skipped via `@skip` or `@include` directives. This ordering is
correctly produced when using the {CollectFields()} algorithm.
Response serialization formats capable of representing ordered maps should
maintain this ordering. Serialization formats which can only represent unordered
maps (such as JSON) should retain this order textually. That is, if two fields
`{foo, bar}` were queried in that order, the resulting JSON serialization should
contain `{"foo": "...", "bar": "..."}` in the same order.
Producing a response where fields are represented in the same order in which
they appear in the request improves human readability during debugging and
enables more efficient parsing of responses if the order of properties can be
anticipated.
If a fragment is spread before other fields, the fields that fragment specifies
occur in the response before the following fields.
```graphql example
{
foo
...Frag
qux
}
fragment Frag on Query {
bar
baz
}
```
Produces the ordered result:
```json example
{
"foo": 1,
"bar": 2,
"baz": 3,
"qux": 4
}
```
If a field is queried multiple times in a selection, it is ordered by the first
time it is encountered. However fragments for which the type does not apply do
not affect ordering.
```graphql example
{
foo
...Ignored
...Matching
bar
}
fragment Ignored on UnknownType {
qux
baz
}
fragment Matching on Query {
bar
qux
foo
}
```
Produces the ordered result:
```json example
{
"foo": 1,
"bar": 2,
"qux": 3
}
```
Also, if directives result in fields being excluded, they are not considered in
the ordering of fields.
```graphql example
{
foo @skip(if: true)
bar
foo
}
```
Produces the ordered result:
```json example
{
"bar": 1,
"foo": 2
}
```
**Result Coercion**
Determining the result of coercing an object is the heart of the GraphQL
executor, see [Value Completion](#sec-Value-Completion).
**Input Coercion**
Objects are never valid inputs.
**Type Validation**
Object types have the potential to be invalid if incorrectly defined. This set
of rules must be adhered to by every Object type in a GraphQL schema.
1. An Object type must define one or more fields.
2. For each field of an Object type:
1. The field must have a unique name within that Object type; no two fields
may share the same name.
2. The field must not have a name which begins with the characters {"\_\_"}
(two underscores).
3. The field must return a type where {IsOutputType(fieldType)} returns
{true}.
4. For each argument of the field:
1. The argument must not have a name which begins with the characters
{"\_\_"} (two underscores).
2. The argument must have a unique name within that field; no two
arguments may share the same name.
3. The argument must accept a type where {IsInputType(argumentType)}
returns {true}.
4. If argument type is Non-Null and a default value is not defined:
1. The `@deprecated` directive must not be applied to this argument.
5. If the argument has a default value it must be compatible with
{argumentType} as per the coercion rules for that type.
3. An object type may declare that it implements one or more unique interfaces.
4. An object type must be a super-set of all interfaces it implements:
1. Let this object type be {objectType}.
2. For each interface declared implemented as {interfaceType},
{IsValidImplementation(objectType, interfaceType)} must be {true}.
IsValidImplementation(type, implementedType):
1. If {implementedType} declares it implements any interfaces, {type} must also
declare it implements those interfaces.
2. {type} must include a field of the same name for every field defined in
{implementedType}.
1. Let {field} be that named field on {type}.
2. Let {implementedField} be that named field on {implementedType}.
3. {field} must include an argument of the same name for every argument
defined in {implementedField}.
1. That named argument on {field} must accept the same type (invariant)
as that named argument on {implementedField}.
4. {field} may include additional arguments not defined in
{implementedField}, but any additional argument must not be required,
e.g. must not be of a non-nullable type.
5. {field} must return a type which is equal to or a sub-type of (covariant)
the return type of {implementedField} field's return type:
1. Let {fieldType} be the return type of {field}.
2. Let {implementedFieldType} be the return type of {implementedField}.
3. {IsValidImplementationFieldType(fieldType, implementedFieldType)} must
be {true}.
6. If {field} is deprecated then {implementedField} must also be deprecated.
IsValidImplementationFieldType(fieldType, implementedFieldType):
1. If {fieldType} is a Non-Null type:
1. Let {nullableType} be the unwrapped nullable type of {fieldType}.
2. Let {implementedNullableType} be the unwrapped nullable type of
{implementedFieldType} if it is a Non-Null type, otherwise let it be
{implementedFieldType} directly.
3. Return {IsValidImplementationFieldType(nullableType,
implementedNullableType)}.
2. If {fieldType} is a List type and {implementedFieldType} is also a List type:
1. Let {itemType} be the unwrapped item type of {fieldType}.
2. Let {implementedItemType} be the unwrapped item type of
{implementedFieldType}.
3. Return {IsValidImplementationFieldType(itemType, implementedItemType)}.
3. Return {IsSubType(fieldType, implementedFieldType)}.
IsSubType(possibleSubType, superType):
1. If {possibleSubType} is the same type as {superType} then return {true}.
2. If {possibleSubType} is an Object type and {superType} is a Union type and
{possibleSubType} is a possible type of {superType} then return {true}.
3. If {possibleSubType} is an Object or Interface type and {superType} is an
Interface type and {possibleSubType} declares it implements {superType} then
return {true}.
4. Otherwise return {false}.
### Field Arguments
ArgumentsDefinition : ( InputValueDefinition+ )
InputValueDefinition : Description? Name : Type DefaultValue? Directives[Const]?
Object fields are conceptually functions which yield values. Occasionally object
fields can accept arguments to further specify the return value. Object field
arguments are defined as a list of all possible argument names and their
expected input types.
All arguments defined within a field must not have a name which begins with
{"\_\_"} (two underscores), as this is used exclusively by GraphQL's
introspection system.
For example, a `Person` type with a `picture` field could accept an argument to
determine what size of an image to return.
```graphql example
type Person {
name: String
picture(size: Int): Url
}
```
Operations can optionally specify arguments to their fields to provide these
arguments.
This example operation:
```graphql example
{
name
picture(size: 600)
}
```
May return the result:
```json example
{
"name": "Mark Zuckerberg",
"picture": "http://some.cdn/picture_600.jpg"
}
```
The type of an object field argument must be an input type (any type except an
Object, Interface, or Union type).
### Field Deprecation
Fields in an object may be marked as deprecated as deemed necessary by the
application. It is still legal to include these fields in a _selection set_ (to
ensure existing clients are not broken by the change), but the fields should be
appropriately treated in documentation and tooling.
When using the type system definition language, `@deprecated` directives are
used to indicate that a field is deprecated:
```graphql example
type ExampleType {
oldField: String @deprecated
}
```
### Object Extensions
ObjectTypeExtension :
- extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
- extend type Name ImplementsInterfaces? Directives[Const] [lookahead != `{`]
- extend type Name ImplementsInterfaces [lookahead != `{`]
Object type extensions are used to represent a type which has been extended from
some previous type. For example, this might be used to represent local data, or
by a GraphQL service which is itself an extension of another GraphQL service.
In this example, a local data field is added to a `Story` type:
```graphql example
extend type Story {
isHiddenLocally: Boolean
}
```
Object type extensions may choose not to add additional fields, instead only
adding interfaces or directives.
In this example, a directive is added to a `User` type without adding fields:
```graphql example
extend type User @addedDirective
```
**Type Validation**
Object type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be an Object type.
2. The fields of an Object type extension must have unique names; no two fields
may share the same name.
3. Any fields of an Object type extension must not be already defined on the
previous Object type.
4. Any non-repeatable directives provided must not already apply to the previous
Object type.
5. Any interfaces provided must not be already implemented by the previous
Object type.
6. The resulting extended object type must be a super-set of all interfaces it
implements.
## Interfaces
InterfaceTypeDefinition :
- Description? interface Name ImplementsInterfaces? Directives[Const]?
FieldsDefinition
- Description? interface Name ImplementsInterfaces? Directives[Const]?
[lookahead != `{`]
GraphQL interfaces represent a list of named fields and their arguments. GraphQL
objects and interfaces can then implement these interfaces which requires the
implementing type to define all fields defined by those interfaces.
Fields on a GraphQL interface have the same rules as fields on a GraphQL object;
their type can be Scalar, Object, Enum, Interface, or Union, or any wrapping
type whose base type is one of those five.
For example, an interface `NamedEntity` may describe a required field and types
such as `Person` or `Business` may then implement this interface to guarantee
this field will always exist.
Types may also implement multiple interfaces. For example, `Business` implements
both the `NamedEntity` and `ValuedEntity` interfaces in the example below.
```graphql example
interface NamedEntity {
name: String
}
interface ValuedEntity {
value: Int
}
type Person implements NamedEntity {
name: String
age: Int
}
type Business implements NamedEntity & ValuedEntity {
name: String
value: Int
employeeCount: Int
}
```
Fields which yield an interface are useful when one of many Object types are
expected, but some fields should be guaranteed.
To continue the example, a `Contact` might refer to `NamedEntity`.
```graphql example
type Contact {
entity: NamedEntity
phoneNumber: String
address: String
}
```
This allows us to write a _selection set_ for a `Contact` that can select the
common fields.
```graphql example
{
entity {
name
}
phoneNumber
}
```
When selecting fields on an interface type, only those fields declared on the
interface may be queried. In the above example, `entity` returns a
`NamedEntity`, and `name` is defined on `NamedEntity`, so it is valid. However,
the following would not be a valid selection set against `Contact`:
```graphql counter-example
{
entity {
name
age
}
phoneNumber
}
```
because `entity` refers to a `NamedEntity`, and `age` is not defined on that
interface. Querying for `age` is only valid when the result of `entity` is a
`Person`; this can be expressed using a fragment or an inline fragment:
```graphql example
{
entity {
name
... on Person {
age
}
}
phoneNumber
}
```
**Interfaces Implementing Interfaces**
When defining an interface that implements another interface, the implementing
interface must define each field that is specified by the implemented interface.
For example, the interface Resource must define the field id to implement the
Node interface:
```raw graphql example
interface Node {
id: ID!
}
interface Resource implements Node {
id: ID!
url: String
}
```
Transitively implemented interfaces (interfaces implemented by the interface
that is being implemented) must also be defined on an implementing type or
interface. For example, `Image` cannot implement `Resource` without also
implementing `Node`:
```raw graphql example
interface Node {
id: ID!
}
interface Resource implements Node {
id: ID!
url: String
}
interface Image implements Resource & Node {
id: ID!
url: String
thumbnail: String
}
```
Interface definitions must not contain cyclic references nor implement
themselves. This example is invalid because `Node` and `Named` implement
themselves and each other:
```graphql counter-example
interface Node implements Named & Node {
id: ID!
name: String
}
interface Named implements Node & Named {
id: ID!
name: String
}
```
**Result Coercion**
The interface type should have some way of determining which object a given
result corresponds to. Once it has done so, the result coercion of the interface
is the same as the result coercion of the object.
**Input Coercion**
Interfaces are never valid inputs.
**Type Validation**
Interface types have the potential to be invalid if incorrectly defined.
1. An Interface type must define one or more fields.
2. For each field of an Interface type:
1. The field must have a unique name within that Interface type; no two
fields may share the same name.
2. The field must not have a name which begins with the characters {"\_\_"}
(two underscores).
3. The field must return a type where {IsOutputType(fieldType)} returns
{true}.
4. For each argument of the field:
1. The argument must not have a name which begins with the characters
{"\_\_"} (two underscores).
2. The argument must have a unique name within that field; no two
arguments may share the same name.
3. The argument must accept a type where {IsInputType(argumentType)}
returns {true}.
3. An interface type may declare that it implements one or more unique
interfaces, but may not implement itself.
4. An interface type must be a super-set of all interfaces it implements:
1. Let this interface type be {implementingType}.
2. For each interface declared implemented as {implementedType},
{IsValidImplementation(implementingType, implementedType)} must be {true}.
### Interface Extensions
InterfaceTypeExtension :
- extend interface Name ImplementsInterfaces? Directives[Const]?
FieldsDefinition
- extend interface Name ImplementsInterfaces? Directives[Const] [lookahead !=
`{`]
- extend interface Name ImplementsInterfaces [lookahead != `{`]
Interface type extensions are used to represent an interface which has been
extended from some previous interface. For example, this might be used to
represent common local data on many types, or by a GraphQL service which is
itself an extension of another GraphQL service.
In this example, an extended data field is added to a `NamedEntity` type along
with the types which implement it:
```graphql example
extend interface NamedEntity {
nickname: String
}
extend type Person {
nickname: String
}
extend type Business {
nickname: String
}
```
Interface type extensions may choose not to add additional fields, instead only
adding directives.
In this example, a directive is added to a `NamedEntity` type without adding
fields:
```graphql example
extend interface NamedEntity @addedDirective
```
**Type Validation**
Interface type extensions have the potential to be invalid if incorrectly
defined.
1. The named type must already be defined and must be an Interface type.
2. The fields of an Interface type extension must have unique names; no two
fields may share the same name.
3. Any fields of an Interface type extension must not be already defined on the
previous Interface type.
4. Any Object or Interface type which implemented the previous Interface type
must also be a super-set of the fields of the Interface type extension (which
may be due to Object type extension).
5. Any non-repeatable directives provided must not already apply to the previous
Interface type.
6. The resulting extended Interface type must be a super-set of all Interfaces
it implements.
## Unions
UnionTypeDefinition : Description? union Name Directives[Const]?
UnionMemberTypes?
UnionMemberTypes :
- UnionMemberTypes | NamedType
- = `|`? NamedType
GraphQL Unions represent an object that could be one of a list of GraphQL Object
types, but provides for no guaranteed fields between those types. They also
differ from interfaces in that Object types declare what interfaces they
implement, but are not aware of what unions contain them.
With interfaces and objects, only those fields defined on the type can be
queried directly; to query other fields on an interface, typed fragments must be
used. This is the same as for unions, but unions do not define any fields, so
**no** fields may be queried on this type without the use of type refining
fragments or inline fragments (with the exception of the meta-field
{\_\_typename}).
For example, we might define the following types:
```graphql example
union SearchResult = Photo | Person
type Person {
name: String
age: Int
}
type Photo {
height: Int
width: Int
}
type SearchQuery {
firstSearchResult: SearchResult
}
```
In this example, a query operation wants the name if the result was a Person,
and the height if it was a photo. However because a union itself defines no
fields, this could be ambiguous and is invalid.
```graphql counter-example
{
firstSearchResult {
name
height
}
}
```
A valid operation includes typed fragments (in this example, inline fragments):
```graphql example
{
firstSearchResult {
... on Person {
name
}
... on Photo {
height
}
}
}
```
Union members may be defined with an optional leading `|` character to aid
formatting when representing a longer list of possible types:
```raw graphql example
union SearchResult =
| Photo
| Person
```
**Result Coercion**
The union type should have some way of determining which object a given result
corresponds to. Once it has done so, the result coercion of the union is the
same as the result coercion of the object.
**Input Coercion**
Unions are never valid inputs.
**Type Validation**
Union types have the potential to be invalid if incorrectly defined.
1. A Union type must include one or more unique member types.
2. The member types of a Union type must all be Object base types; Scalar,
Interface and Union types must not be member types of a Union. Similarly,
wrapping types must not be member types of a Union.
### Union Extensions
UnionTypeExtension :
- extend union Name Directives[Const]? UnionMemberTypes
- extend union Name Directives[Const]
Union type extensions are used to represent a union type which has been extended
from some previous union type. For example, this might be used to represent
additional local data, or by a GraphQL service which is itself an extension of
another GraphQL service.
**Type Validation**
Union type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be a Union type.
2. The member types of a Union type extension must all be Object base types;
Scalar, Interface and Union types must not be member types of a Union.
Similarly, wrapping types must not be member types of a Union.
3. All member types of a Union type extension must be unique.
4. All member types of a Union type extension must not already be a member of
the previous Union type.
5. Any non-repeatable directives provided must not already apply to the previous
Union type.
## Enums
EnumTypeDefinition :
- Description? enum Name Directives[Const]? EnumValuesDefinition
- Description? enum Name Directives[Const]? [lookahead != `{`]
EnumValuesDefinition : { EnumValueDefinition+ }
EnumValueDefinition : Description? EnumValue Directives[Const]?
GraphQL Enum types, like Scalar types, also represent leaf values in a GraphQL
type system. However Enum types describe the set of possible values.
Enums are not references for a numeric value, but are unique values in their own
right. They may serialize as a string: the name of the represented value.
In this example, an Enum type called `Direction` is defined:
```graphql example
enum Direction {
NORTH
EAST
SOUTH
WEST
}
```
**Result Coercion**
GraphQL services must return one of the defined set of possible values. If a
reasonable coercion is not possible they must raise an _execution error_.
**Input Coercion**
GraphQL has a constant literal to represent enum input values. GraphQL string
literals must not be accepted as an enum input and instead raise a request
error.
Variable transport serializations which have a different representation for
non-string symbolic values (for example,
[EDN](https://github.com/edn-format/edn)) should only allow such values as enum
input values. Otherwise, for most transport serializations that do not, strings
may be interpreted as the enum input value with the same name.
**Type Validation**
Enum types have the potential to be invalid if incorrectly defined.
1. An Enum type must define one or more unique enum values.
### Enum Extensions
EnumTypeExtension :
- extend enum Name Directives[Const]? EnumValuesDefinition
- extend enum Name Directives[Const] [lookahead != `{`]
Enum type extensions are used to represent an enum type which has been extended
from some previous enum type. For example, this might be used to represent
additional local data, or by a GraphQL service which is itself an extension of
another GraphQL service.
**Type Validation**
Enum type extensions have the potential to be invalid if incorrectly defined.
1. The named type must already be defined and must be an Enum type.
2. All values of an Enum type extension must be unique.
3. All values of an Enum type extension must not already be a value of the
previous Enum.
4. Any non-repeatable directives provided must not already apply to the previous
Enum type.
## Input Objects
InputObjectTypeDefinition :
- Description? input Name Directives[Const]? InputFieldsDefinition
- Description? input Name Directives[Const]? [lookahead != `{`]
InputFieldsDefinition : { InputValueDefinition+ }
Fields may accept arguments to configure their behavior. These inputs are often
scalars or enums, but they sometimes need to represent more complex values.
:: A GraphQL _Input Object_ defines a set of input fields; the input fields are
scalars, enums, other input objects, or any wrapping type whose underlying base
type is one of those three. This allows arguments to accept arbitrarily complex
structs.
In this example, an Input Object called `Point2D` describes `x` and `y` inputs:
```graphql example
input Point2D {
x: Float
y: Float
}
```
Note: The GraphQL Object type ({ObjectTypeDefinition}) defined above is
inappropriate for re-use here, because Object types can contain fields that
define arguments or contain references to interfaces and unions, neither of
which is appropriate for use as an input argument. For this reason, input
objects have a separate type in the system.
**Circular References**
Input Objects are allowed to reference other Input Objects as field types. A
circular reference occurs when an Input Object references itself either directly
or through referenced Input Objects.
Circular references are generally allowed, however they may not be defined as an
unbroken chain of Non-Null singular fields. Such Input Objects are invalid
because there is no way to provide a legal value for them.
This example of a circularly-referenced input type is valid as the field `self`
may be omitted or the value {null}.
```graphql example
input Example {
self: Example
value: String
}
```
This example is also valid as the field `self` may be an empty List.
```graphql example
input Example {
self: [Example!]!
value: String
}
```
This example of a circularly-referenced input type is invalid as the field
`self` cannot be provided a finite value.
```graphql counter-example
input Example {
self: Example!
value: String
}
```
This example is also invalid, as there is a non-null singular circular reference
via the `First.second` and `Second.first` fields.
```graphql counter-example
input First {
second: Second!
value: String
}
input Second {
first: First!
value: String
}
```
**Result Coercion**
An input object is never a valid result. Input Object types cannot be the return
type of an Object or Interface field.
**Input Coercion**
The value for an input object should be an input object literal or an unordered
map supplied by a variable, otherwise a _request error_ must be raised. In
either case, the input object literal or unordered map must not contain any
entries with names not defined by a field of this input object type, otherwise a
request error must be raised.
The result of coercion is an unordered map with an entry for each field both
defined by the input object type and for which a value exists. The resulting map
is constructed with the following rules:
- If no value is provided for a defined input object field and that field
definition provides a default value, the result of coercing the default value
according to the coercion rules of the input field type should be used. If no
default value is provided and the input object field's type is non-null, an
error should be raised. Otherwise, if the field is not required, then no entry
is added to the coerced unordered map.
- If the value {null} was provided for an input object field, and the field's
type is not a non-null type, an entry in the coerced unordered map is given
the value {null}. In other words, there is a semantic difference between the
explicitly provided value {null} versus having not provided a value.
- If a literal value is provided for an input object field, an entry in the
coerced unordered map is given the result of coercing that value according to
the input coercion rules for the type of that field.
- If a variable is provided for an input object field, the runtime value of that
variable must be used. If the runtime value is {null} and the field type is
non-null, an _execution error_ must be raised. If no runtime value is
provided, the variable definition's default value should be used. If the
variable definition does not provide a default value, the input object field
definition's default value should be used.
Following are examples of input coercion for an input object type with a
`String` field `a` and a required (non-null) `Int!` field `b`:
```graphql example
input ExampleInputObject {
a: String
b: Int!
}
```
| Literal Value | Variables | Coerced Value |
| ------------------------ | ----------------------- | ------------------------------------ |
| `{ a: "abc", b: 123 }` | `{}` | `{ a: "abc", b: 123 }` |
| `{ a: null, b: 123 }` | `{}` | `{ a: null, b: 123 }` |
| `{ b: 123 }` | `{}` | `{ b: 123 }` |
| `{ a: $var, b: 123 }` | `{ var: null }` | `{ a: null, b: 123 }` |
| `{ a: $var, b: 123 }` | `{}` | `{ b: 123 }` |
| `{ b: $var }` | `{ var: 123 }` | `{ b: 123 }` |
| `$var` | `{ var: { b: 123 } }` | `{ b: 123 }` |
| `"abc123"` | `{}` | Error: Incorrect value |
| `$var` | `{ var: "abc123" }` | Error: Incorrect value |
| `{ a: "abc", b: "123" }` | `{}` | Error: Incorrect value for field {b} |
| `{ a: "abc" }` | `{}` | Error: Missing required field {b} |
| `{ b: $var }` | `{}` | Error: Missing required field {b}. |
| `$var` | `{ var: { a: "abc" } }` | Error: Missing required field {b} |
| `{ a: "abc", b: null }` | `{}` | Error: {b} must be non-null. |
| `{ b: $var }` | `{ var: null }` | Error: {b} must be non-null. |
| `{ b: 123, c: "xyz" }` | `{}` | Error: Unexpected field {c} |
**Type Validation**
1. An Input Object type must define one or more input fields.
2. For each input field of an Input Object type:
1. The input field must have a unique name within that Input Object type; no
two input fields may share the same name.
2. The input field must not have a name which begins with the characters
{"\_\_"} (two underscores).
3. The input field must accept a type where {IsInputType(inputFieldType)}
returns {true}.
4. If input field type is Non-Null and a default value is not defined:
1. The `@deprecated` directive must not be applied to this input field.
5. If the Input Object is a _OneOf Input Object_ then:
1. The type of the input field must be nullable.
2. The input field must not have a default value.
3. If an Input Object references itself either directly or through referenced
Input Objects, at least one of the fields in the chain of references must be
either a nullable or a List type.
4. {InputObjectDefaultValueHasCycle(inputObject)} must be {false}.
InputObjectDefaultValueHasCycle(inputObject, defaultValue, visitedFields):
- If {defaultValue} is not provided, initialize it to an empty unordered map.
- If {visitedFields} is not provided, initialize it to the empty set.
- If {defaultValue} is a list:
- For each {itemValue} in {defaultValue}:
- If {InputObjectDefaultValueHasCycle(inputObject, itemValue,
visitedFields)}, return {true}.
- Otherwise, if {defaultValue} is an unordered map:
- For each field {field} in {inputObject}:
- If {InputFieldDefaultValueHasCycle(field, defaultValue, visitedFields)},
return {true}.
- Return {false}.
InputFieldDefaultValueHasCycle(field, defaultValue, visitedFields):
- Assert: {defaultValue} is an unordered map.
- Let {fieldType} be the type of {field}.
- Let {namedFieldType} be the underlying named type of {fieldType}.
- If {namedFieldType} is not an input object type:
- Return {false}.
- Let {fieldName} be the name of {field}.
- Let {fieldDefaultValue} be the value for {fieldName} in {defaultValue}.
- If {fieldDefaultValue} exists:
- Return {InputObjectDefaultValueHasCycle(namedFieldType, fieldDefaultValue,
visitedFields)}.
- Otherwise:
- Let {fieldDefaultValue} be the default value of {field}.
- If {fieldDefaultValue} does not exist:
- Return {false}.
- If {field} is within {visitedFields}:
- Return {true}.
- Let {nextVisitedFields} be a new set containing {field} and everything from
{visitedFields}.
- Return {InputObjectDefaultValueHasCycle(namedFieldType, fieldDefaultValue,
nextVisitedFields)}.
### OneOf Input Objects
:: A _OneOf Input Object_ is a special variant of _Input Object_ where exactly
one field must be set and non-null, all others being omitted. This is useful for
representing situations where an input may be one of many different options.
When using the type system definition language, the [`@oneOf`](#sec--oneOf)
directive is used to indicate that an Input Object is a OneOf Input Object (and
thus requires exactly one of its fields be provided):
```graphql
input UserUniqueCondition @oneOf {
id: ID
username: String
organizationAndEmail: OrganizationAndEmailInput
}
```
In schema introspection, the `__Type.isOneOf` field will return {true} for OneOf
Input Objects, and {false} for all other Input Objects.
**Input Coercion**
The value of a OneOf Input Object, as a variant of Input Object, must also be an
input object literal or an unordered map supplied by a variable, otherwise a
_request error_ must be raised.
- Prior to construction of the coerced map via the input coercion rules of an
_Input Object_: the value to be coerced must contain exactly one entry and
that entry must not be {null} or the {null} literal, otherwise a _request
error_ must be raised.
- All _Input Object_ [input coercion rules](#sec-Input-Objects.Input-Coercion)
must also apply to a _OneOf Input Object_.
- The resulting coerced map must contain exactly one entry and the value for
that entry must not be {null}, otherwise an _execution error_ must be raised.
Following are additional examples of input coercion for a OneOf Input Object
type with a `String` member field `a` and an `Int` member field `b`:
```graphql example
input ExampleOneOfInputObject @oneOf {
a: String
b: Int
}
```
| Literal Value | Variables | Coerced Value |
| ----------------------- | ------------------------------- | --------------------------------------------------- |
| `{ a: "abc" }` | `{}` | `{ a: "abc" }` |
| `{ b: 123 }` | `{}` | `{ b: 123 }` |
| `$var` | `{ var: { a: "abc" } }` | `{ a: "abc" }` |
| `{ a: null }` | `{}` | Error: Value for member field {a} must be non-null |
| `$var` | `{ var: { a: null } }` | Error: Value for member field {a} must be non-null |
| `{ a: $a }` | `{}` | Error: Value for member field {a} must be specified |
| `{ a: "abc", b: 123 }` | `{}` | Error: Exactly one key must be specified |
| `{ a: 456, b: "xyz" }` | `{}` | Error: Exactly one key must be specified |
| `$var` | `{ var: { a: "abc", b: 123 } }` | Error: Exactly one key must be specified |
| `{ a: "abc", b: null }` | `{}` | Error: Exactly one key must be specified |
| `{ a: "abc", b: $b }` | `{}` | Error: Exactly one key must be specified |
| `{ a: $a, b: $b }` | `{ a: "abc" }` | Error: Exactly one key must be specified |
| `{}` | `{}` | Error: Exactly one key must be specified |
| `$var` | `{ var: {} }` | Error: Exactly one key must be specified |
### Input Object Extensions
InputObjectTypeExtension :
- extend input Name Directives[Const]? InputFieldsDefinition
- extend input Name Directives[Const] [lookahead != `{`]
Input object type extensions are used to represent an input object type which
has been extended from some previous input object type. For example, this might
be used by a GraphQL service which is itself an extension of another GraphQL
service.
**Type Validation**
Input object type extensions have the potential to be invalid if incorrectly
defined.
1. The named type must already be defined and must be a Input Object type.
2. All fields of an Input Object type extension must have unique names.
3. All fields of an Input Object type extension must not already be a field of
the previous Input Object.
4. Any non-repeatable directives provided must not already apply to the previous
Input Object type.
5. The `@oneOf` directive must not be provided by an Input Object type
extension.
6. If the original Input Object is a _OneOf Input Object_ then:
1. All fields of the Input Object type extension must be nullable.
2. All fields of the Input Object type extension must not have default
values.
## List
A GraphQL list is a special collection type which declares the type of each item
in the List (referred to as the _item type_ of the list). List values are
serialized as ordered lists, where each item in the list is serialized as per
the item type.
To denote that a field uses a List type the item type is wrapped in square
brackets like this: `pets: [Pet]`. Nesting lists is allowed: `matrix: [[Int]]`.
**Result Coercion**
GraphQL services must return an ordered list as the result of a list type. Each
item in the list must be the result of a result coercion of the item type. If a
reasonable coercion is not possible it must raise an _execution error_. In
particular, if a non-list is returned, the coercion should fail, as this
indicates a mismatch in expectations between the type system and the
implementation.
If a list's item type is nullable, then errors occurring during preparation or
coercion of an individual item in the list must result in the value {null} at
that position in the list along with an _execution error_ added to the response.
If a list's item type is non-null, an execution error occurring at an individual
item in the list must result in an execution error for the entire list.
Note: See [Handling Execution Errors](#sec-Handling-Execution-Errors) for more
about this behavior.
**Input Coercion**
When expected as an input, list values are accepted only when each item in the
list can be accepted by the list's item type.
If the value passed as an input to a list type is _not_ a list and not the
{null} value, then the result of input coercion is a list of size one, where the
single item value is the result of input coercion for the list's item type on
the provided value (note this may apply recursively for nested lists).
This allows inputs which accept one or many arguments (sometimes referred to as
"var args") to declare their input type as a list while for the common case of a
single value, a client can just pass that value directly rather than
constructing the list.
Following are examples of input coercion with various list types and values:
| Expected Type | Provided Value | Coerced Value |
| ------------- | ---------------- | --------------------------- |
| `[Int]` | `[1, 2, 3]` | `[1, 2, 3]` |
| `[Int]` | `[1, "b", true]` | Error: Incorrect item value |
| `[Int]` | `1` | `[1]` |
| `[Int]` | `null` | `null` |
| `[[Int]]` | `[[1], [2, 3]]` | `[[1], [2, 3]]` |
| `[[Int]]` | `[1, 2, 3]` | `[[1], [2], [3]]` |
| `[[Int]]` | `[1, null, 3]` | `[[1], null, [3]]` |
| `[[Int]]` | `[[1], ["b"]]` | Error: Incorrect item value |
| `[[Int]]` | `1` | `[[1]]` |
| `[[Int]]` | `null` | `null` |
## Non-Null
By default, all types in GraphQL are nullable; the {null} value is a valid
response for all of the above types. To declare a type that disallows null, the
GraphQL Non-Null type can be used. This type wraps an underlying type, and this
type acts identically to that wrapped type, with the exception that {null} is
not a valid response for the wrapping type. A trailing exclamation mark is used
to denote a field that uses a Non-Null type like this: `name: String!`.
**Nullable vs. Optional**
Fields are _always_ optional within the context of a _selection set_, a field
may be omitted and the selection set is still valid (so long as the selection
set does not become empty). However fields that return Non-Null types will never
return the value {null} if queried.
Inputs (such as field arguments), are always optional by default. However a
non-null input type is required. In addition to not accepting the value {null},
it also does not accept omission. For the sake of simplicity nullable types are
always optional and non-null types are always required.
**Result Coercion**
In all of the above result coercions, {null} was considered a valid value. To
coerce the result of a Non-Null type, the coercion of the wrapped type should be
performed. If that result was not {null}, then the result of coercing the
Non-Null type is that result. If that result was {null}, then an _execution
error_ must be raised.
Note: When an _execution error_ is raised on a non-null _response position_, the
error propagates to the parent _response position_. For more information on this
process, see
[Errors and Non-Null Types](#sec-Executing-Selection-Sets.Errors-and-Non-Null-Types)
within the Execution section.
**Input Coercion**
If an argument or input-object field of a Non-Null type is not provided, is
provided with the literal value {null}, or is provided with a variable that was
either not provided a value at runtime, or was provided the value {null}, then a
_request error_ must be raised.
If the value provided to the Non-Null type is provided with a literal value
other than {null}, or a Non-Null variable value, it is coerced using the input
coercion for the wrapped type.
A non-null argument cannot be omitted:
```graphql counter-example
{
fieldWithNonNullArg
}
```
The value {null} cannot be provided to a non-null argument:
```graphql counter-example
{
fieldWithNonNullArg(nonNullArg: null)
}
```
A variable of a nullable type cannot be provided to a non-null argument:
```graphql example
query withNullableVariable($var: String) {
fieldWithNonNullArg(nonNullArg: $var)
}
```
Note: The Validation section defines providing a nullable variable type to a
non-null input type as invalid.
**Type Validation**
1. A Non-Null type must not wrap another Non-Null type.
### Combining List and Non-Null
The List and Non-Null wrapping types can compose, representing more complex
types. The rules for result coercion and input coercion of Lists and Non-Null
types apply in a recursive fashion.
For example if the inner item type of a List is Non-Null (e.g. `[T!]`), then
that List may not contain any {null} items. However if the inner type of a
Non-Null is a List (e.g. `[T]!`), then {null} is not accepted however an empty
list is accepted.
Following are examples of result coercion with various types and values:
| Expected Type | Internal Value | Coerced Result |
| ------------- | --------------- | ----------------------------------- |
| `[Int]` | `[1, 2, 3]` | `[1, 2, 3]` |
| `[Int]` | `null` | `null` |
| `[Int]` | `[1, 2, null]` | `[1, 2, null]` |
| `[Int]` | `[1, 2, Error]` | `[1, 2, null]` (With logged error) |
| `[Int]!` | `[1, 2, 3]` | `[1, 2, 3]` |
| `[Int]!` | `null` | Error: Value cannot be null |
| `[Int]!` | `[1, 2, null]` | `[1, 2, null]` |
| `[Int]!` | `[1, 2, Error]` | `[1, 2, null]` (With logged error) |
| `[Int!]` | `[1, 2, 3]` | `[1, 2, 3]` |
| `[Int!]` | `null` | `null` |
| `[Int!]` | `[1, 2, null]` | `null` (With logged coercion error) |
| `[Int!]` | `[1, 2, Error]` | `null` (With logged error) |
| `[Int!]!` | `[1, 2, 3]` | `[1, 2, 3]` |
| `[Int!]!` | `null` | Error: Value cannot be null |
| `[Int!]!` | `[1, 2, null]` | Error: Item cannot be null |
| `[Int!]!` | `[1, 2, Error]` | Error: Error occurred in item |
## Directives
DirectiveDefinition : Description? directive @ Name ArgumentsDefinition?
`repeatable`? on DirectiveLocations
DirectiveLocations :
- DirectiveLocations | DirectiveLocation
- `|`? DirectiveLocation
DirectiveLocation :
- ExecutableDirectiveLocation
- TypeSystemDirectiveLocation
ExecutableDirectiveLocation : one of
- `QUERY`
- `MUTATION`
- `SUBSCRIPTION`
- `FIELD`
- `FRAGMENT_DEFINITION`
- `FRAGMENT_SPREAD`
- `INLINE_FRAGMENT`
- `VARIABLE_DEFINITION`
TypeSystemDirectiveLocation : one of
- `SCHEMA`
- `SCALAR`
- `OBJECT`
- `FIELD_DEFINITION`
- `ARGUMENT_DEFINITION`
- `INTERFACE`
- `UNION`
- `ENUM`
- `ENUM_VALUE`
- `INPUT_OBJECT`
- `INPUT_FIELD_DEFINITION`
A GraphQL schema describes directives which are used to annotate various parts
of a GraphQL document as an indicator that they should be evaluated differently
by a validator, executor, or client tool such as a code generator.
**Built-in Directives**
:: A _built-in directive_ is any directive defined within this specification.
GraphQL implementations should provide the `@skip` and `@include` directives.
GraphQL implementations that support the type system definition language must
provide the `@deprecated` directive if representing deprecated portions of the
schema.
GraphQL implementations that support the type system definition language should
provide the `@specifiedBy` directive if representing custom scalar definitions.
GraphQL implementations that support the type system definition language should
provide the `@oneOf` directive if representing OneOf Input Objects.
When representing a GraphQL schema using the type system definition language any
_built-in directive_ may be omitted for brevity.
When introspecting a GraphQL service all provided directives, including any
_built-in directive_, must be included in the set of returned directives.
**Custom Directives**
:: GraphQL services and client tooling may provide any additional _custom
directive_ beyond those defined in this document. Directives are the preferred
way to extend GraphQL with custom or experimental behavior.
Note: When defining a _custom directive_, it is recommended to prefix the
directive's name to make its scope of usage clear and to prevent a collision
with _built-in directive_ which may be specified by future versions of this
document (which will not include `_` in their name). For example, a _custom
directive_ used by Facebook's GraphQL service should be named `@fb_auth` instead
of `@auth`. This is especially recommended for proposed additions to this
specification which can change during the
[RFC process](https://github.com/graphql/graphql-spec/blob/main/CONTRIBUTING.md).
For example a work in progress version of `@live` should be named `@rfc_live`.
Directives must only be used in the locations they are declared to belong in. In
this example, a directive is defined which can be used to annotate a field:
```graphql example
directive @example on FIELD
fragment SomeFragment on SomeType {
field @example
}
```
Directive locations may be defined with an optional leading `|` character to aid
formatting when representing a longer list of possible locations:
```raw graphql example
directive @example on
| FIELD
| FRAGMENT_SPREAD
| INLINE_FRAGMENT
```
Directives can also be used to annotate the type system definition language as
well, which can be a useful tool for supplying additional metadata in order to
generate GraphQL execution services, produce client generated runtime code, or
many other useful extensions of the GraphQL semantics.
In this example, the directive `@example` annotates field and argument
definitions:
```graphql example
directive @example on FIELD_DEFINITION | ARGUMENT_DEFINITION
type SomeType {
field(arg: Int @example): String @example
}
```
A directive may be defined as repeatable by including the "repeatable" keyword.
Repeatable directives are often useful when the same directive should be used
with different arguments at a single location, especially in cases where
additional information needs to be provided to a type or schema extension via a
directive:
```graphql example
directive @delegateField(name: String!) repeatable on OBJECT | INTERFACE
type Book @delegateField(name: "pageCount") @delegateField(name: "author") {
id: ID!
}
extend type Book @delegateField(name: "index")
```
While defining a directive, it must not reference itself directly or indirectly:
```graphql counter-example
directive @invalidExample(arg: String @invalidExample) on ARGUMENT_DEFINITION
```
Note: The order in which directives appear may be significant, including
repeatable directives.
**Type Validation**
1. A Directive definition must include at least one DirectiveLocation.
2. A Directive definition must not contain the use of a Directive which
references itself directly.
3. A Directive definition must not contain the use of a Directive which
references itself indirectly by referencing a Type or Directive which
transitively includes a reference to this Directive.
4. The Directive must not have a name which begins with the characters {"\_\_"}
(two underscores).
5. For each argument of the Directive:
1. The argument must not have a name which begins with the characters
{"\_\_"} (two underscores).
2. The argument must have a unique name within that Directive; no two
arguments may share the same name.
3. The argument must accept a type where {IsInputType(argumentType)} returns
{true}.
### @skip
```graphql
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
```
The `@skip` _built-in directive_ may be provided for fields, fragment spreads,
and inline fragments, and allows for conditional exclusion during execution as
described by the `if` argument.
In this example `experimentalField` will only be queried if the variable
`$someTest` has the value `false`.
```graphql example
query myQuery($someTest: Boolean!) {
experimentalField @skip(if: $someTest)
}
```
### @include
```graphql
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
```
The `@include` _built-in directive_ may be provided for fields, fragment
spreads, and inline fragments, and allows for conditional inclusion during
execution as described by the `if` argument.
In this example `experimentalField` will only be queried if the variable
`$someTest` has the value `true`
```graphql example
query myQuery($someTest: Boolean!) {
experimentalField @include(if: $someTest)
}
```
Note: Neither `@skip` nor `@include` has precedence over the other. In the case
that both the `@skip` and `@include` directives are provided on the same field
or fragment, it _must_ be queried only if the `@skip` condition is false _and_
the `@include` condition is true. Stated conversely, the field or fragment must
_not_ be queried if either the `@skip` condition is true _or_ the `@include`
condition is false.
### @deprecated
```graphql
directive @deprecated(
reason: String! = "No longer supported"
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE
```
The `@deprecated` _built-in directive_ is used within the type system definition
language to indicate deprecated portions of a GraphQL service's schema, such as
deprecated fields on a type, arguments on a field, input fields on an input
type, or values of an enum type.
Deprecations include a reason for why it is deprecated, which is formatted using
Markdown syntax (as specified by [CommonMark](https://commonmark.org/)).
In this example type definition, `oldField` is deprecated in favor of using
`newField` and `oldArg` is deprecated in favor of using `newArg`.
```graphql example
type ExampleType {
newField: String
oldField: String @deprecated(reason: "Use `newField`.")
anotherField(
newArg: String
oldArg: String @deprecated(reason: "Use `newArg`.")
): String
}
```
The `@deprecated` directive must not appear on required (non-null without a
default) arguments or input object field definitions.
```graphql counter-example
type ExampleType {
invalidField(
newArg: String
oldArg: String! @deprecated(reason: "Use `newArg`.")
): String
}
```
To deprecate a required argument or input field, it must first be made optional
by either changing the type to nullable or adding a default value.
### @specifiedBy
```graphql
directive @specifiedBy(url: String!) on SCALAR
```
The `@specifiedBy` _built-in directive_ is used within the type system
definition language to provide a _scalar specification URL_ for specifying the
behavior of [custom scalar types](#sec-Scalars.Custom-Scalars). The URL should
point to a human-readable specification of the data format, serialization, and
coercion rules. It must not appear on built-in scalar types.
Note: Details on implementing a GraphQL scalar specification can be found in the
[scalars.graphql.org implementation guide](https://scalars.graphql.org/implementation-guide).
In this example, a custom scalar type for `UUID` is defined with a URL pointing
to the relevant IETF specification.
```graphql example
scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
```
### @oneOf
```graphql
directive @oneOf on INPUT_OBJECT
```
The `@oneOf` _built-in directive_ is used within the type system definition
language to indicate an _Input Object_ is a _OneOf Input Object_.
```graphql example
input UserUniqueCondition @oneOf {
id: ID
username: String
organizationAndEmail: OrganizationAndEmailInput
}
```
================================================
FILE: spec/Section 4 -- Introspection.md
================================================
# Introspection
A GraphQL service supports introspection over its schema. This schema is queried
using GraphQL itself, creating a powerful platform for tool-building.
Take an example request for a trivial app. In this case there is a User type
with three fields: id, name, and birthday.
For example, given a service with the following type definition:
```graphql example
type User {
id: String
name: String
birthday: Date
}
```
A request containing the operation:
```graphql example
{
__type(name: "User") {
name
fields {
name
type {
name
}
}
}
}
```
would produce the result:
```json example
{
"__type": {
"name": "User",
"fields": [
{
"name": "id",
"type": { "name": "String" }
},
{
"name": "name",
"type": { "name": "String" }
},
{
"name": "birthday",
"type": { "name": "Date" }
}
]
}
}
```
**Reserved Names**
Types and fields required by the GraphQL introspection system that are used in
the same context as user defined types and fields are prefixed with {"\_\_"}
(two underscores), in order to avoid naming collisions with user defined GraphQL
types.
Otherwise, any {Name} within a GraphQL type system must not start with two
underscores {"\_\_"}.
## Type Name Introspection
GraphQL supports type name introspection within any _selection set_ in an
operation, with the single exception of selections at the root of a subscription
operation. Type name introspection is accomplished via the meta-field
`__typename: String!` on any Object, Interface, or Union. It returns the name of
the concrete Object type at that point during execution.
This is most often used when querying against Interface or Union types to
identify which actual Object type of the possible types has been returned.
As a meta-field, `__typename` is implicit and does not appear in the fields list
in any defined type.
Note: `__typename` may not be included as a root field in a subscription
operation.
## Schema Introspection
The schema introspection system is accessible from the meta-fields `__schema`
and `__type` which are accessible from the type of the root of a query
operation.
```graphql
__schema: __Schema!
__type(name: String!): __Type
```
Like all meta-fields, these are implicit and do not appear in the fields list in
the root type of the query operation.
**First Class Documentation**
All types in the introspection system provide a `description` field of type
`String` to allow type designers to publish documentation in addition to
capabilities. A GraphQL service may return the `description` field using
Markdown syntax (as specified by [CommonMark](https://commonmark.org/)).
Therefore it is recommended that any tool that displays `description` use a
CommonMark-compliant Markdown renderer.
**Deprecation**
To support the management of backwards compatibility, GraphQL fields, arguments,
input fields, and enum values can indicate whether or not they are deprecated
(`isDeprecated: Boolean!`) along with a description of why it is deprecated
(`deprecationReason: String`).
Tools built using GraphQL introspection should respect deprecation by
discouraging deprecated use through information hiding or developer-facing
warnings.
**Stable Ordering**
The observable order of all data collections should be preserved to improve
schema legibility and stability. When a schema is produced from a
{TypeSystemDocument}, introspection should return items in the same source order
for each element list: object fields, input object fields, arguments, enum
values, directives, union member types, and implemented interfaces.
**Schema Introspection Schema**
The schema introspection system is itself represented as a GraphQL schema. Below
are the full set of type system definitions providing schema introspection,
which are fully defined in the sections below.
```graphql
type __Schema {
description: String
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}
type __Type {
kind: __TypeKind!
name: String
description: String
# may be non-null for custom SCALAR, otherwise null.
specifiedByURL: String
# must be non-null for OBJECT and INTERFACE, otherwise null.
fields(includeDeprecated: Boolean! = false): [__Field!]
# must be non-null for OBJECT and INTERFACE, otherwise null.
interfaces: [__Type!]
# must be non-null for INTERFACE and UNION, otherwise null.
possibleTypes: [__Type!]
# must be non-null for ENUM, otherwise null.
enumValues(includeDeprecated: Boolean! = false): [__EnumValue!]
# must be non-null for INPUT_OBJECT, otherwise null.
inputFields(includeDeprecated: Boolean! = false): [__InputValue!]
# must be non-null for NON_NULL and LIST, otherwise null.
ofType: __Type
# must be non-null for INPUT_OBJECT, otherwise null.
isOneOf: Boolean
}
enum __TypeKind {
SCALAR
OBJECT
INTERFACE
UNION
ENUM
INPUT_OBJECT
LIST
NON_NULL
}
type __Field {
name: String!
description: String
args(includeDeprecated: Boolean! = false): [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
}
type __InputValue {
name: String!
description: String
type: __Type!
defaultValue: String
isDeprecated: Boolean!
deprecationReason: String
}
type __EnumValue {
name: String!
description: String
isDeprecated: Boolean!
deprecationReason: String
}
type __Directive {
name: String!
description: String
isRepeatable: Boolean!
locations: [__DirectiveLocation!]!
args(includeDeprecated: Boolean! = false): [__InputValue!]!
}
enum __DirectiveLocation {
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
VARIABLE_DEFINITION
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
}
```
### The \_\_Schema Type
The `__Schema` type is returned from the `__schema` meta-field and provides all
information about the schema of a GraphQL service.
Fields\:
- `description` may return a String or {null}.
- `queryType` is the root type of a query operation.
- `mutationType` is the root type of a mutation operation, if supported.
Otherwise {null}.
- `subscriptionType` is the root type of a subscription operation, if supported.
Otherwise {null}.
- `types` must return the set of all named types contained within this schema.
Any named type which can be found through a field of any introspection type
must be included in this set.
- `directives` must return the set of all directives available within this
schema including all built-in directives.
### The \_\_Type Type
`__Type` is at the core of the type introspection system. It represents all
types in the system: both named types (e.g. Scalars and Object types) and type
modifiers (e.g. List and Non-Null types).
Type modifiers are used to modify the type presented in the field `ofType`. This
modified type may recursively be a modified type, representing a list or
non-null type, and combinations thereof, ultimately modifying a named type.
There are several different kinds of type. In each kind, different fields are
actually valid. All possible kinds are listed in the `__TypeKind` enum.
Each sub-section below defines the expected fields of `__Type` given each
possible value of the `__TypeKind` enum:
- {"SCALAR"}
- {"OBJECT"}
- {"INTERFACE"}
- {"UNION"}
- {"ENUM"}
- {"INPUT_OBJECT"}
- {"LIST"}
- {"NON_NULL"}
**Scalar**
Represents scalar types such as Int, String, and Boolean. Scalars cannot have
fields.
Also represents [Custom scalars](#sec-Scalars.Custom-Scalars) which may provide
`specifiedByURL` as a _scalar specification URL_.
Fields\:
- `kind` must return `__TypeKind.SCALAR`.
- `name` must return a String.
- `description` may return a String or {null}.
- `specifiedByURL` may return a String (in the form of a URL) for custom
scalars, otherwise must be {null}.
- All other fields must return {null}.
**Object**
Object types represent concrete instantiations of sets of fields. The
introspection types (e.g. `__Type`, `__Field`, etc.) are examples of objects.
Fields\:
- `kind` must return `__TypeKind.OBJECT`.
- `name` must return a String.
- `description` may return a String or {null}.
- `fields` must return the set of fields that can be selected for this type.
- Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated fields are also returned.
- `interfaces` must return the set of interfaces that an object implements (if
none, `interfaces` must return the empty set).
- All other fields must return {null}.
**Union**
Unions are an abstract type where no common fields are declared. The possible
types of a union are explicitly listed out in `possibleTypes`. An object type
can be a member of a union without modification to that type.
Fields\:
- `kind` must return `__TypeKind.UNION`.
- `name` must return a String.
- `description` may return a String or {null}.
- `possibleTypes` returns the list of types that can be represented within this
union. They must be object types.
- All other fields must return {null}.
**Interface**
Interfaces are an abstract type where there are common fields declared. Any type
that implements an interface must define all the named fields where each
implementing field type is equal to or a sub-type of (covariant) the interface
type. The implementations of this interface are explicitly listed out in
`possibleTypes`.
Fields\:
- `kind` must return `__TypeKind.INTERFACE`.
- `name` must return a String.
- `description` may return a String or {null}.
- `fields` must return the set of fields required by this interface.
- Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated fields are also returned.
- `interfaces` must return the set of interfaces that an interface implements
(if none, `interfaces` must return the empty set).
- `possibleTypes` returns the list of types that implement this interface. They
must be object types.
- All other fields must return {null}.
**Enum**
Enums are special scalars that can only have a defined set of values.
Fields\:
- `kind` must return `__TypeKind.ENUM`.
- `name` must return a String.
- `description` may return a String or {null}.
- `enumValues` must return the set of enum values as a list of `__EnumValue`.
There must be at least one and they must have unique names.
- Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated enum values are also returned.
- All other fields must return {null}.
**Input Object**
Input objects are composite types defined as a list of named input values. They
are only used as inputs to arguments and variables and cannot be a field return
type.
For example the input object `Point` could be defined as:
```graphql example
input Point {
x: Int
y: Int
}
```
Fields\:
- `kind` must return `__TypeKind.INPUT_OBJECT`.
- `name` must return a String.
- `description` may return a String or {null}.
- `inputFields` must return the set of input fields as a list of `__InputValue`.
- Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated input fields are also returned.
- `isOneOf` must return {true} when representing a _OneOf Input Object_,
otherwise {false}.
- All other fields must return {null}.
**List**
Lists represent sequences of values in GraphQL. A List type is a type modifier:
it wraps another type instance in the `ofType` field, which defines the type of
each item in the list.
The modified type in the `ofType` field may itself be a modified type, allowing
the representation of Lists of Lists, or Lists of Non-Nulls.
Fields\:
- `kind` must return `__TypeKind.LIST`.
- `ofType` must return a type of any kind.
- All other fields must return {null}.
**Non-Null**
GraphQL types are nullable. The value {null} is a valid response for field type.
A Non-Null type is a type modifier: it wraps another type instance in the
`ofType` field. Non-null types do not allow {null} as a response, and indicate
required inputs for arguments and input object fields.
The modified type in the `ofType` field may itself be a modified List type,
allowing the representation of Non-Null of Lists. However it must not be a
modified Non-Null type to avoid a redundant Non-Null of Non-Null.
Fields\:
- `kind` must return `__TypeKind.NON_NULL`.
- `ofType` must return a type of any kind except Non-Null.
- All other fields must return {null}.
### The \_\_Field Type
The `__Field` type represents each field in an Object or Interface type.
Fields\:
- `name` must return a String.
- `description` may return a String or {null}.
- `args` returns a List of `__InputValue` representing the arguments this field
accepts.
- Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated arguments are also returned.
- `type` must return a `__Type` that represents the type of value returned by
this field.
- `isDeprecated` returns {true} if this field should no longer be used,
otherwise {false}.
- `deprecationReason` returns the reason why this field is deprecated, or null
if this field is not deprecated.
### The \_\_InputValue Type
The `__InputValue` type represents field and directive arguments as well as the
`inputFields` of an input object.
Fields\:
- `name` must return a String.
- `description` may return a String or {null}.
- `type` must return a `__Type` that represents the type this input value
expects.
- `defaultValue` may return a String encoding (using the GraphQL language) of
the default value used by this input value in the condition a value is not
provided at runtime. If this input value has no default value, returns {null}.
- `isDeprecated` returns {true} if this input field or argument should no longer
be used, otherwise {false}.
- `deprecationReason` returns the reason why this input field or argument is
deprecated, or null if the input field or argument is not deprecated.
### The \_\_EnumValue Type
The `__EnumValue` type represents one of possible values of an enum.
Fields\:
- `name` must return a String.
- `description` may return a String or {null}.
- `isDeprecated` returns {true} if this enum value should no longer be used,
otherwise {false}.
- `deprecationReason` returns the reason why this enum value is deprecated, or
null if the enum value is not deprecated.
### The \_\_Directive Type
The `__Directive` type represents a directive that a service supports.
This includes both any _built-in directive_ and any _custom directive_.
Individual directives may only be used in locations that are explicitly
supported. All possible locations are listed in the `__DirectiveLocation` enum:
- {"QUERY"}
- {"MUTATION"}
- {"SUBSCRIPTION"}
- {"FIELD"}
- {"FRAGMENT_DEFINITION"}
- {"FRAGMENT_SPREAD"}
- {"INLINE_FRAGMENT"}
- {"VARIABLE_DEFINITION"}
- {"SCHEMA"}
- {"SCALAR"}
- {"OBJECT"}
- {"FIELD_DEFINITION"}
- {"ARGUMENT_DEFINITION"}
- {"INTERFACE"}
- {"UNION"}
- {"ENUM"}
- {"ENUM_VALUE"}
- {"INPUT_OBJECT"}
- {"INPUT_FIELD_DEFINITION"}
Fields\:
- `name` must return a String.
- `description` may return a String or {null}.
- `locations` returns a List of `__DirectiveLocation` representing the valid
locations this directive may be placed.
- `args` returns a List of `__InputValue` representing the arguments this
directive accepts.
- Accepts the argument `includeDeprecated` which defaults to {false}. If
{true}, deprecated arguments are also returned.
- `isRepeatable` must return a Boolean that indicates if the directive may be
used repeatedly at a single location.
================================================
FILE: spec/Section 5 -- Validation.md
================================================
# Validation
A GraphQL service does not just verify if a request is syntactically correct,
but also ensures that it is unambiguous and mistake-free in the context of a
given GraphQL schema.
An invalid request is still technically executable, and will always produce a
stable result as defined by the algorithms in the Execution section, however
that result may be ambiguous, surprising, or unexpected relative to a request
containing validation errors, so execution should only occur for valid requests.
Typically validation is performed in the context of a request immediately before
execution, however a GraphQL service may execute a request without explicitly
validating it if that exact same request is known to have been validated before.
For example: the request may be validated during development, provided it does
not later change, or a service may validate a request once and memoize the
result to avoid validating the same request again in the future. Any client-side
or development-time tool should report validation errors and not allow the
formulation or execution of requests known to be invalid at that given point in
time.
**Type System Evolution**
As GraphQL type system schema evolves over time by adding new types and new
fields, it is possible that a request which was previously valid could later
become invalid. Any change that can cause a previously valid request to become
invalid is considered a _breaking change_. GraphQL services and schema
maintainers are encouraged to avoid breaking changes, however in order to be
more resilient to these breaking changes, sophisticated GraphQL systems may
still allow for the execution of requests which _at some point_ were known to be
free of any validation errors, and have not changed since.
**Examples**
The examples in this section will use the following types:
```graphql example
type Query {
dog: Dog
findDog(searchBy: FindDogInput): Dog
}
type Mutation {
addPet(pet: PetInput!): Pet
addPets(pets: [PetInput!]!): [Pet]
}
enum DogCommand {
SIT
DOWN
HEEL
}
type Dog implements Pet {
name: String!
nickname: String
barkVolume: Int
doesKnowCommand(dogCommand: DogCommand!): Boolean!
isHouseTrained(atOtherHomes: Boolean): Boolean!
owner: Human
}
interface Sentient {
name: String!
}
interface Pet {
name: String!
}
type Alien implements Sentient {
name: String!
homePlanet: String
}
type Human implements Sentient {
name: String!
pets: [Pet!]
}
enum CatCommand {
JUMP
}
type Cat implements Pet {
name: String!
nickname: String
doesKnowCommand(catCommand: CatCommand!): Boolean!
meowVolume: Int
}
union CatOrDog = Cat | Dog
union DogOrHuman = Dog | Human
union HumanOrAlien = Human | Alien
input FindDogInput {
name: String
owner: String
}
input CatInput {
name: String!
nickname: String
meowVolume: Int
}
input DogInput {
name: String!
nickname: String
barkVolume: Int
}
input PetInput @oneOf {
cat: CatInput
dog: DogInput
}
```
## Documents
### Executable Definitions
**Formal Specification**
- For each definition {definition} in the document:
- {definition} must be {ExecutableDefinition} (it must not be
{TypeSystemDefinitionOrExtension}).
**Explanatory Text**
GraphQL execution will only consider the executable definitions Operation and
Fragment. Type system definitions and extensions are not executable, and are not
considered during execution.
To avoid ambiguity, a document containing {TypeSystemDefinitionOrExtension} is
invalid for execution.
GraphQL documents not intended to be directly executed may include
{TypeSystemDefinitionOrExtension}.
For example, the following document is invalid for execution since the original
executing schema may not know about the provided type extension:
```graphql counter-example
query getDogName {
dog {
name
color
}
}
extend type Dog {
color: String
}
```
## Operations
### All Operation Definitions
#### Operation Type Existence
**Formal Specification**
- For each operation definition {operation} in the document:
- Let {rootOperationType} be the _root operation type_ in {schema}
corresponding to the kind of {operation}.
- {rootOperationType} must exist.
**Explanatory Text**
A schema defines the _root operation type_ for each kind of operation that it
supports. Every schema must support `query` operations, however support for
`mutation` and `subscription` operations is optional.
If the schema does not include the necessary _root operation type_ for the kind
of an operation defined in the document, that operation is invalid since it
cannot be executed.
For example given the following schema:
```graphql example
type Query {
hello: String
}
```
The following operation is valid:
```graphql example
query helloQuery {
hello
}
```
While the following operation is invalid:
```graphql counter-example
mutation goodbyeMutation {
goodbye
}
```
### Named Operation Definitions
#### Operation Name Uniqueness
**Formal Specification**
- For each operation definition {operation} in the document:
- Let {operationName} be the name of {operation}.
- If {operationName} exists:
- Let {operations} be all operation definitions in the document named
{operationName}.
- {operations} must be a set of one.
**Explanatory Text**
Each named operation definition must be unique within a document when referred
to by its name.
For example the following document is valid:
```graphql example
query getDogName {
dog {
name
}
}
query getOwnerName {
dog {
owner {
name
}
}
}
```
While this document is invalid:
```graphql counter-example
query getName {
dog {
name
}
}
query getName {
dog {
owner {
name
}
}
}
```
It is invalid even if the type of each operation is different:
```graphql counter-example
query dogOperation {
dog {
name
}
}
mutation dogOperation {
mutateDog {
id
}
}
```
### Anonymous Operation Definitions
#### Lone Anonymous Operation
**Formal Specification**
- Let {operations} be all operation definitions in the document.
- Let {anonymous} be all anonymous operation definitions in the document.
- If {operations} is a set of more than 1:
- {anonymous} must be empty.
**Explanatory Text**
GraphQL allows a shorthand form for defining query operations when only that one
operation exists in the document.
For example the following document is valid:
```graphql example
{
dog {
name
}
}
```
While this document is invalid:
```graphql counter-example
{
dog {
name
}
}
query getName {
dog {
owner {
name
}
}
}
```
### Subscription Operation Definitions
#### Single Root Field
**Formal Specification**
- Let {subscriptionType} be the root Subscription type in {schema}.
- For each subscription operation definition {subscription} in the document:
- Let {selectionSet} be the top level selection set on {subscription}.
- Let {collectedFieldsMap} be the result of
{CollectSubscriptionFields(subscriptionType, selectionSet)}.
- {collectedFieldsMap} must have exactly one entry, which must not be an
introspection field.
CollectSubscriptionFields(objectType, selectionSet, visitedFragments):
- If {visitedFragments} is not provided, initialize it to the empty set.
- Initialize {collectedFieldsMap} to an empty ordered map of ordered sets.
- For each {selection} in {selectionSet}:
- {selection} must not provide the `@skip` directive.
- {selection} must not provide the `@include` directive.
- If {selection} is a {Field}:
- Let {responseName} be the _response name_ of {selection} (the alias if
defined, otherwise the field name).
- Let {fieldsForResponseKey} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Add {selection} to the {fieldsForResponseKey}.
- If {selection} is a {FragmentSpread}:
- Let {fragmentSpreadName} be the name of {selection}.
- If {fragmentSpreadName} is in {visitedFragments}, continue with the next
{selection} in {selectionSet}.
- Add {fragmentSpreadName} to {visitedFragments}.
- Let {fragment} be the Fragment in the current Document whose name is
{fragmentSpreadName}.
- If no such {fragment} exists, continue with the next {selection} in
{selectionSet}.
- Let {fragmentType} be the type condition on {fragment}.
- If {DoesFragmentTypeApply(objectType, fragmentType)} is {false}, continue
with the next {selection} in {selectionSet}.
- Let {fragmentSelectionSet} be the top-level selection set of {fragment}.
- Let {fragmentCollectedFieldsMap} be the result of calling
{CollectSubscriptionFields(objectType, fragmentSelectionSet,
visitedFragments)}.
- For each {responseName} and {fragmentFields} in
{fragmentCollectedFieldsMap}:
- Let {fieldsForResponseKey} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Add each item from {fragmentFields} to {fieldsForResponseKey}.
- If {selection} is an {InlineFragment}:
- Let {fragmentType} be the type condition on {selection}.
- If {fragmentType} is not {null} and {DoesFragmentTypeApply(objectType,
fragmentType)} is {false}, continue with the next {selection} in
{selectionSet}.
- Let {fragmentSelectionSet} be the top-level selection set of {selection}.
- Let {fragmentCollectedFieldsMap} be the result of calling
{CollectSubscriptionFields(objectType, fragmentSelectionSet,
visitedFragments)}.
- For each {responseName} and {fragmentFields} in
{fragmentCollectedFieldsMap}:
- Let {fieldsForResponseKey} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Add each item from {fragmentFields} to {fieldsForResponseKey}.
- Return {collectedFieldsMap}.
Note: This algorithm is very similar to {CollectFields()}, it differs in that it
does not have access to runtime variables and thus the `@skip` and `@include`
directives cannot be used.
**Explanatory Text**
Subscription operations must have exactly one root field.
To enable us to determine this without access to runtime variables, we must
forbid the `@skip` and `@include` directives in the root selection set.
Valid examples:
```graphql example
subscription sub {
newMessage {
body
sender
}
}
```
```graphql example
subscription sub {
...newMessageFields
}
fragment newMessageFields on Subscription {
newMessage {
body
sender
}
}
```
Invalid:
```graphql counter-example
subscription sub {
newMessage {
body
sender
}
disallowedSecondRootField
}
```
```graphql counter-example
subscription sub {
...multipleSubscriptions
}
fragment multipleSubscriptions on Subscription {
newMessage {
body
sender
}
disallowedSecondRootField
}
```
We do not allow the `@skip` and `@include` directives at the root of the
subscription operation. The following example is also invalid:
```graphql counter-example
subscription requiredRuntimeValidation($bool: Boolean!) {
newMessage @include(if: $bool) {
body
sender
}
disallowedSecondRootField @skip(if: $bool)
}
```
The root field of a subscription operation must not be an introspection field.
The following example is also invalid:
```graphql counter-example
subscription sub {
__typename
}
```
Note: While each subscription must have exactly one root field, a document may
contain any number of operations, each of which may contain different root
fields. When executed, a document containing multiple subscription operations
must provide the operation name as described in {GetOperation()}.
## Fields
### Field Selections
Field selections must exist on Object, Interface, and Union types.
**Formal Specification**
- For each {selection} in the document:
- Let {fieldName} be the target field of {selection}.
- {fieldName} must be defined on type in scope.
**Explanatory Text**
The target field of a field selection must be defined on the scoped type of the
selection set. There are no limitations on alias names.
For example the following fragment would not pass validation:
```graphql counter-example
fragment fieldNotDefined on Dog {
meowVolume
}
fragment aliasedLyingFieldTargetNotDefined on Dog {
barkVolume: kawVolume
}
```
For interfaces, direct field selection can only be done on fields. Fields of
concrete implementers are not relevant to the validity of the given
interface-typed selection set.
For example, the following is valid:
```graphql example
fragment interfaceFieldSelection on Pet {
name
}
```
and the following is invalid:
```graphql counter-example
fragment definedOnImplementersButNotInterface on Pet {
nickname
}
```
Because unions do not define fields, fields may not be directly selected from a
union-typed selection set, with the exception of the meta-field {\_\_typename}.
Fields from a union-typed selection set must only be queried indirectly via a
fragment.
For example the following is valid:
```graphql example
fragment inDirectFieldSelectionOnUnion on CatOrDog {
__typename
... on Pet {
name
}
... on Dog {
barkVolume
}
}
```
But the following is invalid:
```graphql counter-example
fragment directFieldSelectionOnUnion on CatOrDog {
name
barkVolume
}
```
### Field Selection Merging
**Formal Specification**
- Let {set} be any selection set defined in the GraphQL document.
- {FieldsInSetCanMerge(set)} must be true.
FieldsInSetCanMerge(set):
- Let {fieldsForName} be the set of selections with a given _response name_ in
{set} including visiting fragments and inline fragments.
- Given each pair of distinct members {fieldA} and {fieldB} in {fieldsForName}:
- {SameResponseShape(fieldA, fieldB)} must be true.
- If the parent types of {fieldA} and {fieldB} are equal or if either is not
an Object Type:
- {fieldA} and {fieldB} must have identical field names.
- {fieldA} and {fieldB} must have identical sets of arguments.
- Let {mergedSet} be the result of adding the selection set of {fieldA} and
the selection set of {fieldB}.
- {FieldsInSetCanMerge(mergedSet)} must be true.
SameResponseShape(fieldA, fieldB):
- Let {typeA} be the return type of {fieldA}.
- Let {typeB} be the return type of {fieldB}.
- If {typeA} or {typeB} is Non-Null:
- If {typeA} or {typeB} is nullable, return {false}.
- Let {typeA} be the nullable type of {typeA}.
- Let {typeB} be the nullable type of {typeB}.
- If {typeA} or {typeB} is List:
- If {typeA} or {typeB} is not List, return {false}.
- Let {typeA} be the item type of {typeA}.
- Let {typeB} be the item type of {typeB}.
- Repeat from step 3.
- If {typeA} or {typeB} is Scalar or Enum:
- If {typeA} and {typeB} are the same type return {true}, otherwise return
{false}.
- Assert: {typeA} is an object, union or interface type.
- Assert: {typeB} is an object, union or interface type.
- Let {mergedSet} be the result of adding the selection set of {fieldA} and the
selection set of {fieldB}.
- Let {fieldsForName} be the set of selections with a given _response name_ in
{mergedSet} including visiting fragments and inline fragments.
- Given each pair of distinct members {subfieldA} and {subfieldB} in
{fieldsForName}:
- If {SameResponseShape(subfieldA, subfieldB)} is {false}, return {false}.
- Return {true}.
Note: In prior versions of the spec the term "composite" was used to signal a
type that is either an Object, Interface or Union type.
**Explanatory Text**
If multiple field selections with the same _response name_ are encountered
during execution, the field and arguments to execute and the resulting value
should be unambiguous. Therefore any two field selections which might both be
encountered for the same object are only valid if they are equivalent.
During execution, the simultaneous execution of fields with the same response
name is accomplished by performing {CollectSubfields()} before their execution.
For simple hand-written GraphQL, this rule is obviously a clear developer error,
however nested fragments can make this difficult to detect manually.
The following selections correctly merge:
```graphql example
fragment mergeIdenticalFields on Dog {
name
name
}
fragment mergeIdenticalAliasesAndFields on Dog {
otherName: name
otherName: name
}
```
The following is not able to merge:
```graphql counter-example
fragment conflictingBecauseAlias on Dog {
name: nickname
name
}
```
Identical fields are also merged if they have identical arguments. Both values
and variables can be correctly merged.
For example the following correctly merge:
```graphql example
fragment mergeIdenticalFieldsWithIdenticalArgs on Dog {
doesKnowCommand(dogCommand: SIT)
doesKnowCommand(dogCommand: SIT)
}
fragment mergeIdenticalFieldsWithIdenticalValues on Dog {
doesKnowCommand(dogCommand: $dogCommand)
doesKnowCommand(dogCommand: $dogCommand)
}
```
The following do not correctly merge:
```graphql counter-example
fragment conflictingArgsOnValues on Dog {
doesKnowCommand(dogCommand: SIT)
doesKnowCommand(dogCommand: HEEL)
}
fragment conflictingArgsValueAndVar on Dog {
doesKnowCommand(dogCommand: SIT)
doesKnowCommand(dogCommand: $dogCommand)
}
fragment conflictingArgsWithVars on Dog {
doesKnowCommand(dogCommand: $varOne)
doesKnowCommand(dogCommand: $varTwo)
}
fragment differingArgs on Dog {
doesKnowCommand(dogCommand: SIT)
doesKnowCommand
}
```
The following fields would not merge together, however both cannot be
encountered against the same object, so they are safe:
```graphql example
fragment safeDifferingFields on Pet {
... on Dog {
volume: barkVolume
}
... on Cat {
volume: meowVolume
}
}
fragment safeDifferingArgs on Pet {
... on Dog {
doesKnowCommand(dogCommand: SIT)
}
... on Cat {
doesKnowCommand(catCommand: JUMP)
}
}
```
However, the field responses must be shapes which can be merged. For example,
leaf types must not differ. In this example, `someValue` might be a `String` or
an `Int`:
```graphql counter-example
fragment conflictingDifferingResponses on Pet {
... on Dog {
someValue: nickname
}
... on Cat {
someValue: meowVolume
}
}
```
### Leaf Field Selections
**Formal Specification**
- For each {selection} in the document:
- Let {selectionType} be the unwrapped result type of {selection}.
- If {selectionType} is a scalar or enum:
- The subselection set of that selection must be empty.
- If {selectionType} is an interface, union, or object:
- The subselection set of that selection must not be empty.
**Explanatory Text**
A field subselection is not allowed on leaf fields. A leaf field is any field
with a scalar or enum unwrapped type.
The following is valid.
```graphql example
fragment scalarSelection on Dog {
barkVolume
}
```
The following is invalid.
```graphql counter-example
fragment scalarSelectionsNotAllowedOnInt on Dog {
barkVolume {
sinceWhen
}
}
```
Conversely, non-leaf fields must have a field subselection. A non-leaf field is
any field with an object, interface, or union unwrapped type.
Let's assume the following additions to the query root operation type of the
schema:
```graphql example
extend type Query {
human: Human
pet: Pet
catOrDog: CatOrDog
}
```
The following examples are invalid because they include non-leaf fields without
a field subselection.
```graphql counter-example
query directQueryOnObjectWithoutSubFields {
human
}
query directQueryOnInterfaceWithoutSubFields {
pet
}
query directQueryOnUnionWithoutSubFields {
catOrDog
}
```
However the following example is valid since it includes a field subselection.
```graphql example
query directQueryOnObjectWithSubFields {
human {
name
}
}
```
## Arguments
Arguments are provided to both fields and directives. The following validation
rules apply in both cases.
### Argument Names
**Formal Specification**
- For each {argument} in the document:
- Let {argumentName} be the Name of {argument}.
- Let {argumentDefinition} be the argument definition provided by the parent
field or definition named {argumentName}.
- {argumentDefinition} must exist.
**Explanatory Text**
Every argument provided to a field or directive must be defined in the set of
possible arguments of that field or directive.
For example the following are valid:
```graphql example
fragment argOnRequiredArg on Dog {
doesKnowCommand(dogCommand: SIT)
}
fragment argOnOptional on Dog {
isHouseTrained(atOtherHomes: true) @include(if: true)
}
```
the following is invalid since `command` is not defined on `DogCommand`.
```graphql counter-example
fragment invalidArgName on Dog {
doesKnowCommand(command: CLEAN_UP_HOUSE)
}
```
and this is also invalid as `unless` is not defined on `@include`.
```graphql counter-example
fragment invalidArgName on Dog {
isHouseTrained(atOtherHomes: true) @include(unless: false)
}
```
In order to explore more complicated argument examples, let's add the following
to our type system:
```graphql example
type Arguments {
multipleRequirements(x: Int!, y: Int!): Int!
booleanArgField(booleanArg: Boolean): Boolean
floatArgField(floatArg: Float): Float
intArgField(intArg: Int): Int
nonNullBooleanArgField(nonNullBooleanArg: Boolean!): Boolean!
booleanListArgField(booleanListArg: [Boolean]!): [Boolean]
optionalNonNullBooleanArgField(optionalBooleanArg: Boolean! = false): Boolean!
}
extend type Query {
arguments: Arguments
}
```
Order does not matter in arguments. Therefore both the following examples are
valid.
```graphql example
fragment multipleArgs on Arguments {
multipleRequirements(x: 1, y: 2)
}
fragment multipleArgsReverseOrder on Arguments {
multipleRequirements(y: 2, x: 1)
}
```
### Argument Uniqueness
Fields and directives treat arguments as a mapping of argument name to value.
More than one argument with the same name in an argument set is ambiguous and
invalid.
**Formal Specification**
- For each {argument} in the Document:
- Let {argumentName} be the Name of {argument}.
- Let {arguments} be all Arguments named {argumentName} in the Argument Set
which contains {argument}.
- {arguments} must be the set containing only {argument}.
### Required Arguments
- For each Field or Directive in the document:
- Let {arguments} be the arguments provided by the Field or Directive.
- Let {argumentDefinitions} be the set of argument definitions of that Field
or Directive.
- For each {argumentDefinition} in {argumentDefinitions}:
- Let {type} be the expected type of {argumentDefinition}.
- Let {defaultValue} be the default value of {argumentDefinition}.
- If {type} is Non-Null and {defaultValue} does not exist:
- Let {argumentName} be the name of {argumentDefinition}.
- Let {argument} be the argument in {arguments} named {argumentName}.
- {argument} must exist.
- Let {value} be the value of {argument}.
- {value} must not be the {null} literal.
**Explanatory Text**
Arguments can be required. An argument is required if the argument type is
non-null and does not have a default value. Otherwise, the argument is optional.
For example the following are valid:
```graphql example
fragment goodBooleanArg on Arguments {
booleanArgField(booleanArg: true)
}
fragment goodNonNullArg on Arguments {
nonNullBooleanArgField(nonNullBooleanArg: true)
}
```
The argument can be omitted from a field with a nullable argument.
Therefore the following fragment is valid:
```graphql example
fragment goodBooleanArgDefault on Arguments {
booleanArgField
}
```
but this is not valid on a required argument.
```graphql counter-example
fragment missingRequiredArg on Arguments {
nonNullBooleanArgField
}
```
Providing the explicit value {null} is also not valid since required arguments
always have a non-null type.
```graphql counter-example
fragment missingRequiredArg on Arguments {
nonNullBooleanArgField(nonNullBooleanArg: null)
}
```
## Fragments
### Fragment Declarations
#### Fragment Name Uniqueness
**Formal Specification**
- For each fragment definition {fragment} in the document:
- Let {fragmentName} be the name of {fragment}.
- Let {fragments} be all fragment definitions in the document named
{fragmentName}.
- {fragments} must be a set of one.
**Explanatory Text**
Fragment definitions are referenced in fragment spreads by name. To avoid
ambiguity, each fragment's name must be unique within a document.
Inline fragments are not considered fragment definitions, and are unaffected by
this validation rule.
For example the following document is valid:
```graphql example
{
dog {
...fragmentOne
...fragmentTwo
}
}
fragment fragmentOne on Dog {
name
}
fragment fragmentTwo on Dog {
owner {
name
}
}
```
While this document is invalid:
```graphql counter-example
{
dog {
...fragmentOne
}
}
fragment fragmentOne on Dog {
name
}
fragment fragmentOne on Dog {
owner {
name
}
}
```
#### Fragment Spread Type Existence
**Formal Specification**
- For each named spread {namedSpread} in the document:
- Let {fragment} be the target of {namedSpread}.
- The target type of {fragment} must be defined in the schema.
**Explanatory Text**
Fragments must be specified on types that exist in the schema. This applies for
both named and inline fragments. If they are not defined in the schema, the
fragment is invalid.
For example the following fragments are valid:
```graphql example
fragment correctType on Dog {
name
}
fragment inlineFragment on Dog {
... on Dog {
name
}
}
fragment inlineFragment2 on Dog {
... @include(if: true) {
name
}
}
```
and the following do not validate:
```graphql counter-example
fragment notOnExistingType on NotInSchema {
name
}
fragment inlineNotExistingType on Dog {
... on NotInSchema {
name
}
}
```
#### Fragments on Object, Interface or Union Types
**Formal Specification**
- For each {fragment} defined in the document:
- The target type of fragment must have kind {UNION}, {INTERFACE}, or
{OBJECT}.
**Explanatory Text**
Fragments can only be declared on unions, interfaces, and objects. They are
invalid on scalars. They can only be applied on non-leaf fields. This rule
applies to both inline and named fragments.
The following fragment declarations are valid:
```graphql example
fragment fragOnObject on Dog {
name
}
fragment fragOnInterface on Pet {
name
}
fragment fragOnUnion on CatOrDog {
... on Dog {
name
}
}
```
and the following are invalid:
```graphql counter-example
fragment fragOnScalar on Int {
something
}
fragment inlineFragOnScalar on Dog {
... on Boolean {
somethingElse
}
}
```
#### Fragments Must Be Used
**Formal Specification**
- For each {fragment} defined in the document:
- {fragment} must be the target of at least one spread in the document.
**Explanatory Text**
Defined fragments must be used within a document.
For example the following is an invalid document:
```raw graphql counter-example
fragment nameFragment on Dog { # unused
name
}
{
dog {
name
}
}
```
### Fragment Spreads
Field selection is also determined by spreading fragments into one another. The
selection set of the target fragment is combined into the selection set at the
level at which the target fragment is referenced.
#### Fragment Spread Target Defined
**Formal Specification**
- For every {namedSpread} in the document:
- Let {fragment} be the target of {namedSpread}.
- {fragment} must be defined in the document.
**Explanatory Text**
Named fragment spreads must refer to fragments defined within the document. It
is a validation error if the target of a spread is not defined.
```graphql counter-example
{
dog {
...undefinedFragment
}
}
```
#### Fragment Spreads Must Not Form Cycles
**Formal Specification**
- For each {fragmentDefinition} in the document:
- Let {visited} be the empty set.
- {DetectFragmentCycles(fragmentDefinition, visited)}.
DetectFragmentCycles(fragmentDefinition, visited):
- Let {spreads} be all fragment spread descendants of {fragmentDefinition}.
- For each {spread} in {spreads}:
- {visited} must not contain {spread}.
- Let {nextVisited} be the set including {spread} and members of {visited}.
- Let {nextFragmentDefinition} be the target of {spread}.
- {DetectFragmentCycles(nextFragmentDefinition, nextVisited)}.
**Explanatory Text**
The graph of fragment spreads must not form any cycles including spreading
itself. Otherwise an operation could infinitely spread or infinitely execute on
cycles in the underlying data.
This invalidates fragments that would result in an infinite spread:
```graphql counter-example
{
dog {
...nameFragment
}
}
fragment nameFragment on Dog {
name
...barkVolumeFragment
}
fragment barkVolumeFragment on Dog {
barkVolume
...nameFragment
}
```
If the above fragments were inlined, this would result in the infinitely large:
```graphql example
{
dog {
name
barkVolume
name
barkVolume
name
barkVolume
name
# forever...
}
}
```
This also invalidates fragments that would result in an infinite recursion when
executed against cyclic data:
```graphql counter-example
{
dog {
...dogFragment
}
}
fragment dogFragment on Dog {
name
owner {
...ownerFragment
}
}
fragment ownerFragment on Human {
name
pets {
...dogFragment
}
}
```
#### Fragment Spread Is Possible
**Formal Specification**
- For each {spread} (named or inline) defined in the document:
- Let {fragment} be the target of {spread}.
- Let {fragmentType} be the type condition of {fragment}.
- Let {parentType} be the type of the selection set containing {spread}.
- Let {applicableTypes} be the intersection of
{GetPossibleTypes(fragmentType)} and {GetPossibleTypes(parentType)}.
- {applicableTypes} must not be empty.
GetPossibleTypes(type):
- If {type} is an object type, return a set containing {type}.
- If {type} is an interface type, return the set of types implementing {type}.
- If {type} is a union type, return the set of possible types of {type}.
**Explanatory Text**
Fragments are declared on a type and will only apply when the runtime object
type matches the type condition. They also are spread within the context of a
parent type. A fragment spread is only valid if its type condition could ever
apply within the parent type.
##### Object Spreads in Object Scope
In the scope of an object type, the only valid object type fragment spread is
one that applies to the same type that is in scope.
For example
```graphql example
fragment dogFragment on Dog {
... on Dog {
barkVolume
}
}
```
and the following is invalid
```graphql counter-example
fragment catInDogFragmentInvalid on Dog {
... on Cat {
meowVolume
}
}
```
##### Abstract Spreads in Object Scope
In scope of an object type, unions or interface spreads can be used if the
object type implements the interface or is a member of the union.
For example
```graphql example
fragment petNameFragment on Pet {
name
}
fragment interfaceWithinObjectFragment on Dog {
...petNameFragment
}
```
is valid because {Dog} implements Pet.
Likewise
```graphql example
fragment catOrDogNameFragment on CatOrDog {
... on Cat {
meowVolume
}
}
fragment unionWithObjectFragment on Dog {
...catOrDogNameFragment
}
```
is valid because {Dog} is a member of the {CatOrDog} union. It is worth noting
that if one inspected the contents of the {CatOrDogNameFragment} you could note
that no valid results would ever be returned. However we do not specify this as
invalid because we only consider the fragment declaration, not its body.
##### Object Spreads in Abstract Scope
Union or interface spreads can be used within the context of an object type
fragment, but only if the object type is one of the possible types of that
interface or union.
For example, the following fragments are valid:
```graphql example
fragment petFragment on Pet {
name
... on Dog {
barkVolume
}
}
fragment catOrDogFragment on CatOrDog {
... on Cat {
meowVolume
}
}
```
{petFragment} is valid because {Dog} implements the interface {Pet}.
{catOrDogFragment} is valid because {Cat} is a member of the {CatOrDog} union.
By contrast the following fragments are invalid:
```graphql counter-example
fragment sentientFragment on Sentient {
... on Dog {
barkVolume
}
}
fragment humanOrAlienFragment on HumanOrAlien {
... on Cat {
meowVolume
}
}
```
{Dog} does not implement the interface {Sentient} and therefore
{sentientFragment} can never return meaningful results. Therefore the fragment
is invalid. Likewise {Cat} is not a member of the union {HumanOrAlien}, and it
can also never return meaningful results, making it invalid.
##### Abstract Spreads in Abstract Scope
Union or interfaces fragments can be used within each other. As long as there
exists at least _one_ object type that exists in the intersection of the
possible types of the scope and the spread, the spread is considered valid.
So for example
```graphql example
fragment unionWithInterface on Pet {
...dogOrHumanFragment
}
fragment dogOrHumanFragment on DogOrHuman {
... on Dog {
barkVolume
}
}
```
is considered valid because {Dog} implements interface {Pet} and is a member of
{DogOrHuman}.
However
```graphql counter-example
fragment nonIntersectingInterfaces on Pet {
...sentientFragment
}
fragment sentientFragment on Sentient {
name
}
```
is not valid because there exists no type that implements both {Pet} and
{Sentient}.
**Interface Spreads in Implemented Interface Scope**
Additionally, an interface type fragment can always be spread into an interface
scope which it implements.
In the example below, the `...resourceFragment` fragments spreads is valid,
since `Resource` implements `Node`.
```raw graphql example
interface Node {
id: ID!
}
interface Resource implements Node {
id: ID!
url: String
}
fragment interfaceWithInterface on Node {
...resourceFragment
}
fragment resourceFragment on Resource {
url
}
```
## Values
### Values of Correct Type
**Formal Specification**
- For each literal Input Value {value} in the document:
- Let {type} be the type expected in the position {value} is found.
- {value} must be coercible to {type} (with the assumption that any
{variableUsage} nested within {value} will represent a runtime value valid
for usage in its position).
**Explanatory Text**
Literal values must be compatible with the type expected in the position they
are found as per the coercion rules defined in the Type System chapter.
Note: A {ListValue} or {ObjectValue} may contain nested Input Values, some of
which may be a variable usage. The
[All Variable Usages Are Allowed](#sec-All-Variable-Usages-Are-Allowed)
validation rule ensures that each {variableUsage} is of a type allowed in its
position. The [Coercing Variable Values](#sec-Coercing-Variable-Values)
algorithm ensures runtime values for variables coerce correctly. Therefore, for
the purposes of the "coercible" assertion in this validation rule, we can assume
the runtime value of each {variableUsage} is valid for usage in its position.
The type expected in a position includes the type defined by the argument a
value is provided for, the type defined by an input object field a value is
provided for, and the type of a variable definition a default value is provided
for.
The following examples are valid use of value literals:
```graphql example
fragment goodBooleanArg on Arguments {
booleanArgField(booleanArg: true)
}
fragment coercedIntIntoFloatArg on Arguments {
# Note: The input coercion rules for Float allow Int literals.
floatArgField(floatArg: 123)
}
query goodComplexDefaultValue($search: FindDogInput = { name: "Fido" }) {
findDog(searchBy: $search) {
name
}
}
mutation addPet($pet: PetInput! = { cat: { name: "Brontie" } }) {
addPet(pet: $pet) {
name
}
}
```
Non-coercible values (such as a String into an Int) are invalid. The following
examples are invalid:
```graphql counter-example
fragment stringIntoInt on Arguments {
intArgField(intArg: "123")
}
query badComplexValue {
findDog(searchBy: { name: 123 }) {
name
}
}
mutation oneOfWithNoFields {
addPet(pet: {}) {
name
}
}
mutation oneOfWithTwoFields($dog: DogInput) {
addPet(pet: { cat: { name: "Brontie" }, dog: $dog }) {
name
}
}
mutation listOfOneOfWithNullableVariable($dog: DogInput) {
addPets(pets: [{ dog: $dog }]) {
name
}
}
```
### Input Object Field Names
**Formal Specification**
- For each Input Object Field {inputField} in the document:
- Let {inputFieldName} be the Name of {inputField}.
- Let {inputFieldDefinition} be the input field definition provided by the
parent input object type named {inputFieldName}.
- {inputFieldDefinition} must exist.
**Explanatory Text**
Every input field provided in an input object value must be defined in the set
of possible fields of that input object's expected type.
For example the following example input object is valid:
```graphql example
{
findDog(searchBy: { name: "Fido" }) {
name
}
}
```
While the following example input-object uses a field "favoriteCookieFlavor"
which is not defined on the expected type:
```graphql counter-example
{
findDog(searchBy: { favoriteCookieFlavor: "Bacon" }) {
name
}
}
```
### Input Object Field Uniqueness
**Formal Specification**
- For each input object value {inputObject} in the document:
- For every {inputField} in {inputObject}:
- Let {name} be the Name of {inputField}.
- Let {fields} be all Input Object Fields named {name} in {inputObject}.
- {fields} must be the set containing only {inputField}.
**Explanatory Text**
Input objects must not contain more than one field of the same name, otherwise
an ambiguity would exist which includes an ignored portion of syntax.
For example the following document will not pass validation.
```graphql counter-example
{
field(arg: { field: true, field: false })
}
```
### Input Object Required Fields
**Formal Specification**
- For each Input Object in the document:
- Let {fields} be the fields provided by that Input Object.
- Let {fieldDefinitions} be the set of input field definitions of that Input
Object.
- For each {fieldDefinition} in {fieldDefinitions}:
- Let {type} be the expected type of {fieldDefinition}.
- Let {defaultValue} be the default value of {fieldDefinition}.
- If {type} is Non-Null and {defaultValue} does not exist:
- Let {fieldName} be the name of {fieldDefinition}.
- Let {field} be the input field in {fields} named {fieldName}.
- {field} must exist.
- Let {value} be the value of {field}.
- {value} must not be the {null} literal.
**Explanatory Text**
Input object fields may be required. Much like a field may have required
arguments, an input object may have required fields. An input field is required
if it has a non-null type and does not have a default value. Otherwise, the
input object field is optional.
## Directives
### Directives Are Defined
**Formal Specification**
- For every {directive} in a document:
- Let {directiveName} be the name of {directive}.
- Let {directiveDefinition} be the directive named {directiveName}.
- {directiveDefinition} must exist.
**Explanatory Text**
GraphQL services define what directives they support. For each usage of a
directive, the directive must be available on that service.
### Directives Are in Valid Locations
**Formal Specification**
- For every {directive} in a document:
- Let {directiveName} be the name of {directive}.
- Let {directiveDefinition} be the directive named {directiveName}.
- Let {locations} be the valid locations for {directiveDefinition}.
- Let {adjacent} be the AST node the directive affects.
- {adjacent} must be represented by an item within {locations}.
**Explanatory Text**
GraphQL services define what directives they support and where they support
them. For each usage of a directive, the directive must be used in a location
that the service has declared support for.
For example the following document will not pass validation because `@skip` does
not provide `QUERY` as a valid location.
```graphql counter-example
query @skip(if: $foo) {
field
}
```
### Directives Are Unique per Location
**Formal Specification**
- For every {location} in the document for which Directives can apply:
- Let {directives} be the set of Directives which apply to {location} and are
not repeatable.
- For each {directive} in {directives}:
- Let {directiveName} be the name of {directive}.
- Let {namedDirectives} be the set of all Directives named {directiveName}
in {directives}.
- {namedDirectives} must be a set of one.
**Explanatory Text**
GraphQL allows directives that are defined as `repeatable` to be used more than
once on the definition they apply to, possibly with different arguments. In
contrast, if a directive is not `repeatable`, then only one occurrence of it is
allowed per location.
For example, the following document will not pass validation because
non-repeatable `@skip` has been used twice for the same field:
```raw graphql counter-example
query ($foo: Boolean = true, $bar: Boolean = false) {
field @skip(if: $foo) @skip(if: $bar)
}
```
However the following example is valid because `@skip` has been used only once
per location, despite being used twice in the operation and on the same named
field:
```raw graphql example
query ($foo: Boolean = true, $bar: Boolean = false) {
field @skip(if: $foo) {
subfieldA
}
field @skip(if: $bar) {
subfieldB
}
}
```
## Variables
### Variable Uniqueness
**Formal Specification**
- For every {operation} in the document:
- For every {variable} defined on {operation}:
- Let {variableName} be the name of {variable}.
- Let {variables} be the set of all variables named {variableName} on
{operation}.
- {variables} must be a set of one.
**Explanatory Text**
If any operation defines more than one variable with the same name, it is
ambiguous and invalid. It is invalid even if the type of the duplicate variable
is the same.
```graphql counter-example
query houseTrainedQuery($atOtherHomes: Boolean, $atOtherHomes: Boolean) {
dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
It is valid for multiple operations to define a variable with the same name. If
two operations reference the same fragment, it might actually be necessary:
```graphql example
query A($atOtherHomes: Boolean) {
...HouseTrainedFragment
}
query B($atOtherHomes: Boolean) {
...HouseTrainedFragment
}
fragment HouseTrainedFragment on Query {
dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
### Variables Are Input Types
**Formal Specification**
- For every {operation} in a {document}:
- For every {variable} on each {operation}:
- Let {variableType} be the type of {variable}.
- {IsInputType(variableType)} must be {true}.
**Explanatory Text**
Variables can only be input types. Objects, unions, and interfaces cannot be
used as inputs.
For these examples, consider the following type system additions:
```graphql example
extend type Query {
booleanList(booleanListArg: [Boolean!]): Boolean
}
```
The following operations are valid:
```graphql example
query takesBoolean($atOtherHomes: Boolean) {
dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
query takesComplexInput($search: FindDogInput) {
findDog(searchBy: $search) {
name
}
}
query TakesListOfBooleanBang($booleans: [Boolean!]) {
booleanList(booleanListArg: $booleans)
}
```
The following operations are invalid:
```graphql counter-example
query takesCat($cat: Cat) {
# ...
}
query takesDogBang($dog: Dog!) {
# ...
}
query takesListOfPet($pets: [Pet]) {
# ...
}
query takesCatOrDog($catOrDog: CatOrDog) {
# ...
}
```
### All Variable Uses Defined
**Formal Specification**
- For each {operation} in a document:
- For each {variableUsage} in scope, variable must be in {operation}'s
variable list.
- Let {fragments} be every fragment referenced by that {operation}
transitively.
- For each {fragment} in {fragments}:
- For each {variableUsage} in scope of {fragment}, variable must be in
{operation}'s variable list.
**Explanatory Text**
Variables are scoped on a per-operation basis. That means that any variable used
within the context of an operation must be defined at the top level of that
operation
For example:
```graphql example
query variableIsDefined($atOtherHomes: Boolean) {
dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
is valid. ${atOtherHomes} is defined by the operation.
By contrast the following document is invalid:
```graphql counter-example
query variableIsNotDefined {
dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
}
```
${atOtherHomes} is not defined by the operation.
Fragments complicate this rule. Any fragment transitively included by an
operation has access to the variables defined by that operation. Fragments can
appear within multiple operations and therefore variable usages must correspond
to variable definitions in all of those operations.
For example the following is valid:
```graphql example
query variableIsDefinedUsedInSingleFragment($atOtherHomes: Boolean) {
dog {
...isHouseTrainedFragment
}
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
since {isHouseTrainedFragment} is used within the context of the operation
{variableIsDefinedUsedInSingleFragment} and the variable is defined by that
operation.
On the other hand, if a fragment is included within an operation that does not
define a referenced variable, the document is invalid.
```graphql counter-example
query variableIsNotDefinedUsedInSingleFragment {
dog {
...isHouseTrainedFragment
}
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
This applies transitively as well, so the following also fails:
```graphql counter-example
query variableIsNotDefinedUsedInNestedFragment {
dog {
...outerHouseTrainedFragment
}
}
fragment outerHouseTrainedFragment on Dog {
...isHouseTrainedFragment
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
Variables must be defined in all operations in which a fragment is used.
```graphql example
query houseTrainedQueryOne($atOtherHomes: Boolean) {
dog {
...isHouseTrainedFragment
}
}
query houseTrainedQueryTwo($atOtherHomes: Boolean) {
dog {
...isHouseTrainedFragment
}
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
However the following does not validate:
```graphql counter-example
query houseTrainedQueryOne($atOtherHomes: Boolean) {
dog {
...isHouseTrainedFragment
}
}
query houseTrainedQueryTwoNotDefined {
dog {
...isHouseTrainedFragment
}
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
This is because {houseTrainedQueryTwoNotDefined} does not define a variable
${atOtherHomes} but that variable is used by {isHouseTrainedFragment} which is
included in that operation.
### All Variables Used
**Formal Specification**
- For every {operation} in the document:
- Let {variables} be the variables defined by that {operation}.
- Each {variable} in {variables} must be used at least once in either the
operation scope itself or any fragment transitively referenced by that
operation.
**Explanatory Text**
All variables defined by an operation must be used in that operation or a
fragment transitively included by that operation. Unused variables cause a
validation error.
For example the following is invalid:
```graphql counter-example
query variableUnused($atOtherHomes: Boolean) {
dog {
isHouseTrained
}
}
```
because ${atOtherHomes} is not referenced.
These rules apply to transitive fragment spreads as well:
```graphql example
query variableUsedInFragment($atOtherHomes: Boolean) {
dog {
...isHouseTrainedFragment
}
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
The above is valid since ${atOtherHomes} is used in {isHouseTrainedFragment}
which is included by {variableUsedInFragment}.
If that fragment did not have a reference to ${atOtherHomes} it would be not
valid:
```graphql counter-example
query variableNotUsedWithinFragment($atOtherHomes: Boolean) {
dog {
...isHouseTrainedWithoutVariableFragment
}
}
fragment isHouseTrainedWithoutVariableFragment on Dog {
isHouseTrained
}
```
All operations in a document must use all of their variables.
As a result, the following document does not validate.
```graphql counter-example
query queryWithUsedVar($atOtherHomes: Boolean) {
dog {
...isHouseTrainedFragment
}
}
query queryWithExtraVar($atOtherHomes: Boolean, $extra: Int) {
dog {
...isHouseTrainedFragment
}
}
fragment isHouseTrainedFragment on Dog {
isHouseTrained(atOtherHomes: $atOtherHomes)
}
```
This document is not valid because {queryWithExtraVar} defines an extraneous
variable.
### All Variable Usages Are Allowed
**Formal Specification**
- For each {operation} in {document}:
- Let {variableUsages} be all usages transitively included in the {operation}.
- For each {variableUsage} in {variableUsages}:
- Let {variableName} be the name of {variableUsage}.
- Let {variableDefinition} be the {VariableDefinition} named {variableName}
defined within {operation}.
- {IsVariableUsageAllowed(variableDefinition, variableUsage)} must be
{true}.
IsVariableUsageAllowed(variableDefinition, variableUsage):
- Let {variableType} be the expected type of {variableDefinition}.
- Let {locationType} be the expected type of the {Argument}, {ObjectField}, or
{ListValue} entry where {variableUsage} is located.
- If {IsNonNullPosition(locationType, variableUsage)} AND {variableType} is NOT
a non-null type:
- Let {hasNonNullVariableDefaultValue} be {true} if a default value exists for
{variableDefinition} and is not the value {null}.
- Let {hasLocationDefaultValue} be {true} if a default value exists for the
{Argument} or {ObjectField} where {variableUsage} is located.
- If {hasNonNullVariableDefaultValue} is NOT {true} AND
{hasLocationDefaultValue} is NOT {true}, return {false}.
- Let {nullableLocationType} be the unwrapped nullable type of {locationType}.
- Return {AreTypesCompatible(variableType, nullableLocationType)}.
- Return {AreTypesCompatible(variableType, locationType)}.
IsNonNullPosition(locationType, variableUsage):
- If {locationType} is a non-null type, return {true}.
- If the location of {variableUsage} is an {ObjectField}:
- Let {parentObjectValue} be the {ObjectValue} containing {ObjectField}.
- Let {parentLocationType} be the expected type of {ObjectValue}.
- If {parentLocationType} is a _OneOf Input Object_ type, return {true}.
- Return {false}.
AreTypesCompatible(variableType, locationType):
- If {locationType} is a non-null type:
- If {variableType} is NOT a non-null type, return {false}.
- Let {nullableLocationType} be the unwrapped nullable type of {locationType}.
- Let {nullableVariableType} be the unwrapped nullable type of {variableType}.
- Return {AreTypesCompatible(nullableVariableType, nullableLocationType)}.
- Otherwise, if {variableType} is a non-null type:
- Let {nullableVariableType} be the nullable type of {variableType}.
- Return {AreTypesCompatible(nullableVariableType, locationType)}.
- Otherwise, if {locationType} is a list type:
- If {variableType} is NOT a list type, return {false}.
- Let {itemLocationType} be the unwrapped item type of {locationType}.
- Let {itemVariableType} be the unwrapped item type of {variableType}.
- Return {AreTypesCompatible(itemVariableType, itemLocationType)}.
- Otherwise, if {variableType} is a list type, return {false}.
- Return {true} if {variableType} and {locationType} are identical, otherwise
{false}.
**Explanatory Text**
Variable usages must be compatible with the arguments they are passed to.
Validation failures occur when variables are used in the context of types that
are complete mismatches, or if a nullable type in a variable is passed to a
non-null argument type.
Types must match:
```graphql counter-example
query intCannotGoIntoBoolean($intArg: Int) {
arguments {
booleanArgField(booleanArg: $intArg)
}
}
```
${intArg} typed as {Int} cannot be used as an argument to {booleanArg}, typed as
{Boolean}.
List cardinality must also be the same. For example, lists cannot be passed into
singular values.
```graphql counter-example
query booleanListCannotGoIntoBoolean($booleanListArg: [Boolean]) {
arguments {
booleanArgField(booleanArg: $booleanListArg)
}
}
```
Nullability must also be respected. In general a nullable variable cannot be
passed to a non-null argument.
```graphql counter-example
query booleanArgQuery($booleanArg: Boolean) {
arguments {
nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
}
}
```
For list types, the same rules around nullability apply to both outer types and
inner types. A nullable list cannot be passed to a non-null list, and a list of
nullable values cannot be passed to a list of non-null values. The following is
valid:
```graphql example
query nonNullListToList($nonNullBooleanList: [Boolean]!) {
arguments {
booleanListArgField(booleanListArg: $nonNullBooleanList)
}
}
```
However, a nullable list cannot be passed to a non-null list:
```graphql counter-example
query listToNonNullList($booleanList: [Boolean]) {
arguments {
nonNullBooleanListField(nonNullBooleanListArg: $booleanList)
}
}
```
This would fail validation because a `[T]` cannot be passed to a `[T]!`.
Similarly a `[T]` cannot be passed to a `[T!]`.
Variables used for OneOf Input Object fields must be non-nullable.
```graphql example
mutation addCat($cat: CatInput!) {
addPet(pet: { cat: $cat }) {
name
}
}
mutation addCatWithDefault($cat: CatInput! = { name: "Brontie" }) {
addPet(pet: { cat: $cat }) {
name
}
}
```
```graphql counter-example
mutation addNullableCat($cat: CatInput) {
addPet(pet: { cat: $cat }) {
name
}
}
```
**Allowing Optional Variables When Default Values Exist**
A notable exception to typical variable type compatibility is allowing a
variable definition with a nullable type to be provided to a non-null location
as long as either that variable or that location provides a default value.
In the example below, an optional variable `$booleanArg` is allowed to be used
in the non-null argument `optionalBooleanArg` because the field argument is
optional since it provides a default value in the schema.
```graphql example
query booleanArgQueryWithDefault($booleanArg: Boolean) {
arguments {
optionalNonNullBooleanArgField(optionalBooleanArg: $booleanArg)
}
}
```
In the example below, an optional variable `$booleanArg` is allowed to be used
in the non-null argument (`nonNullBooleanArg`) because the variable provides a
default value in the operation. This behavior is explicitly supported for
compatibility with earlier editions of this specification. GraphQL authoring
tools may wish to report this as a warning with the suggestion to replace
`Boolean` with `Boolean!` to avoid ambiguity.
```graphql example
query booleanArgQueryWithDefault($booleanArg: Boolean = true) {
arguments {
nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
}
}
```
Note: The value {null} could still be provided to such a variable at runtime. A
non-null argument must raise an _execution error_ if provided a {null} value.
================================================
FILE: spec/Section 6 -- Execution.md
================================================
# Execution
A GraphQL service generates a response from a request via execution.
:: A _request_ for execution consists of a few pieces of information:
- {schema}: The schema to use, typically solely provided by the GraphQL service.
- {document}: A {Document} which must contain GraphQL {OperationDefinition} and
may contain {FragmentDefinition}.
- {operationName} (optional): The name of the Operation in the Document to
execute.
- {variableValues} (optional): Values for any Variables defined by the
Operation.
- {initialValue} (optional): An initial value corresponding to the root type
being executed. Conceptually, an initial value represents the "universe" of
data available via a GraphQL Service. It is common for a GraphQL Service to
always use the same initial value for every request.
- {extensions} (optional): A map reserved for implementation-specific additional
information.
Given this information, the result of {ExecuteRequest(schema, document,
operationName, variableValues, initialValue)} produces the response, to be
formatted according to the Response section below.
Implementations should not add additional properties to a _request_, which may
conflict with future editions of the GraphQL specification. Instead,
{extensions} provides a reserved location for implementation-specific additional
information. If present, {extensions} must be a map, but there are no additional
restrictions on its contents. To avoid conflicts, keys should use unique
prefixes.
Note: GraphQL requests do not require any specific serialization format or
transport mechanism. Message serialization and transport mechanisms should be
chosen by the implementing service.
Note: Descriptions and comments in executable documents (operation definitions,
fragment definitions, and variable definitions) MUST be ignored during execution
and have no effect on the observable execution, validation, or response of a
GraphQL document. Descriptions and comments on executable documents MAY be used
for non-observable purposes, such as logging and other developer tools.
## Executing Requests
To execute a request, the executor must have a parsed {Document} and a selected
operation name to run if the document defines multiple operations, otherwise the
document is expected to only contain a single operation. The result of the
request is determined by the result of executing this operation according to the
"Executing Operations” section below.
ExecuteRequest(schema, document, operationName, variableValues, initialValue):
- Let {operation} be the result of {GetOperation(document, operationName)}.
- Let {coercedVariableValues} be the result of {CoerceVariableValues(schema,
operation, variableValues)}.
- If {operation} is a query operation:
- Return {ExecuteQuery(operation, schema, coercedVariableValues,
initialValue)}.
- Otherwise if {operation} is a mutation operation:
- Return {ExecuteMutation(operation, schema, coercedVariableValues,
initialValue)}.
- Otherwise if {operation} is a subscription operation:
- Return {Subscribe(operation, schema, coercedVariableValues, initialValue)}.
GetOperation(document, operationName):
- If {operationName} is {null}:
- If {document} contains exactly one operation.
- Return the Operation contained in the {document}.
- Otherwise raise a _request error_ requiring {operationName}.
- Otherwise:
- Let {operation} be the Operation named {operationName} in {document}.
- If {operation} was not found, raise a _request error_.
- Return {operation}.
### Validating Requests
As explained in the Validation section, only requests which pass all validation
rules should be executed. If validation errors are known, they should be
reported in the list of "errors" in the response and the request must fail
without execution.
Typically validation is performed in the context of a request immediately before
execution, however a GraphQL service may execute a request without immediately
validating it if that exact same request is known to have been validated before.
A GraphQL service should only execute requests which _at some point_ were known
to be free of any validation errors, and have since not changed.
For example: the request may be validated during development, provided it does
not later change, or a service may validate a request once and memoize the
result to avoid validating the same request again in the future.
### Coercing Variable Values
If the operation has defined any variables, then the values for those variables
need to be coerced using the input coercion rules of the variable's declared
type. If a _request error_ is encountered during input coercion of variable
values, then the operation fails without execution.
CoerceVariableValues(schema, operation, variableValues):
- Let {coercedValues} be an empty unordered Map.
- Let {variablesDefinition} be the variables defined by {operation}.
- For each {variableDefinition} in {variablesDefinition}:
- Let {variableName} be the name of {variableDefinition}.
- Let {variableType} be the expected type of {variableDefinition}.
- Assert: {IsInputType(variableType)} must be {true}.
- Let {defaultValue} be the default value for {variableDefinition}.
- Let {hasValue} be {true} if {variableValues} provides a value for the name
{variableName}.
- Let {value} be the value provided in {variableValues} for the name
{variableName}.
- If {hasValue} is not {true} and {defaultValue} exists (including {null}):
- Let {coercedDefaultValue} be the result of coercing {defaultValue}
according to the input coercion rules of {variableType}.
- Add an entry to {coercedValues} named {variableName} with the value
{coercedDefaultValue}.
- Otherwise if {variableType} is a Non-Nullable type, and either {hasValue} is
not {true} or {value} is {null}, raise a _request error_.
- Otherwise if {hasValue} is {true}:
- If {value} is {null}:
- Add an entry to {coercedValues} named {variableName} with the value
{null}.
- Otherwise:
- If {value} cannot be coerced according to the input coercion rules of
{variableType}, raise a _request error_.
- Let {coercedValue} be the result of coercing {value} according to the
input coercion rules of {variableType}.
- Add an entry to {coercedValues} named {variableName} with the value
{coercedValue}.
- Return {coercedValues}.
Note: This algorithm is very similar to {CoerceArgumentValues()}.
## Executing Operations
The type system, as described in the "Type System" section of the spec, must
provide a query root operation type. If mutations or subscriptions are
supported, it must also provide a mutation or subscription root operation type,
respectively.
### Query
If the operation is a query, the result of the operation is the result of
executing the operation’s _root selection set_ with the query root operation
type.
An initial value may be provided when executing a query operation.
ExecuteQuery(query, schema, variableValues, initialValue):
- Let {queryType} be the root Query type in {schema}.
- Assert: {queryType} is an Object type.
- Let {rootSelectionSet} be the _root selection set_ in {query}.
- Return {ExecuteRootSelectionSet(variableValues, initialValue, queryType,
rootSelectionSet, "normal")}.
### Mutation
If the operation is a mutation, the result of the operation is the result of
executing the operation’s _root selection set_ on the mutation root object type.
This selection set should be executed serially.
It is expected that the top level fields in a mutation operation perform
side-effects on the underlying data system. Serial execution of the provided
mutations ensures against race conditions during these side-effects.
ExecuteMutation(mutation, schema, variableValues, initialValue):
- Let {mutationType} be the root Mutation type in {schema}.
- Assert: {mutationType} is an Object type.
- Let {rootSelectionSet} be the _root selection set_ in {mutation}.
- Return {ExecuteRootSelectionSet(variableValues, initialValue, mutationType,
rootSelectionSet, "serial")}.
### Subscription
If the operation is a subscription, the result is an _event stream_ called the
_response stream_ where each event in the event stream is the result of
executing the operation for each new event on an underlying _source stream_.
Executing a subscription operation creates a persistent function on the service
that maps an underlying _source stream_ to a returned _response stream_.
Subscribe(subscription, schema, variableValues, initialValue):
- Let {sourceStream} be the result of running
{CreateSourceEventStream(subscription, schema, variableValues, initialValue)}.
- Let {responseStream} be the result of running
{MapSourceToResponseEvent(sourceStream, subscription, schema,
variableValues)}.
- Return {responseStream}.
Note: In a large-scale subscription system, the {Subscribe()} and
{ExecuteSubscriptionEvent()} algorithms may be run on separate services to
maintain predictable scaling properties. See the section below on Supporting
Subscriptions at Scale.
As an example, consider a chat application. To subscribe to new messages posted
to the chat room, the client sends a request like so:
```graphql example
subscription NewMessages {
newMessage(roomId: 123) {
sender
text
}
}
```
While the client is subscribed, whenever new messages are posted to chat room
with ID "123", the selection for "sender" and "text" will be evaluated and
published to the client, for example:
```json example
{
"data": {
"newMessage": {
"sender": "Hagrid",
"text": "You're a wizard!"
}
}
}
```
The "new message posted to chat room" could use a "Pub-Sub" system where the
chat room ID is the "topic" and each "publish" contains the sender and text.
**Event Streams**
:: An _event stream_ represents a sequence of events: discrete emitted values
over time which can be observed. As an example, a "Pub-Sub" system may produce
an _event stream_ when "subscribing to a topic", with an value emitted for each
"publish" to that topic.
An _event stream_ may complete at any point, often because no further events
will occur. An _event stream_ may emit an infinite sequence of values, in which
it may never complete. If an _event stream_ encounters an error, it must
complete with that error.
An observer may at any point decide to stop observing an _event stream_ by
cancelling it. When an _event stream_ is cancelled, it must complete.
Internal user code also may cancel an _event stream_ for any reason, which would
be observed as that _event stream_ completing.
**Supporting Subscriptions at Scale**
Query and mutation operations are stateless, allowing scaling via cloning of
GraphQL service instances. Subscriptions, by contrast, are stateful and require
maintaining the GraphQL document, variables, and other context over the lifetime
of the subscription.
Consider the behavior of your system when state is lost due to the failure of a
single machine in a service. Durability and availability may be improved by
having separate dedicated services for managing subscription state and client
connectivity.
**Delivery Agnostic**
GraphQL subscriptions do not require any specific serialization format or
transport mechanism. GraphQL specifies algorithms for the creation of the
response stream, the content of each payload on that stream, and the closing of
that stream. There are intentionally no specifications for message
acknowledgement, buffering, resend requests, or any other quality of service
(QoS) details. Message serialization, transport mechanisms, and quality of
service details should be chosen by the implementing service.
#### Source Stream
:: A _source stream_ is an _event stream_ representing a sequence of root
values, each of which will trigger a GraphQL execution. Like field value
resolution, the logic to create a _source stream_ is application-specific.
CreateSourceEventStream(subscription, schema, variableValues, initialValue):
- Let {subscriptionType} be the root Subscription type in {schema}.
- Assert: {subscriptionType} is an Object type.
- Let {selectionSet} be the top level selection set in {subscription}.
- Let {collectedFieldsMap} be the result of {CollectFields(subscriptionType,
selectionSet, variableValues)}.
- If {collectedFieldsMap} does not have exactly one entry, raise a _request
error_.
- Let {fields} be the value of the first entry in {collectedFieldsMap}.
- Let {fieldName} be the name of the first entry in {fields}. Note: This value
is unaffected if an alias is used.
- Let {field} be the first entry in {fields}.
- Let {argumentValues} be the result of {CoerceArgumentValues(subscriptionType,
field, variableValues)}.
- Let {sourceStream} be the result of running
{ResolveFieldEventStream(subscriptionType, initialValue, fieldName,
argumentValues)}.
- Return {sourceStream}.
ResolveFieldEventStream(subscriptionType, rootValue, fieldName, argumentValues):
- Let {resolver} be the internal function provided by {subscriptionType} for
determining the resolved _event stream_ of a subscription field named
{fieldName}.
- Return the result of calling {resolver}, providing {rootValue} and
{argumentValues}.
Note: This {ResolveFieldEventStream()} algorithm is intentionally similar to
{ResolveFieldValue()} to enable consistency when defining resolvers on any
operation type.
#### Response Stream
Each event from the underlying _source stream_ triggers execution of the
subscription _selection set_ using that event's value as the {initialValue}.
MapSourceToResponseEvent(sourceStream, subscription, schema, variableValues):
- Let {responseStream} be a new _event stream_.
- When {sourceStream} emits {sourceValue}:
- Let {executionResult} be the result of running
{ExecuteSubscriptionEvent(subscription, schema, variableValues,
sourceValue)}.
- If internal {error} was raised:
- Cancel {sourceStream}.
- Complete {responseStream} with {error}.
- Otherwise emit {executionResult} on {responseStream}.
- When {sourceStream} completes normally:
- Complete {responseStream} normally.
- When {sourceStream} completes with {error}:
- Complete {responseStream} with {error}.
- When {responseStream} is cancelled:
- Cancel {sourceStream}.
- Complete {responseStream} normally.
- Return {responseStream}.
Note: Since {ExecuteSubscriptionEvent()} handles all _execution error_, and
_request error_ only occur during {CreateSourceEventStream()}, the only
remaining error condition handled from {ExecuteSubscriptionEvent()} are internal
exceptional errors not described by this specification.
ExecuteSubscriptionEvent(subscription, schema, variableValues, initialValue):
- Let {subscriptionType} be the root Subscription type in {schema}.
- Assert: {subscriptionType} is an Object type.
- Let {rootSelectionSet} be the _root selection set_ in {subscription}.
- Return {ExecuteRootSelectionSet(variableValues, initialValue,
subscriptionType, rootSelectionSet, "normal")}.
Note: The {ExecuteSubscriptionEvent()} algorithm is intentionally similar to
{ExecuteQuery()} since this is how each event result is produced.
#### Unsubscribe
Unsubscribe cancels the _response stream_ when a client no longer wishes to
receive payloads for a subscription. This in turn also cancels the Source
Stream, which is a good opportunity to clean up any other resources used by the
subscription.
Unsubscribe(responseStream):
- Cancel {responseStream}.
## Executing Selection Sets
Executing a GraphQL operation recursively collects and executes every selected
field in the operation. First all initially selected fields from the operation's
top most _root selection set_ are collected, then each executed. As each field
completes, all its subfields are collected, then each executed. This process
continues until there are no more subfields to collect and execute.
### Executing the Root Selection Set
:: A _root selection set_ is the top level _selection set_ provided by a GraphQL
operation. A root selection set always selects from a _root operation type_.
To execute the root selection set, the initial value being evaluated and the
root type must be known, as well as whether the fields must be executed in a
series, or normally by executing all fields in parallel (see
[Normal and Serial Execution](#sec-Normal-and-Serial-Execution)).
Executing the root selection set works similarly for queries (parallel),
mutations (serial), and subscriptions (where it is executed for each event in
the underlying Source Stream).
First, the _selection set_ is collected into a _collected fields map_ which is
then executed, returning the resulting {data} and {errors}.
ExecuteRootSelectionSet(variableValues, initialValue, objectType, selectionSet,
executionMode):
- Let {collectedFieldsMap} be the result of {CollectFields(objectType,
selectionSet, variableValues)}.
- Let {data} be the result of running
{ExecuteCollectedFields(collectedFieldsMap, objectType, initialValue,
variableValues)} _serially_ if {executionMode} is {"serial"}, otherwise
_normally_ (allowing parallelization)).
- Let {errors} be the list of all _execution error_ raised while executing the
selection set.
- Return an unordered map containing {data} and {errors}.
### Field Collection
Before execution, each _selection set_ is converted to a _collected fields map_
by collecting all fields with the same response name, including those in
referenced fragments, into an individual _field set_. This ensures that multiple
references to fields with the same response name will only be executed once.
:: A _collected fields map_ is an ordered map where each entry is a _response
name_ and its associated _field set_. A _collected fields map_ may be produced
from a selection set via {CollectFields()} or from the selection sets of all
entries of a _field set_ via {CollectSubfields()}.
:: A _field set_ is an ordered set of selected fields that share the same
_response name_ (the field alias if defined, otherwise the field's name).
Validation ensures each field in the set has the same name and arguments,
however each may have different subfields (see:
[Field Selection Merging](#sec-Field-Selection-Merging)).
Note: The order of field selections in both a _collected fields map_ and a
_field set_ are significant, hence the algorithms in this specification model
them as an ordered map and ordered set.
As an example, collecting the fields of this query's selection set would result
in a collected fields map with two entries, `"a"` and `"b"`, with two instances
of the field `a` and one of field `b`:
```graphql example
{
a {
subfield1
}
...ExampleFragment
}
fragment ExampleFragment on Query {
a {
subfield2
}
b
}
```
The depth-first-search order of each _field set_ produced by {CollectFields()}
is maintained through execution, ensuring that fields appear in the executed
response in a stable and predictable order.
CollectFields(objectType, selectionSet, variableValues, visitedFragments):
- If {visitedFragments} is not provided, initialize it to the empty set.
- Initialize {collectedFieldsMap} to an empty ordered map of ordered sets.
- For each {selection} in {selectionSet}:
- If {selection} provides the directive `@skip`, let {skipDirective} be that
directive.
- If {skipDirective}'s {if} argument is {true} or is a variable in
{variableValues} with the value {true}, continue with the next {selection}
in {selectionSet}.
- If {selection} provides the directive `@include`, let {includeDirective} be
that directive.
- If {includeDirective}'s {if} argument is not {true} and is not a variable
in {variableValues} with the value {true}, continue with the next
{selection} in {selectionSet}.
- If {selection} is a {Field}:
- Let {responseName} be the _response name_ of {selection} (the alias if
defined, otherwise the field name).
- Let {fieldsForResponseName} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Add {selection} to the {fieldsForResponseName}.
- If {selection} is a {FragmentSpread}:
- Let {fragmentSpreadName} be the name of {selection}.
- If {fragmentSpreadName} is in {visitedFragments}, continue with the next
{selection} in {selectionSet}.
- Add {fragmentSpreadName} to {visitedFragments}.
- Let {fragment} be the Fragment in the current Document whose name is
{fragmentSpreadName}.
- If no such {fragment} exists, continue with the next {selection} in
{selectionSet}.
- Let {fragmentType} be the type condition on {fragment}.
- If {DoesFragmentTypeApply(objectType, fragmentType)} is {false}, continue
with the next {selection} in {selectionSet}.
- Let {fragmentSelectionSet} be the top-level selection set of {fragment}.
- Let {fragmentCollectedFieldsMap} be the result of calling
{CollectFields(objectType, fragmentSelectionSet, variableValues,
visitedFragments)}.
- For each {responseName} and {fragmentFields} in
{fragmentCollectedFieldsMap}:
- Let {fieldsForResponseName} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Add each item from {fragmentFields} to {fieldsForResponseName}.
- If {selection} is an {InlineFragment}:
- Let {fragmentType} be the type condition on {selection}.
- If {fragmentType} is not {null} and {DoesFragmentTypeApply(objectType,
fragmentType)} is {false}, continue with the next {selection} in
{selectionSet}.
- Let {fragmentSelectionSet} be the top-level selection set of {selection}.
- Let {fragmentCollectedFieldsMap} be the result of calling
{CollectFields(objectType, fragmentSelectionSet, variableValues,
visitedFragments)}.
- For each {responseName} and {fragmentFields} in
{fragmentCollectedFieldsMap}:
- Let {fieldsForResponseName} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Append each item from {fragmentFields} to {fieldsForResponseName}.
- Return {collectedFieldsMap}.
DoesFragmentTypeApply(objectType, fragmentType):
- If {fragmentType} is an Object Type:
- If {objectType} and {fragmentType} are the same type, return {true},
otherwise return {false}.
- If {fragmentType} is an Interface Type:
- If {objectType} is an implementation of {fragmentType}, return {true},
otherwise return {false}.
- If {fragmentType} is a Union:
- If {objectType} is a possible type of {fragmentType}, return {true},
otherwise return {false}.
Note: The steps in {CollectFields()} evaluating the `@skip` and `@include`
directives may be applied in either order since they apply commutatively.
**Merging Selection Sets**
In order to execute the sub-selections of an object typed field, all _selection
sets_ of each field with the same response name in the parent _field set_ are
merged together into a single _collected fields map_ representing the subfields
to be executed next.
An example operation illustrating parallel fields with the same name with
sub-selections.
Continuing the example above,
```graphql example
{
a {
subfield1
}
...ExampleFragment
}
fragment ExampleFragment on Query {
a {
subfield2
}
b
}
```
After resolving the value for field `"a"`, the following multiple selection sets
are collected and merged together so `"subfield1"` and `"subfield2"` are
resolved in the same phase with the same value.
CollectSubfields(objectType, fields, variableValues):
- Let {collectedFieldsMap} be an empty ordered map of ordered sets.
- For each {field} in {fields}:
- Let {fieldSelectionSet} be the selection set of {field}.
- If {fieldSelectionSet} is null or empty, continue to the next field.
- Let {fieldCollectedFieldsMap} be the result of {CollectFields(objectType,
fieldSelectionSet, variableValues)}.
- For each {responseName} and {subfields} in {fieldCollectedFieldsMap}:
- Let {fieldsForResponseName} be the _field set_ value in
{collectedFieldsMap} for the key {responseName}; otherwise create the
entry with an empty ordered set.
- Add each fields from {subfields} to {fieldsForResponseName}.
- Return {collectedFieldsMap}.
Note: All the {fields} passed to {CollectSubfields()} share the same _response
name_.
### Executing Collected Fields
To execute a _collected fields map_, the object type being evaluated and the
runtime value need to be known, as well as the runtime values for any variables.
Execution will recursively resolve and complete the value of every entry in the
collected fields map, producing an entry in the result map with the same
_response name_ key.
ExecuteCollectedFields(collectedFieldsMap, objectType, objectValue,
variableValues):
- Initialize {resultMap} to an empty ordered map.
- For each {responseName} and {fields} in {collectedFieldsMap}:
- Let {fieldName} be the name of the first entry in {fields}. Note: This value
is unaffected if an alias is used.
- Let {fieldType} be the return type defined for the field {fieldName} of
{objectType}.
- If {fieldType} is defined:
- Let {responseValue} be {ExecuteField(objectType, objectValue, fieldType,
fields, variableValues)}.
- Set {responseValue} as the value for {responseName} in {resultMap}.
- Return {resultMap}.
Note: {resultMap} is ordered by which fields appear first in the operation. This
is explained in greater detail in the [Field Collection](#sec-Field-Collection)
section.
**Errors and Non-Null Types**
If during {ExecuteCollectedFields()} a _response position_ with a non-null type
raises an _execution error_ then that error must propagate to the parent
response position (the entire selection set in the case of a field, or the
entire list in the case of a list position), either resolving to {null} if
allowed or being further propagated to a parent response position.
If this occurs, any sibling response positions which have not yet executed or
have not yet yielded a value may be cancelled to avoid unnecessary work.
Note: See [Handling Execution Errors](#sec-Handling-Execution-Errors) for more
about this behavior.
### Normal and Serial Execution
Normally the executor can execute the entries in a _collected fields map_ in
whatever order it chooses (normally in parallel). Because the resolution of
fields other than top-level mutation fields must always be side effect-free and
idempotent, the execution order must not affect the result, and hence the
service has the freedom to execute the field entries in whatever order it deems
optimal.
For example, given the following collected fields map to be executed normally:
```graphql example
{
birthday {
month
}
address {
street
}
}
```
A valid GraphQL executor can resolve the four fields in whatever order it chose
(however of course `birthday` must be resolved before `month`, and `address`
before `street`).
When executing a mutation, the selections in the top most selection set will be
executed in serial order, starting with the first appearing field textually.
When executing a collected fields map serially, the executor must consider each
entry from the collected fields map in the order provided in the collected
fields map. It must determine the corresponding entry in the result map for each
item to completion before it continues on to the next entry in the collected
fields map:
For example, given the following mutation operation, the root _selection set_
must be executed serially:
```graphql example
mutation ChangeBirthdayAndAddress($newBirthday: String!, $newAddress: String!) {
changeBirthday(birthday: $newBirthday) {
month
}
changeAddress(address: $newAddress) {
street
}
}
```
Therefore the executor must, in serial:
- Run {ExecuteField()} for `changeBirthday`, which during {CompleteValue()} will
execute the `{ month }` sub-selection set normally.
- Run {ExecuteField()} for `changeAddress`, which during {CompleteValue()} will
execute the `{ street }` sub-selection set normally.
As an illustrative example, let's assume we have a mutation field
`changeTheNumber` that returns an object containing one field, `theNumber`. If
we execute the following _selection set_ serially:
```graphql example
# Note: This is a selection set, not a full document using the query shorthand.
{
first: changeTheNumber(newNumber: 1) {
theNumber
}
second: changeTheNumber(newNumber: 3) {
theNumber
}
third: changeTheNumber(newNumber: 2) {
theNumber
}
}
```
The executor will execute the following serially:
- Resolve the `changeTheNumber(newNumber: 1)` field
- Execute the `{ theNumber }` sub-selection set of `first` normally
- Resolve the `changeTheNumber(newNumber: 3)` field
- Execute the `{ theNumber }` sub-selection set of `second` normally
- Resolve the `changeTheNumber(newNumber: 2)` field
- Execute the `{ theNumber }` sub-selection set of `third` normally
A correct executor must generate the following result for that _selection set_:
```json example
{
"first": {
"theNumber": 1
},
"second": {
"theNumber": 3
},
"third": {
"theNumber": 2
}
}
```
## Executing Fields
Each entry in a result map is the result of executing a field on an object type
selected by the name of that field in a _collected fields map_. Field execution
first coerces any provided argument values, then resolves a value for the field,
and finally completes that value either by recursively executing another
selection set or coercing a scalar value.
ExecuteField(objectType, objectValue, fieldType, fields, variableValues):
- Let {field} be the first entry in {fields}.
- Let {fieldName} be the field name of {field}.
- Let {argumentValues} be the result of {CoerceArgumentValues(objectType, field,
variableValues)}.
- Let {resolvedValue} be {ResolveFieldValue(objectType, objectValue, fieldName,
argumentValues)}.
- Return the result of {CompleteValue(fieldType, fields, resolvedValue,
variableValues)}.
### Coercing Field Arguments
Fields may include arguments which are provided to the underlying runtime in
order to correctly produce a value. These arguments are defined by the field in
the type system to have a specific input type.
At each argument position in an operation may be a literal {Value}, or a
{Variable} to be provided at runtime.
CoerceArgumentValues(objectType, field, variableValues):
- Let {coercedValues} be an empty unordered Map.
- Let {argumentValues} be the argument values provided in {field}.
- Let {fieldName} be the name of {field}.
- Let {argumentDefinitions} be the arguments defined by {objectType} for the
field named {fieldName}.
- For each {argumentDefinition} in {argumentDefinitions}:
- Let {argumentName} be the name of {argumentDefinition}.
- Let {argumentType} be the expected type of {argumentDefinition}.
- Let {defaultValue} be the default value for {argumentDefinition}.
- Let {argumentValue} be the value provided in {argumentValues} for the name
{argumentName}.
- If {argumentValue} is a {Variable}:
- Let {variableName} be the name of {argumentValue}.
- If {variableValues} provides a value for the name {variableName}:
- Let {hasValue} be {true}.
- Let {value} be the value provided in {variableValues} for the name
{variableName}.
- Otherwise if {argumentValues} provides a value for the name {argumentName}.
- Let {hasValue} be {true}.
- Let {value} be {argumentValue}.
- If {hasValue} is not {true} and {defaultValue} exists (including {null}):
- Let {coercedDefaultValue} be the result of coercing {defaultValue}
according to the input coercion rules of {argumentType}.
- Add an entry to {coercedValues} named {argumentName} with the value
{coercedDefaultValue}.
- Otherwise if {argumentType} is a Non-Nullable type, and either {hasValue} is
not {true} or {value} is {null}, raise an _execution error_.
- Otherwise if {hasValue} is {true}:
- If {value} is {null}:
- Add an entry to {coercedValues} named {argumentName} with the value
{null}.
- Otherwise, if {argumentValue} is a {Variable}:
- Add an entry to {coercedValues} named {argumentName} with the value
{value}.
- Otherwise:
- If {value} cannot be coerced according to the input coercion rules of
{argumentType}, raise an _execution error_.
- Let {coercedValue} be the result of coercing {value} according to the
input coercion rules of {argumentType}.
- Add an entry to {coercedValues} named {argumentName} with the value
{coercedValue}.
- Return {coercedValues}.
Any _request error_ raised as a result of input coercion during
{CoerceArgumentValues()} should be treated instead as an _execution error_.
Note: Variable values are not coerced because they are expected to be coerced
before executing the operation in {CoerceVariableValues()}, and valid operations
must only allow usage of variables of appropriate types.
Note: Implementations are encouraged to optimize the coercion of an argument's
default value by doing so only once and caching the resulting coerced value.
### Value Resolution
While nearly all of GraphQL execution can be described generically, ultimately
the internal system exposing the GraphQL interface must provide values. This is
exposed via {ResolveFieldValue}, which produces a value for a given field on a
type for a real value.
As an example, this might accept the {objectType} `Person`, the {field}
{"soulMate"}, and the {objectValue} representing John Lennon. It would be
expected to yield the value representing Yoko Ono.
ResolveFieldValue(objectType, objectValue, fieldName, argumentValues):
- Let {resolver} be the internal function provided by {objectType} for
determining the resolved value of a field named {fieldName}.
- Return the result of calling {resolver}, providing {objectValue} and
{argumentValues}.
Note: It is common for {resolver} to be asynchronous due to relying on reading
an underlying database or networked service to produce a value. This
necessitates the rest of a GraphQL executor to handle an asynchronous execution
flow. If the field is of a list type, each value in the collection of values
returned by {resolver} may itself be retrieved asynchronously.
### Value Completion
After resolving the value for a field, it is completed by ensuring it adheres to
the expected return type. If the return type is another Object type, then the
field execution process continues recursively by collecting and executing
subfields.
CompleteValue(fieldType, fields, result, variableValues):
- If the {fieldType} is a Non-Null type:
- Let {innerType} be the inner type of {fieldType}.
- Let {completedResult} be the result of calling {CompleteValue(innerType,
fields, result, variableValues)}.
- If {completedResult} is {null}, raise an _execution error_.
- Return {completedResult}.
- If {result} is {null} (or another internal value similar to {null} such as
{undefined}), return {null}.
- If {fieldType} is a List type:
- If {result} is not a collection of values, raise an _execution error_.
- Let {innerType} be the inner type of {fieldType}.
- Return a list where each list item is the result of calling
{CompleteValue(innerType, fields, resultItem, variableValues)}, where
{resultItem} is each item in {result}.
- If {fieldType} is a Scalar or Enum type:
- Return the result of {CoerceResult(fieldType, result)}.
- If {fieldType} is an Object, Interface, or Union type:
- If {fieldType} is an Object type.
- Let {objectType} be {fieldType}.
- Otherwise if {fieldType} is an Interface or Union type.
- Let {objectType} be {ResolveAbstractType(fieldType, result)}.
- Let {collectedFieldsMap} be the result of calling
{CollectSubfields(objectType, fields, variableValues)}.
- Return the result of evaluating {ExecuteCollectedFields(collectedFieldsMap,
objectType, result, variableValues)} _normally_ (allowing for
parallelization).
**Coercing Results**
The primary purpose of value completion is to ensure that the values returned by
field resolvers are valid according to the GraphQL type system and a service's
schema. This "dynamic type checking" allows GraphQL to provide consistent
guarantees about returned types atop any service's internal runtime.
See the Scalars
[Result Coercion and Serialization](#sec-Scalars.Result-Coercion-and-Serialization)
sub-section for more detailed information about how GraphQL's built-in scalars
coerce result values.
CoerceResult(leafType, value):
- Assert: {value} is not {null}.
- Return the result of calling the internal method provided by the type system
for determining the "result coercion" of {leafType} given the value {value}.
This internal method must return a valid value for the type and not {null}.
Otherwise raise an _execution error_.
Note: If a field resolver returns {null} then it is handled within
{CompleteValue()} before {CoerceResult()} is called. Therefore both the input
and output of {CoerceResult()} must not be {null}.
**Resolving Abstract Types**
When completing a field with an abstract return type, that is an Interface or
Union return type, first the abstract type must be resolved to a relevant Object
type. This determination is made by the internal system using whatever means
appropriate.
Note: A common method of determining the Object type for an {objectValue} in
object-oriented environments, such as Java or C#, is to use the class name of
the {objectValue}.
ResolveAbstractType(abstractType, objectValue):
- Return the result of calling the internal method provided by the type system
for determining the Object type of {abstractType} given the value
{objectValue}.
### Handling Execution Errors
An _execution error_ is an error raised during field execution, value resolution
or coercion, at a specific _response position_. While these errors must be
reported in the response, they are "handled" by producing partial {"data"} in
the _response_.
Note: This is distinct from a _request error_ which results in a _request error
result_ with no data.
If an execution error is raised while resolving a field (either directly or
nested inside any lists), it is handled as though the _response position_ at
which the error occurred resolved to {null}, and the error must be added to the
{"errors"} list in the _execution result_.
If the result of resolving a _response position_ is {null} (either due to the
result of {ResolveFieldValue()} or because an execution error was raised), and
that position is of a `Non-Null` type, then an execution error is raised at that
position. The error must be added to the {"errors"} list in the _execution
result_.
If a _response position_ resolves to {null} because of an execution error which
has already been added to the {"errors"} list in the _execution result_, the
{"errors"} list must not be further affected. That is, only one error should be
added to the errors list per _response position_.
Since `Non-Null` response positions cannot be {null}, execution errors are
propagated to be handled by the parent _response position_. If the parent
response position may be {null} then it resolves to {null}, otherwise if it is a
`Non-Null` type, the execution error is further propagated to its parent
_response position_.
If a `List` type wraps a `Non-Null` type, and one of the _response position_
elements of that list resolves to {null}, then the entire list _response
position_ must resolve to {null}. If the `List` type is also wrapped in a
`Non-Null`, the execution error continues to propagate upwards.
If every _response position_ from the root of the request to the source of the
execution error has a `Non-Null` type, then the {"data"} entry in the _execution
result_ should be {null}.
================================================
FILE: spec/Section 7 -- Response.md
================================================
# Response
When a GraphQL service receives a _request_, it must return a well-formed
response. The service's response describes the result of executing the requested
operation if successful, and describes any errors raised during the request.
A response may contain both a partial response as well as a list of errors in
the case that any _execution error_ was raised and replaced with {null}.
## Response Format
:: A GraphQL request returns a _response_. A _response_ is either an _execution
result_, a _response stream_, or a _request error result_.
### Execution Result
:: A GraphQL request returns an _execution result_ when the GraphQL operation is
a query or mutation and the request included execution. Additionally, for each
event in a subscription's _source stream_, the _response stream_ will emit an
_execution result_.
An _execution result_ must be a map.
The _execution result_ must contain an entry with key {"data"}. The value of
this entry is described in the "Data" section.
If execution raised any errors, the _execution result_ must contain an entry
with key {"errors"}. The value of this entry must be a non-empty list of
_execution error_ raised during execution. Each error must be a map as described
in the "Errors" section below. If the request completed without raising any
errors, this entry must not be present.
Note: When {"errors"} is present in an _execution result_, it may be helpful for
it to appear first when serialized to make it more apparent that errors are
present.
The _execution result_ may also contain an entry with key `extensions`. The
value of this entry is described in the "Extensions" section.
### Response Stream
:: A GraphQL request returns a _response stream_ when the GraphQL operation is a
subscription and the request included execution. A response stream must be a
stream of _execution result_.
### Request Error Result
:: A GraphQL request returns a _request error result_ when one or more _request
error_ are raised, causing the request to fail before execution. This request
will result in no response data.
Note: A _request error_ may be raised before execution due to missing
information, syntax errors, validation failure, coercion failure, or any other
reason the implementation may determine should prevent the request from
proceeding.
A _request error result_ must be a map.
The _request error result_ map must contain an entry with key {"errors"}. The
value of this entry must be a non-empty list of _request error_ raised during
the _request_. It must contain at least one _request error_ indicating why no
data was able to be returned. Each error must be a map as described in the
"Errors" section below.
Note: It may be helpful for the {"errors"} key to appear first when serialized
to make it more apparent that errors are present.
The _request error result_ map must not contain an entry with key {"data"}.
The _request error result_ map may also contain an entry with key `extensions`.
The value of this entry is described in the "Extensions" section.
### Response Position
:: A _response position_ is a uniquely identifiable position in the response
data produced during execution. It is either a direct entry in the {resultMap}
of a {ExecuteSelectionSet()}, or it is a position in a (potentially nested) List
value. Each response position is uniquely identifiable via a _response path_.
:: A _response path_ uniquely identifies a _response position_ via a list of
path segments (response names or list indices) starting at the root of the
response and ending with the associated response position.
The value for a _response path_ must be a list of path segments. Path segments
that represent field _response name_ must be strings, and path segments that
represent list indices must be 0-indexed integers. If a path segment is
associated with an aliased field it must use the aliased name, since it
represents a path in the response, not in the request.
When a _response path_ is present on an _error result_, it identifies the
_response position_ which raised the error.
A single field execution may result in multiple response positions. For example,
```graphql example
{
hero(episode: $episode) {
name
friends {
name
}
}
}
```
The hero's name would be found in the _response position_ identified by the
_response path_ `["hero", "name"]`. The List of the hero's friends would be
found at `["hero", "friends"]`, the hero's first friend at
`["hero", "friends", 0]` and that friend's name at
`["hero", "friends", 0, "name"]`.
### Data
The {"data"} entry in the _execution result_ will be the result of the execution
of the requested operation. If the operation was a query, this output will be an
object of the query root operation type; if the operation was a mutation, this
output will be an object of the mutation root operation type.
The response data is the result of accumulating the resolved result of all
response positions during execution.
If an error was raised before execution begins, the _response_ must be a
_request error result_ which will result in no response data.
If an error was raised during the execution that prevented a valid response, the
{"data"} entry in the response should be `null`.
### Errors
The {"errors"} entry in the _execution result_ or _request error result_ is a
non-empty list of errors raised during the _request_, where each error is a map
of data described by the error result format below.
**Request Errors**
:: A _request error_ is an error raised during a _request_ which results in no
response data. Typically raised before execution begins, a request error may
occur due to a parse grammar or validation error in the _Document_, an inability
to determine which operation to execute, or invalid input values for variables.
A request error is typically the fault of the requesting client.
If a request error is raised, the _response_ must be a _request error result_.
The {"data"} entry in this map must not be present, the {"errors"} entry must
include the error, and request execution should be halted.
**Execution Errors**
:: An _execution error_ is an error raised during the execution of a particular
field which results in partial response data. This may occur due to failure to
coerce the arguments for the field, an internal error during value resolution,
or failure to coerce the resulting value.
Note: In previous versions of this specification _execution error_ was called
_field error_.
An execution error is typically the fault of a GraphQL service.
An _execution error_ must occur at a specific _response position_, and may occur
in any response position. The response position of an execution error is
indicated via a _response path_ in the error response's {"path"} entry.
When an execution error is raised at a given _response position_, then that
response position must not be present within the _response_ {"data"} entry
(except {null}), and the {"errors"} entry must include the error. Nested
execution is halted and sibling execution attempts to continue, producing
partial result (see
[Handling Execution Errors](#sec-Handling-Execution-Errors)).
**Error Result Format**
Every error must contain an entry with the key {"message"} with a string
description of the error intended for the developer as a guide to understand and
correct the error.
If an error can be associated to a particular point in the requested GraphQL
document, it should contain an entry with the key {"locations"} with a list of
locations, where each location is a map with the keys {"line"} and {"column"},
both positive numbers starting from `1` which describe the beginning of an
associated syntax element.
If an error can be associated to a particular field in the GraphQL result, it
must contain an entry with the key {"path"} with a _response path_ which
describes the _response position_ which raised the error. This allows clients to
identify whether a {null} resolved result is a true value or the result of an
_execution error_.
For example, if fetching one of the friends' names fails in the following
operation:
```graphql example
{
hero(episode: $episode) {
name
heroFriends: friends {
id
name
}
}
}
```
The response might look like:
```json example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [{ "line": 6, "column": 7 }],
"path": ["hero", "heroFriends", 1, "name"]
}
],
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
{
"id": "1002",
"name": null
},
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
}
```
If the field which experienced an error was declared as `Non-Null`, the `null`
result will bubble up to the next nullable field. In that case, the `path` for
the error should include the full path to the result field where the error was
raised, even if that field is not present in the response.
For example, if the `name` field from above had declared a `Non-Null` return
type in the schema, the result would look different but the error reported would
be the same:
```json example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [{ "line": 6, "column": 7 }],
"path": ["hero", "heroFriends", 1, "name"]
}
],
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
null,
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
}
```
GraphQL services may provide an additional entry to errors with key
`extensions`. This entry, if set, must have a map as its value. This entry is
reserved for implementers to add additional information to errors however they
see fit, and there are no additional restrictions on its contents.
```json example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [{ "line": 6, "column": 7 }],
"path": ["hero", "heroFriends", 1, "name"],
"extensions": {
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
}
]
}
```
GraphQL services should not provide any additional entries to the error format
since they could conflict with additional entries that may be added in future
versions of this specification.
Note: Previous versions of this spec did not describe the `extensions` entry for
error formatting. While non-specified entries are not violations, they are still
discouraged.
```json counter-example
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [{ "line": 6, "column": 7 }],
"path": ["hero", "heroFriends", 1, "name"],
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
]
}
```
### Extensions
The {"extensions"} entry in an _execution result_ or _request error result_, if
set, must have a map as its value. This entry is reserved for implementers to
extend the protocol however they see fit, and hence there are no additional
restrictions on its contents.
### Additional Entries
To ensure future changes to the protocol do not break existing services and
clients, the _execution result_ and _request error result_ maps must not contain
any entries other than those described above. Clients must ignore any entries
other than those described above.
## Serialization Format
GraphQL does not require a specific serialization format. However, clients
should use a serialization format that supports the major primitives in the
GraphQL response. In particular, the serialization format must at least support
representations of the following four primitives:
- Map
- List
- String
- Null
A serialization format should also support the following primitives, each
representing one of the common GraphQL scalar types, however a string or simpler
primitive may be used as a substitute if any are not directly supported:
- Boolean
- Int
- Float
- Enum Value
This is not meant to be an exhaustive list of what a serialization format may
encode. For example custom scalars representing a Date, Time, URI, or number
with a different precision may be represented in whichever relevant format a
given serialization format may support.
### JSON Serialization
JSON is the most common serialization format for GraphQL. Though as mentioned
above, GraphQL does not require a specific serialization format.
When using JSON as a serialization of GraphQL responses, the following JSON
values should be used to encode the related GraphQL values:
| GraphQL Value | JSON Value |
| ------------- | ----------------- |
| Map | Object |
| List | Array |
| Null | {null} |
| String | String |
| Boolean | {true} or {false} |
| Int | Number |
| Float | Number |
| Enum Value | String |
Note: For consistency and ease of notation, examples of responses are given in
JSON format throughout this document.
### Serialized Map Ordering
Since the result of evaluating a _selection set_ is ordered, the serialized Map
of results should preserve this order by writing the map entries in the same
order as those fields were requested as defined by selection set execution.
Producing a serialized response where fields are represented in the same order
in which they appear in the request improves human readability during debugging
and enables more efficient parsing of responses if the order of properties can
be anticipated.
Serialization formats which represent an ordered map should preserve the order
of requested fields as defined by {CollectFields()} in the Execution section.
Serialization formats which only represent unordered maps but where order is
still implicit in the serialization's textual order (such as JSON) should
preserve the order of requested fields textually.
For example, if the request was `{ name, age }`, a GraphQL service responding in
JSON should respond with `{ "name": "Mark", "age": 30 }` and should not respond
with `{ "age": 30, "name": "Mark" }`.
While JSON Objects are specified as an
[unordered collection of key-value pairs](https://tools.ietf.org/html/rfc7159#section-4)
the pairs are represented in an ordered manner. In other words, while the JSON
strings `{ "name": "Mark", "age": 30 }` and `{ "age": 30, "name": "Mark" }`
encode the same value, they also have observably different property orderings.
Note: This does not violate the JSON spec, as clients may still interpret
objects in the response as unordered Maps and arrive at a valid value.
================================================
FILE: spec/metadata.json
================================================
{
"biblio": {
"https://www.unicode.org/glossary": {
"byte-order-mark": "#byte_order_mark",
"leading-surrogate": "#leading_surrogate",
"trailing-surrogate": "#trailing_surrogate",
"supplementary-character": "#supplementary_character",
"supplementary-code-point": "#supplementary_code_point",
"surrogate-code-point": "#surrogate_code_point",
"surrogate-pair": "#surrogate_pair",
"unicode-scalar-value": "#unicode_scalar_value",
"utf-16": "#UTF_16"
}
}
}