Skip to content

Commit

Permalink
Editorial: Clarify document grammar rules (#840)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leebyron authored Apr 9, 2021
1 parent dbabf1e commit f8b75d3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
13 changes: 11 additions & 2 deletions spec/Appendix B -- Grammar Summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ Document : Definition+

Definition :
- ExecutableDefinition
- TypeSystemDefinition
- TypeSystemExtension
- TypeSystemDefinitionOrExtension

ExecutableDocument : ExecutableDefinition+

ExecutableDefinition :
- OperationDefinition
Expand Down Expand Up @@ -209,11 +210,19 @@ Directives[Const] : Directive[?Const]+

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

TypeSystemDocument : TypeSystemDefinition+

TypeSystemDefinition :
- SchemaDefinition
- TypeDefinition
- DirectiveDefinition

TypeSystemExtensionDocument : TypeSystemDefinitionOrExtension+

TypeSystemDefinitionOrExtension :
- TypeSystemDefinition
- TypeSystemExtension

TypeSystemExtension :
- SchemaExtension
- TypeExtension
Expand Down
28 changes: 16 additions & 12 deletions spec/Section 2 -- Language.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ Document : Definition+

Definition :
- ExecutableDefinition
- TypeSystemDefinition
- TypeSystemExtension
- TypeSystemDefinitionOrExtension

ExecutableDocument : ExecutableDefinition+

ExecutableDefinition :
- OperationDefinition
Expand All @@ -244,12 +245,19 @@ 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.

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
Expand All @@ -259,10 +267,6 @@ 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

Expand Down
31 changes: 21 additions & 10 deletions spec/Section 3 -- Type System.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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.

TypeSystemDocument : TypeSystemDefinition+

TypeSystemDefinition :
- SchemaDefinition
- TypeDefinition
Expand All @@ -15,27 +17,36 @@ The GraphQL language includes an
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
Expand Down
10 changes: 5 additions & 5 deletions spec/Section 5 -- Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ union HumanOrAlien = Human | Alien
**Formal Specification**

* For each definition {definition} in the document.
* {definition} must be {OperationDefinition} or {FragmentDefinition} (it must
not be {TypeSystemDefinition}).
* {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:
Expand Down

0 comments on commit f8b75d3

Please sign in to comment.