Skip to content

Commit 58d79ec

Browse files
committed
[RFC] GraphQL IDL additions
This adds the type definition syntax to the GraphQL specification.
1 parent e67a987 commit 58d79ec

File tree

3 files changed

+229
-21
lines changed

3 files changed

+229
-21
lines changed

spec/Appendix B -- Grammar Summary.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ EscapedUnicode :: u /[0-9A-Fa-f]{4}/
8484
EscapedCharacter :: one of `"` \ `/` b f n r t
8585

8686

87-
## Query Document
87+
## Document
8888

8989
Document : Definition+
9090

9191
Definition :
9292
- OperationDefinition
9393
- FragmentDefinition
94+
- TypeDefinition
9495

9596
OperationDefinition :
9697
- SelectionSet
@@ -171,3 +172,38 @@ NonNullType :
171172
Directives : Directive+
172173

173174
Directive : @ Name Arguments?
175+
176+
TypeDefinition :
177+
- ObjectTypeDefinition
178+
- InterfaceTypeDefinition
179+
- UnionTypeDefinition
180+
- ScalarTypeDefinition
181+
- EnumTypeDefinition
182+
- InputObjectTypeDefinition
183+
- TypeExtensionDefinition
184+
185+
ObjectTypeDefinition : type Name ImplementsInterfaces? { FieldDefinition+ }
186+
187+
ImplementsInterfaces : implements NamedType+
188+
189+
FieldDefinition : Name ArgumentsDefinition? : Type
190+
191+
ArgumentsDefinition : ( InputValueDefinition+ )
192+
193+
InputValueDefinition : Name : Type DefaultValue?
194+
195+
InterfaceTypeDefinition : interface Name { FieldDefinition+ }
196+
197+
UnionTypeDefinition : union Name = UnionMembers
198+
199+
ScalarTypeDefinition : scalar Name
200+
201+
EnumTypeDefinition : enum Name { EnumValueDefinition+ }
202+
203+
EnumValueDefinition : EnumValue
204+
205+
EnumValue : Name
206+
207+
InputObjectTypeDefinition : input Name { InputValueDefinition+ }
208+
209+
TypeExtensionDefinition : extend ObjectTypeDefinition

spec/Section 2 -- Language.md

+190-18
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ WhiteSpace ::
3535
White space is used to improve legibility of source text and act as separation
3636
between tokens, and any amount of white space may appear before or after any
3737
token. White space between tokens is not significant to the semantic meaning of
38-
a GraphQL query document, however white space characters may appear within a
38+
a GraphQL Document, however white space characters may appear within a
3939
{String} or {Comment} token.
4040

4141

@@ -49,7 +49,7 @@ LineTerminator ::
4949

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

5555

@@ -67,8 +67,8 @@ comment always consists of all code points starting with the {`#`} character up
6767
to but not including the line terminator.
6868

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

7373

7474
### Insignificant Commas
@@ -77,7 +77,7 @@ Comma :: ,
7777

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

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

101-
Tokens are later used as terminal symbols in a GraphQL query document syntactic
102-
grammars.
101+
Tokens are later used as terminal symbols in a GraphQL Document
102+
syntactic grammars.
103103

104104
### Ignored Tokens
105105

@@ -133,7 +133,7 @@ lacks the punctionation often used to describe mathematical expressions.
133133

134134
Name :: /[_A-Za-z][_0-9A-Za-z]*/
135135

136-
GraphQL query documents are full of named things: operations, fields, arguments,
136+
GraphQL Documents are full of named things: operations, fields, arguments,
137137
directives, fragments, and variables. All names must follow the same
138138
grammatical form.
139139

@@ -145,28 +145,34 @@ Names in GraphQL are limited to this <acronym>ASCII</acronym> subset of possible
145145
characters to support interoperation with as many other systems as possible.
146146

147147

148-
## Query Document
148+
## Document
149149

150150
Document : Definition+
151151

152152
Definition :
153153
- OperationDefinition
154154
- FragmentDefinition
155+
- TypeDefinition
155156

156-
A GraphQL query document describes a complete file or request string received by
157-
a GraphQL service. A document contains multiple definitions of Operations and
158-
Fragments. GraphQL query documents are only executable by a server if they
159-
contain an operation. However documents which do not contain operations may
160-
still be parsed and validated to allow client to represent a single request
161-
across many documents.
162157

163-
If a query document contains only one query operation, that operation may be
158+
A GraphQL Document describes a complete file or request string operated on
159+
by a GraphQL service or client tool. A document contains multiple definitions of
160+
Operations and Fragments, and if consumed by a client tool may also include Type
161+
Definitions. GraphQL Documents are only executable by a server if they
162+
contain an Operation but do not contain a Type Definition. However documents
163+
which do not contain Operations may still be parsed and validated to allow
164+
client tools to represent a single request across many documents.
165+
166+
If a Document contains only one query operation, that operation may be
164167
represented in the shorthand form, which omits the query keyword and
165-
operation name. Otherwise, if a GraphQL query document contains multiple
166-
operations, each operation must be named. When submitting a query document with
168+
operation name. Otherwise, if a GraphQL Document contains multiple
169+
operations, each operation must be named. When submitting a Document with
167170
multiple operations to a GraphQL service, the name of the desired operation to
168171
be executed must also be provided.
169172

173+
GraphQL implementations which only seek to provide GraphQL query execution may
174+
omit the {TypeDefinition} rule from Definition.
175+
170176

171177
### Operations
172178

