Skip to content

Commit 80701d3

Browse files
committed
[RFC] GraphQL IDL additions
This adds the type definition syntax to the GraphQL specification.
1 parent 3b08851 commit 80701d3

File tree

3 files changed

+284
-18
lines changed

3 files changed

+284
-18
lines changed

spec/Appendix B -- Grammar Summary.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ EscapedUnicode :: /[0-9A-Fa-f]{4}/
8282
EscapedCharacter :: one of `"` \ `/` b f n r t
8383

8484

85-
## Query Document
85+
## Document
8686

8787
Document : Definition+
8888

8989
Definition :
9090
- OperationDefinition
9191
- FragmentDefinition
92+
- TypeSystemDefinition
9293

9394
OperationDefinition :
9495
- SelectionSet
@@ -169,3 +170,52 @@ NonNullType :
169170
Directives : Directive+
170171

171172
Directive : @ Name Arguments?
173+
174+
TypeSystemDefinition :
175+
- TypeDefinition
176+
- TypeExtensionDefinition
177+
- DirectiveDefinition
178+
179+
TypeDefinition :
180+
- ObjectTypeDefinition
181+
- InterfaceTypeDefinition
182+
- UnionTypeDefinition
183+
- ScalarTypeDefinition
184+
- EnumTypeDefinition
185+
- InputObjectTypeDefinition
186+
187+
ObjectTypeDefinition : type Name ImplementsInterfaces? { FieldDefinition+ }
188+
189+
ImplementsInterfaces : implements NamedType+
190+
191+
FieldDefinition : Name ArgumentsDefinition? : Type
192+
193+
ArgumentsDefinition : ( InputValueDefinition+ )
194+
195+
InputValueDefinition : Name : Type DefaultValue?
196+
197+
InterfaceTypeDefinition : interface Name { FieldDefinition+ }
198+
199+
UnionTypeDefinition : union Name = UnionMembers
200+
201+
UnionMembers :
202+
- NamedType
203+
- UnionMembers | NamedType
204+
205+
ScalarTypeDefinition : scalar Name
206+
207+
EnumTypeDefinition : enum Name { EnumValueDefinition+ }
208+
209+
EnumValueDefinition : EnumValue
210+
211+
EnumValue : Name
212+
213+
InputObjectTypeDefinition : input Name { InputValueDefinition+ }
214+
215+
TypeExtensionDefinition : extend ObjectTypeDefinition
216+
217+
DirectiveDefinition : directive @ Name ArgumentsDefinition? on DirectiveLocations
218+
219+
DirectiveLocations :
220+
- Name
221+
- DirectiveLocations | Name

spec/Section 2 -- Language.md

Lines changed: 231 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ WhiteSpace ::
4545
White space is used to improve legibility of source text and act as separation
4646
between tokens, and any amount of white space may appear before or after any
4747
token. White space between tokens is not significant to the semantic meaning of
48-
a GraphQL query document, however white space characters may appear within a
48+
a GraphQL Document, however white space characters may appear within a
4949
{String} or {Comment} token.
5050

5151
Note: GraphQL intentionally does not consider Unicode "Zs" category characters
@@ -61,7 +61,7 @@ LineTerminator ::
6161

6262
Like white space, line terminators are used to improve the legibility of source
6363
text, any amount may appear before or after any other token and have no
64-
significance to the semantic meaning of a GraphQL query document. Line
64+
significance to the semantic meaning of a GraphQL Document. Line
6565
terminators are not found within any other token.
6666

6767
Note: Any error reporting which provide the line number in the source of the
@@ -83,8 +83,8 @@ comment always consists of all code points starting with the {`#`} character up
8383
to but not including the line terminator.
8484

8585
Comments behave like white space and may appear after any token, or before a
86-
line terminator, and have no significance to the semantic meaning of a GraphQL
87-
query document.
86+
line terminator, and have no significance to the semantic meaning of a
87+
GraphQL Document.
8888

8989

9090
### Insignificant Commas
@@ -93,7 +93,7 @@ Comma :: ,
9393

9494
Similar to white space and line terminators, commas ({`,`}) are used to improve
9595
the legibility of source text and separate lexical tokens but are otherwise
96-
syntactically and semantically insignificant within GraphQL query documents.
96+
syntactically and semantically insignificant within GraphQL Documents.
9797

9898
Non-significant comma characters ensure that the absence or presence of a comma
9999
does not meaningfully alter the interpreted syntax of the document, as this can
@@ -114,8 +114,8 @@ Token ::
114114
A GraphQL document is comprised of several kinds of indivisible lexical tokens
115115
defined here in a lexical grammar by patterns of source Unicode characters.
116116

117-
Tokens are later used as terminal symbols in a GraphQL query document syntactic
118-
grammars.
117+
Tokens are later used as terminal symbols in a GraphQL Document
118+
syntactic grammars.
119119

120120

