Skip to content

Commit f8b75d3

Browse files
authored
Editorial: Clarify document grammar rules (#840)
Factored out of #777. Clarifies when type definition & extension can and cannot be included in executable documents To do this more clearly, this introduces a number of new grammar rules. This does not change the actual behavior of a parser (all existing docs are parsed equivalently, no new docs are legal). However instead of using language that manipulates existing rules this can just reference those already restricted. Hopefully this actually is useful for implementations which seek to offer clear subsets for each use case.
1 parent dbabf1e commit f8b75d3

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

spec/Appendix B -- Grammar Summary.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ Document : Definition+
119119

120120
Definition :
121121
- ExecutableDefinition
122-
- TypeSystemDefinition
123-
- TypeSystemExtension
122+
- TypeSystemDefinitionOrExtension
123+
124+
ExecutableDocument : ExecutableDefinition+
124125

125126
ExecutableDefinition :
126127
- OperationDefinition
@@ -209,11 +210,19 @@ Directives[Const] : Directive[?Const]+
209210

210211
Directive[Const] : @ Name Arguments[?Const]?
211212

213+
TypeSystemDocument : TypeSystemDefinition+
214+
212215
TypeSystemDefinition :
213216
- SchemaDefinition
214217
- TypeDefinition
215218
- DirectiveDefinition
216219

220+
TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+
221+
222+
TypeSystemDefinitionOrExtension :
223+
- TypeSystemDefinition
224+
- TypeSystemExtension
225+
217226
TypeSystemExtension :
218227
- SchemaExtension
219228
- TypeExtension

spec/Section 2 -- Language.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ Document : Definition+
233233

234234
Definition :
235235
- ExecutableDefinition
236-
- TypeSystemDefinition
237-
- TypeSystemExtension
236+
- TypeSystemDefinitionOrExtension
237+
238+
ExecutableDocument : ExecutableDefinition+
238239

239240
ExecutableDefinition :
240241
- OperationDefinition
@@ -244,12 +245,19 @@ A GraphQL Document describes a complete file or request string operated on
244245
by a GraphQL service or client. A document contains multiple definitions, either
245246
executable or representative of a GraphQL type system.
246247

247-
Documents are only executable by a GraphQL service if they contain an
248-
{OperationDefinition} and otherwise only contain {ExecutableDefinition}.
249-
However documents which do not contain {OperationDefinition} or do contain
250-
{TypeSystemDefinition} or {TypeSystemExtension} may still be parsed
251-
and validated to allow client tools to represent many GraphQL uses which may
252-
appear across many individual files.
248+
Documents are only executable by a GraphQL service if they are
249+
{ExecutableDocument} and contain at least one {OperationDefinition}. A
250+
Document which contains {TypeSystemDefinitionOrExtension} must not be executed;
251+
GraphQL execution services which receive a Document containing these should
252+
return a descriptive error.
253+
254+
GraphQL services which only seek to execute GraphQL requests and not construct
255+
a new GraphQL schema may choose to only permit {ExecutableDocument}.
256+
257+
Documents which do not contain {OperationDefinition} or do contain
258+
{TypeSystemDefinitionOrExtension} may still be parsed and validated to allow
259+
client tools to represent many GraphQL uses which may appear across many
260+
individual files.
253261

254262
If a Document contains only one operation, that operation may be unnamed. If
255263
that operation is a query without variables or directives then it may also be
@@ -259,10 +267,6 @@ operations, each operation must be named. When submitting a Document with
259267
multiple operations to a GraphQL service, the name of the desired operation to
260268
be executed must also be provided.
261269

262-
GraphQL services which only seek to provide GraphQL query execution may choose
263-
to only include {ExecutableDefinition} and omit the {TypeSystemDefinition} and
264-
{TypeSystemExtension} rules from {Definition}.
265-
266270

267271
## Operations
268272

spec/Section 3 -- Type System.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ used to determine if a query is valid. The type system also describes the
55
input types of query variables to determine if values provided at runtime
66
are valid.
77

8+
TypeSystemDocument : TypeSystemDefinition+
9+
810
TypeSystemDefinition :
911
- SchemaDefinition
1012
- TypeDefinition
@@ -15,27 +17,36 @@ The GraphQL language includes an
1517
describe a GraphQL service's type system. Tools may use this definition language
1618
to provide utilities such as client code generation or service boot-strapping.
1719

18-
GraphQL tools which only seek to provide GraphQL query execution may choose not
19-
to parse {TypeSystemDefinition}.
20-
21-
A GraphQL Document which contains {TypeSystemDefinition} must not be executed;
22-
GraphQL execution services which receive a GraphQL Document containing type
23-
system definitions should return a descriptive error.
20+
GraphQL tools or services which only seek to execute GraphQL requests and not
21+
construct a new GraphQL schema may choose not to allow {TypeSystemDefinition}.
22+
Tools which only seek to produce schema and not execute requests may choose to
23+
only allow {TypeSystemDocument} and not allow {ExecutableDefinition} or
24+
{TypeSystemExtension} but should provide a descriptive error if present.
2425

2526
Note: The type system definition language is used throughout the remainder of
2627
this specification document when illustrating example type systems.
2728

2829

2930
## Type System Extensions
3031

32+
TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+
33+
34+
TypeSystemDefinitionOrExtension :
35+
- TypeSystemDefinition
36+
- TypeSystemExtension
37+
3138
TypeSystemExtension :
3239
- SchemaExtension
3340
- TypeExtension
3441

35-
Type system extensions are used to represent a GraphQL type system which has been
36-
extended from some original type system. For example, this might be used by a
37-
local service to represent data a GraphQL client only accesses locally, or by a
38-
GraphQL service which is itself an extension of another GraphQL service.
42+
Type system extensions are used to represent a GraphQL type system which has
43+
been extended from some original type system. For example, this might be used by
44+
a local service to represent data a GraphQL client only accesses locally, or by
45+
a GraphQL service which is itself an extension of another GraphQL service.
46+
47+
Tools which only seek to produce and extend schema and not execute requests may
48+
choose to only allow {TypeSystemExtensionDocument} and not allow
49+
{ExecutableDefinition} but should provide a descriptive error if present.
3950

4051

4152
## Descriptions

spec/Section 5 -- Validation.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,20 @@ union HumanOrAlien = Human | Alien
9797
**Formal Specification**
9898

9999
* For each definition {definition} in the document.
100-
* {definition} must be {OperationDefinition} or {FragmentDefinition} (it must
101-
not be {TypeSystemDefinition}).
100+
* {definition} must be {ExecutableDefinition} (it must not be
101+
{TypeSystemDefinitionOrExtension}).
102102

103103
**Explanatory Text**
104104

105105
GraphQL execution will only consider the executable definitions Operation and
106106
Fragment. Type system definitions and extensions are not executable, and are not
107107
considered during execution.
108108

109-
To avoid ambiguity, a document containing {TypeSystemDefinition} is invalid
110-
for execution.
109+
To avoid ambiguity, a document containing {TypeSystemDefinitionOrExtension} is
110+
invalid for execution.
111111

112112
GraphQL documents not intended to be directly executed may include
113-
{TypeSystemDefinition}.
113+
{TypeSystemDefinitionOrExtension}.
114114

115115
For example, the following document is invalid for execution since the original
116116
executing schema may not know about the provided type extension:

0 commit comments

Comments
 (0)