@@ -873,3 +879,169 @@ FragmentSpreadDirectives(fragmentSpread) :
873879
* If {directives} does not contain a directive named {directive}.
874880
* Add {directive} into {directives}
875881
* Return {directives}
882+
883+
884+
### Type Definitions
885+
886+
TypeDefinition :
887+
- ObjectTypeDefinition
888+
- InterfaceTypeDefinition
889+
- UnionTypeDefinition
890+
- ScalarTypeDefinition
891+
- EnumTypeDefinition
892+
- InputObjectTypeDefinition
893+
- TypeExtensionDefinition
894+
895+
896+
The GraphQL language also includes an
897+
[IDL](https://en.wikipedia.org/wiki/Interface_description_language) used to
898+
describe a GraphQL service's Type System. Tools may use these definitions to
899+
provide various utilities such as client code generation or
900+
service boot-strapping.
901+
902+
A GraphQL Document which contains type definitions must not be executed; GraphQL
903+
execution services which receive a GraphQL Document containing type definitions
904+
should return a descriptive error.
905+
906+
Note: This IDL is used throughout the remainder of this specification document
907+
when illustrating examples.
908+
909+
#### Object
910+
911+
ObjectTypeDefinition : type Name ImplementsInterfaces? { FieldDefinition+ }
912+
913+
ImplementsInterfaces : implements NamedType+
914+
915+
FieldDefinition : Name ArgumentsDefinition? : Type
916+
917+
ArgumentsDefinition : ( InputValueDefinition+ )
918+
919+
InputValueDefinition : Name : Type DefaultValue?
920+
921+
Object types represent a list of named fields, each of which yield a value of a
922+
specific type. Each field itself may accept a list of named arguments.
923+
924+
Objects may implement Interface types. When implementing an Interface type, the
925+
Object type must supply all fields defined by the Interface.
926+
927+
In this example, a Object type called `TodoItem` is defined:
928+
929+
```graphql
930+
type TodoItem implements Node {
931+
id: ID
932+
title: String
933+
isCompleted: Boolean
934+
}
935+
```
936+
937+
#### Interface
938+
939+
InterfaceTypeDefinition : interface Name { FieldDefinition+ }
940+
941+
Interface types, similarly to Object types represent a list of named fields.
942+
Interface types are used as the type of a field when one of many possible Object
943+
types may yielded during execution, but some fields are guaranteed. An Object
944+
type is a possible type of an Interface when it both declares that it implements
945+
that Interface as well as includes all defined fields.
946+
947+
In this example, an Interface type called `Node` is defined:
948+
949+
```graphql
950+
interface Node {
951+
id: ID
952+
}
953+
```
954+
955+
#### Union
956+
957+
UnionTypeDefinition : union Name = UnionMembers
958+
959+
UnionMembers :
960+
- NamedType
961+
- UnionMembers | NamedType
962+
963+
Union types represent a list of named Object types. Union types are used as the
964+
type of a field when one of many possible Object types may yielded during
965+
execution, and no fields are guaranteed. An Object type is a possible type of a
966+
Union when it is declared by the Union.
967+
968+
In this example, a Union type called `Actor` is defined:
969+
970+
```graphql
971+
union Actor = User | Business
972+
```
973+
974+
#### Scalar
975+
976+
ScalarTypeDefinition : scalar Name
977+
978+
Scalar types represent leaf values in a GraphQL type system. While this GraphQL
979+
specification describes a set of Scalar types which all GraphQL services must
980+
supply, custom Scalar types may be supplied by a GraphQL service. Typically, the
981+
set of Scalar types described in this specification are omitted from documents
982+
which describe a schema for brevity.
983+
984+
In this example, a Scalar type called `DateTime` is defined:
985+
986+
```graphql
987+
scalar DateTime
988+
```
989+
990+
#### Enum
991+
992+
EnumTypeDefinition : enum Name { EnumValueDefinition+ }
993+
994+
EnumValueDefinition : EnumValue
995+
996+
EnumValue : Name
997+
998+
Enum types, like Scalar types, also represent leaf values in a GraphQL type
999+
system. However Enum types describe the set of legal values.
1000+
1001+
In this example, an Enum type called `Direction` is defined:
1002+
1003+
```graphql
1004+
enum Direction {
1005+
NORTH
1006+
EAST
1007+
SOUTH
1008+
WEST
1009+
}
1010+
```
1011+
1012+
#### Input Object
1013+
1014+
InputObjectTypeDefinition : input Name { InputValueDefinition+ }
1015+
1016+
Input Object types represent complex input values which may be provided as an
1017+
field argument. Input Object types cannot be the return type of an Object or
1018+
Interface field.
1019+
1020+
In this example, an Input Object called `Point2D` is defined:
1021+
1022+
```graphql
1023+
input Point2D {
1024+
x: Float
1025+
y: Float
1026+
}
1027+
```
1028+
1029+
#### Extension
1030+
1031+
TypeExtensionDefinition : extend ObjectTypeDefinition
1032+
1033+
Type extensions may be used by client-side tools in order to represent a
1034+
GraphQL type system which has been extended from the type system exposed via
1035+
introspection by a GraphQL service, for example to represent fields which a
1036+
client of GraphQL uses locally, but is not provided by a GraphQL service.
1037+
1038+
Any fields or interfaces provided by the extension must not already exist on the
1039+
Object type.
1040+
1041+
In this example, a client only field is added to a `Story` type:
1042+
1043+
```graphql
1044+
extend type Story {
1045+
isHiddenLocally: Boolean
1046+
}
1047+
```

spec/Section 5 -- Validation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,9 @@ fragment inlineFragOnScalar on Dog {
721721

722722
** Explanatory Text **
723723

724-
Defined fragments must be used within a query document.
724+
Defined fragments must be used within a document.
725725

726-
For example the following is an invalid query document:
726+
For example the following is an invalid document:
727727

728728
```!graphql
729729
fragment nameFragment on Dog { # unused

0 commit comments

Comments
 (0)