121121
### Ignored Tokens
@@ -151,7 +151,7 @@ lacks the punctuation often used to describe mathematical expressions.
151151

152152
Name :: /[_A-Za-z][_0-9A-Za-z]*/
153153

154-
GraphQL query documents are full of named things: operations, fields, arguments,
154+
GraphQL Documents are full of named things: operations, fields, arguments,
155155
directives, fragments, and variables. All names must follow the same
156156
grammatical form.
157157

@@ -163,28 +163,41 @@ Names in GraphQL are limited to this <acronym>ASCII</acronym> subset of possible
163163
characters to support interoperation with as many other systems as possible.
164164

165165

166-
## Query Document
166+
## Document
167167

168168
Document : Definition+
169169

170170
Definition :
171171
- OperationDefinition
172172
- FragmentDefinition
173+
- TypeDefinition
173174

174-
A GraphQL query document describes a complete file or request string received by
175-
a GraphQL service. A document contains multiple definitions of Operations and
176-
Fragments. GraphQL query documents are only executable by a server if they
177-
contain an operation. However documents which do not contain operations may
178-
still be parsed and validated to allow client to represent a single request
179-
across many documents.
180175

176+
<<<<<<< HEAD
181177
If a document contains only one operation, that operation may be unnamed or
182178
represented in the shorthand form, which omits both the query keyword and
183179
operation name. Otherwise, if a GraphQL query document contains multiple
184180
operations, each operation must be named. When submitting a query document with
181+
=======
182+
A GraphQL Document describes a complete file or request string operated on
183+
by a GraphQL service or client tool. A document contains multiple definitions of
184+
Operations and Fragments, and if consumed by a client tool may also include Type
185+
Definitions. GraphQL Documents are only executable by a server if they
186+
contain an Operation but do not contain a Type Definition. However documents
187+
which do not contain Operations may still be parsed and validated to allow
188+
client tools to represent a single request across many documents.
189+
190+
If a Document contains only one query operation, that operation may be
191+
represented in the shorthand form, which omits the query keyword and
192+
operation name. Otherwise, if a GraphQL Document contains multiple
193+
operations, each operation must be named. When submitting a Document with
194+
>>>>>>> 2ff83c4... [RFC] GraphQL IDL additions
185195
multiple operations to a GraphQL service, the name of the desired operation to
186196
be executed must also be provided.
187197

198+
GraphQL implementations which only seek to provide GraphQL query execution may
199+
omit the {TypeDefinition} rule from Definition.
200+
188201

189202
### Operations
190203

@@ -928,3 +941,206 @@ and operations.
928941

