Skip to content

Commit b0885a0

Browse files
committed
Updating schema parser to more closely match current state of RFC
graphql/graphql-spec#90
1 parent fdafe32 commit b0885a0

File tree

4 files changed

+65
-60
lines changed

4 files changed

+65
-60
lines changed

src/language/ast.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ export type Document = {
7777

7878
export type Definition = OperationDefinition
7979
| FragmentDefinition
80-
| TypeDefinition
81-
| TypeExtensionDefinition
82-
| DirectiveDefinition
80+
| TypeSystemDefinition
8381

8482
export type OperationDefinition = {
8583
kind: 'OperationDefinition';
@@ -256,15 +254,25 @@ export type NonNullType = {
256254
type: NamedType | ListType;
257255
}
258256

259-
// Type Definition
257+
// Type System Definition
260258

261-
export type TypeDefinition = ObjectTypeDefinition
259+
export type TypeSystemDefinition = TypeDefinition
260+
| TypeExtensionDefinition
261+
| DirectiveDefinition
262+
263+
export type TypeDefinition = ScalarTypeDefinition
264+
| ObjectTypeDefinition
262265
| InterfaceTypeDefinition
263266
| UnionTypeDefinition
264-
| ScalarTypeDefinition
265267
| EnumTypeDefinition
266268
| InputObjectTypeDefinition
267269

270+
export type ScalarTypeDefinition = {
271+
kind: 'ScalarTypeDefinition';
272+
loc?: ?Location;
273+
name: Name;
274+
}
275+
268276
export type ObjectTypeDefinition = {
269277
kind: 'ObjectTypeDefinition';
270278
loc?: ?Location;
@@ -303,12 +311,6 @@ export type UnionTypeDefinition = {
303311
types: Array<NamedType>;
304312
}
305313

306-
export type ScalarTypeDefinition = {
307-
kind: 'ScalarTypeDefinition';
308-
loc?: ?Location;
309-
name: Name;
310-
}
311-
312314
export type EnumTypeDefinition = {
313315
kind: 'EnumTypeDefinition';
314316
loc?: ?Location;

src/language/kinds.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ export const NAMED_TYPE = 'NamedType';
4848
export const LIST_TYPE = 'ListType';
4949
export const NON_NULL_TYPE = 'NonNullType';
5050

51+
// Type System Definitions
52+
5153
// Type Definitions
5254

55+
export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
5356
export const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
5457
export const FIELD_DEFINITION = 'FieldDefinition';
5558
export const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
5659
export const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
5760
export const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
58-
export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
5961
export const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
6062
export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
6163
export const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';

src/language/parser.js

+44-43
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ import type {
3939
Type,
4040
NamedType,
4141

42-
TypeDefinition,
42+
TypeSystemDefinition,
43+
44+
ScalarTypeDefinition,
4345
ObjectTypeDefinition,
4446
FieldDefinition,
4547
InputValueDefinition,
4648
InterfaceTypeDefinition,
4749
UnionTypeDefinition,
48-
ScalarTypeDefinition,
4950
EnumTypeDefinition,
5051
EnumValueDefinition,
5152
InputObjectTypeDefinition,
@@ -85,12 +86,12 @@ import {
8586
LIST_TYPE,
8687
NON_NULL_TYPE,
8788

89+
SCALAR_TYPE_DEFINITION,
8890
OBJECT_TYPE_DEFINITION,
8991
FIELD_DEFINITION,
9092
INPUT_VALUE_DEFINITION,
9193
INTERFACE_TYPE_DEFINITION,
9294
UNION_TYPE_DEFINITION,
93-
SCALAR_TYPE_DEFINITION,
9495
ENUM_TYPE_DEFINITION,
9596
ENUM_VALUE_DEFINITION,
9697
INPUT_OBJECT_TYPE_DEFINITION,
@@ -185,8 +186,7 @@ function parseDocument(parser: Parser): Document {
185186
* Definition :
186187
* - OperationDefinition
187188
* - FragmentDefinition
188-
* - TypeDefinition
189-
* - TypeExtensionDefinition
189+
* - TypeSystemDefinition
190190
*/
191191
function parseDefinition(parser: Parser): Definition {
192192
if (peek(parser, TokenKind.BRACE_L)) {
@@ -202,14 +202,15 @@ function parseDefinition(parser: Parser): Definition {
202202

203203
case 'fragment': return parseFragmentDefinition(parser);
204204

205+
// Note: the Type System IDL is an experimental non-spec addition.
206+
case 'scalar':
205207
case 'type':
206208
case 'interface':
207209
case 'union':
208-
case 'scalar':
209210
case 'enum':
210-
case 'input': return parseTypeDefinition(parser);
211-
case 'extend': return parseTypeExtensionDefinition(parser);
212-
case 'directive': return parseDirectiveDefinition(parser);
211+
case 'input':
212+
case 'extend':
213+
case 'directive': return parseTypeSystemDefinition(parser);
213214
}
214215
}
215216

@@ -649,34 +650,48 @@ export function parseNamedType(parser: Parser): NamedType {
649650
// Implements the parsing rules in the Type Definition section.
650651

651652
/**
653+
* TypeSystemDefinition :
654+
* - TypeDefinition
655+
* - TypeExtensionDefinition
656+
* - DirectiveDefinition
657+
*
652658
* TypeDefinition :
659+
* - ScalarTypeDefinition
653660
* - ObjectTypeDefinition
654661
* - InterfaceTypeDefinition
655662
* - UnionTypeDefinition
656-
* - ScalarTypeDefinition
657663
* - EnumTypeDefinition
658664
* - InputObjectTypeDefinition
659665
*/
660-
function parseTypeDefinition(parser: Parser): TypeDefinition {
661-
if (!peek(parser, TokenKind.NAME)) {
662-
throw unexpected(parser);
663-
}
664-
switch (parser.token.value) {
665-
case 'type':
666-
return parseObjectTypeDefinition(parser);
667-
case 'interface':
668-
return parseInterfaceTypeDefinition(parser);
669-
case 'union':
670-
return parseUnionTypeDefinition(parser);
671-
case 'scalar':
672-
return parseScalarTypeDefinition(parser);
673-
case 'enum':
674-
return parseEnumTypeDefinition(parser);
675-
case 'input':
676-
return parseInputObjectTypeDefinition(parser);
677-
default:
678-
throw unexpected(parser);
666+
function parseTypeSystemDefinition(parser: Parser): TypeSystemDefinition {
667+
if (peek(parser, TokenKind.NAME)) {
668+
switch (parser.token.value) {
669+
case 'scalar': return parseScalarTypeDefinition(parser);
670+
case 'type': return parseObjectTypeDefinition(parser);
671+
case 'interface': return parseInterfaceTypeDefinition(parser);
672+
case 'union': return parseUnionTypeDefinition(parser);
673+
case 'enum': return parseEnumTypeDefinition(parser);
674+
case 'input': return parseInputObjectTypeDefinition(parser);
675+
case 'extend': return parseTypeExtensionDefinition(parser);
676+
case 'directive': return parseDirectiveDefinition(parser);
677+
}
679678
}
679+
680+
throw unexpected(parser);
681+
}
682+
683+
/**
684+
* ScalarTypeDefinition : scalar Name
685+
*/
686+
function parseScalarTypeDefinition(parser: Parser): ScalarTypeDefinition {
687+
const start = parser.token.start;
688+
expectKeyword(parser, 'scalar');
689+
const name = parseName(parser);
690+
return {
691+
kind: SCALAR_TYPE_DEFINITION,
692+
name,
693+
loc: loc(parser, start),
694+
};
680695
}
681696

682697
/**
@@ -816,20 +831,6 @@ function parseUnionMembers(parser: Parser): Array<NamedType> {
816831
return members;
817832
}
818833

819-
/**
820-
* ScalarTypeDefinition : scalar Name
821-
*/
822-
function parseScalarTypeDefinition(parser: Parser): ScalarTypeDefinition {
823-
const start = parser.token.start;
824-
expectKeyword(parser, 'scalar');
825-
const name = parseName(parser);
826-
return {
827-
kind: SCALAR_TYPE_DEFINITION,
828-
name,
829-
loc: loc(parser, start),
830-
};
831-
}
832-
833834
/**
834835
* EnumTypeDefinition : enum Name { EnumValueDefinition+ }
835836
*/

src/language/printer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ const printDocASTReducer = {
9292
ListType: ({ type }) => '[' + type + ']',
9393
NonNullType: ({ type }) => type + '!',
9494

95-
// Type Definitions
95+
// Type System Definitions
96+
97+
ScalarTypeDefinition: ({ name }) =>
98+
`scalar ${name}`,
9699

97100
ObjectTypeDefinition: ({ name, interfaces, fields }) =>
98101
'type ' + name + ' ' +
@@ -111,9 +114,6 @@ const printDocASTReducer = {
111114
UnionTypeDefinition: ({ name, types }) =>
112115
`union ${name} = ${join(types, ' | ')}`,
113116

114-
ScalarTypeDefinition: ({ name }) =>
115-
`scalar ${name}`,
116-
117117
EnumTypeDefinition: ({ name, values }) =>
118118
`enum ${name} ${block(values)}`,
119119

0 commit comments

Comments
 (0)