929942
As future versions of GraphQL adopts new configurable execution capabilities,
930943
they may be exposed via directives.
944+
945+
946+
### Type System Definition
947+
948+
TypeSystemDefinition :
949+
- TypeDefinition
950+
- TypeExtensionDefinition
951+
- DirectiveDefinition
952+
953+
The GraphQL language also includes an
954+
[IDL](https://en.wikipedia.org/wiki/Interface_description_language) used to
955+
describe a GraphQL service's Type System. Tools may use these definitions to
956+
provide various utilities such as client code generation or
957+
service boot-strapping.
958+
959+
A GraphQL Document which contains type system definitions must not be executed;
960+
GraphQL execution services which receive a GraphQL Document containing type
961+
system definitions should return a descriptive error.
962+
963+
Note: This IDL is used throughout the remainder of this specification document
964+
when illustrating example type systems.
965+
966+
967+
#### Type Definition
968+
969+
TypeDefinition :
970+
- ObjectTypeDefinition
971+
- InterfaceTypeDefinition
972+
- UnionTypeDefinition
973+
- ScalarTypeDefinition
974+
- EnumTypeDefinition
975+
- InputObjectTypeDefinition
976+
977+
A GraphQL Type System is defined by many different kinds of types.
978+
979+
980+
#### Object
981+
982+
ObjectTypeDefinition : type Name ImplementsInterfaces? { FieldDefinition+ }
983+
984+
ImplementsInterfaces : implements NamedType+
985+
986+
FieldDefinition : Name ArgumentsDefinition? : Type
987+
988+
ArgumentsDefinition : ( InputValueDefinition+ )
989+
990+
InputValueDefinition : Name : Type DefaultValue?
991+
992+
Object types represent a list of named fields, each of which yield a value of a
993+
specific type. Each field itself may accept a list of named arguments.
994+
995+
Objects may implement Interface types. When implementing an Interface type, the
996+
Object type must supply all fields defined by the Interface.
997+
998+
In this example, a Object type called `TodoItem` is defined:
999+
1000+
```graphql
1001+
type TodoItem implements Node {
1002+
id: ID
1003+
title: String
1004+
isCompleted: Boolean
1005+
}
1006+
```
1007+
1008+
#### Interface
1009+
1010+
InterfaceTypeDefinition : interface Name { FieldDefinition+ }
1011+
1012+
Interface types, similarly to Object types represent a list of named fields.
1013+
Interface types are used as the type of a field when one of many possible Object
1014+
types may yielded during execution, but some fields are guaranteed. An Object
1015+
type is a possible type of an Interface when it both declares that it implements
1016+
that Interface as well as includes all defined fields.
1017+
1018+
In this example, an Interface type called `Node` is defined:
1019+
1020+
```graphql
1021+
interface Node {
1022+
id: ID
1023+
}
1024+
```
1025+
1026+
#### Union
1027+
1028+
UnionTypeDefinition : union Name = UnionMembers
1029+
1030+
UnionMembers :
1031+
- NamedType
1032+
- UnionMembers | NamedType
1033+
1034+
Union types represent a list of named Object types. Union types are used as the
1035+
type of a field when one of many possible Object types may yielded during
1036+
execution, and no fields are guaranteed. An Object type is a possible type of a
1037+
Union when it is declared by the Union.
1038+
1039+
In this example, a Union type called `Actor` is defined:
1040+
1041+
```graphql
1042+
union Actor = User | Business
1043+
```
1044+
1045+
#### Scalar
1046+
1047+
ScalarTypeDefinition : scalar Name
1048+
1049+
Scalar types represent leaf values in a GraphQL type system. While this GraphQL
1050+
specification describes a set of Scalar types which all GraphQL services must
1051+
supply, custom Scalar types may be supplied by a GraphQL service. Typically, the
1052+
set of Scalar types described in this specification are omitted from documents
1053+
which describe a schema for brevity.
1054+
1055+
In this example, a Scalar type called `DateTime` is defined:
1056+
1057+
```graphql
1058+
scalar DateTime
1059+
```
1060+
1061+
#### Enum
1062+
1063+
EnumTypeDefinition : enum Name { EnumValueDefinition+ }
1064+
1065+
EnumValueDefinition : EnumValue
1066+
1067+
EnumValue : Name
1068+
1069+
Enum types, like Scalar types, also represent leaf values in a GraphQL type
1070+
system. However Enum types describe the set of legal values.
1071+
1072+
In this example, an Enum type called `Direction` is defined:
1073+
1074+
```graphql
1075+
enum Direction {
1076+
NORTH
1077+
EAST
1078+
SOUTH
1079+
WEST
1080+
}
1081+
```
1082+
1083+
#### Input Object
1084+
1085+
InputObjectTypeDefinition : input Name { InputValueDefinition+ }
1086+
1087+
Input Object types represent complex input values which may be provided as an
1088+
field argument. Input Object types cannot be the return type of an Object or
1089+
Interface field.
1090+
1091+
In this example, an Input Object called `Point2D` is defined:
1092+
1093+
```graphql
1094+
input Point2D {
1095+
x: Float
1096+
y: Float
1097+
}
1098+
```
1099+
1100+
#### Type Extension
1101+
1102+
TypeExtensionDefinition : extend ObjectTypeDefinition
1103+
1104+
Type extensions may be used by client-side tools in order to represent a
1105+
GraphQL type system which has been extended from the type system exposed via
1106+
introspection by a GraphQL service, for example to represent fields which a
1107+
client of GraphQL uses locally, but is not provided by a GraphQL service.
1108+
1109+
Any fields or interfaces provided by the extension must not already exist on the
1110+
Object type.
1111+
1112+
In this example, a client only field is added to a `Story` type:
1113+
1114+
```graphql
1115+
extend type Story {
1116+
isHiddenLocally: Boolean
1117+
}
1118+
```
1119+
1120+
#### Directive Definition
1121+
1122+
DirectiveDefinition : directive @ Name ArgumentsDefinition? on DirectiveLocations
1123+
1124+
DirectiveLocations :
1125+
- Name
1126+
- DirectiveLocations | Name
1127+
1128+
A GraphQL Type System often also includes directives which may be used to
1129+
annotate various nodes in a GraphQL document as an indicator that they should be
1130+
evaluated differently by a validator, executor, or client tool such as a
1131+
code generator.
1132+
1133+
Since the validation of a GraphQL document includes ensuring that any directives
1134+
used are defined and used correctly, defining a directive allows for a validator
1135+
to be aware of all possible validation rules.
1136+
1137+
In this example, a directive is defined which can be used to annotate a
1138+
fragment definition:
1139+
1140+
```
1141+
directive @someAnnotation on FRAGMENT_DEFINITION
1142+
1143+
fragment SomeFragment on SomeType @someAnnotation {
1144+
someField
1145+
}
1146+
```

spec/Section 5 -- Validation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,9 @@ fragment inlineFragOnScalar on Dog {
772772

773773
** Explanatory Text **
774774

775-
Defined fragments must be used within a query document.
775+
Defined fragments must be used within a document.
776776

777-
For example the following is an invalid query document:
777+
For example the following is an invalid document:
778778

779779
```!graphql
780780
fragment nameFragment on Dog { # unused

0 commit comments

Comments
 (0)