Skip to content

Commit 2a4d18b

Browse files
committed
Schema parser add Union AST definition
Adds Union definition to AST, uses in parser. Order definitions in all files to be in same order.
1 parent 0855947 commit 2a4d18b

File tree

3 files changed

+97
-89
lines changed

3 files changed

+97
-89
lines changed

src/language/schema/ast.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export type SchemaDocument = {
2424
export type SchemaDefinition =
2525
TypeDefinition |
2626
InterfaceDefinition |
27-
EnumDefinition |
27+
UnionDefinition |
2828
ScalarDefinition |
29+
EnumDefinition |
2930
InputObjectDefinition
3031

3132
export type TypeDefinition = {
@@ -36,13 +37,6 @@ export type TypeDefinition = {
3637
fields: Array<FieldDefinition>;
3738
}
3839

39-
export type InterfaceDefinition = {
40-
kind: 'InterfaceDefinition';
41-
loc?: ?Location;
42-
name: Name;
43-
fields: Array<FieldDefinition>;
44-
}
45-
4640
export type FieldDefinition = {
4741
kind: 'FieldDefinition';
4842
loc?: ?Location;
@@ -51,18 +45,31 @@ export type FieldDefinition = {
5145
arguments: Array<ArgumentDefinition>;
5246
}
5347

54-
export type InputFieldDefinition = {
55-
kind: 'InputFieldDefinition';
48+
export type ArgumentDefinition = {
49+
kind: 'ArgumentDefinition';
5650
loc?: ?Location;
5751
name: Name;
5852
type: Type;
5953
}
6054

61-
export type ArgumentDefinition = {
62-
kind: 'ArgumentDefinition';
55+
export type InterfaceDefinition = {
56+
kind: 'InterfaceDefinition';
57+
loc?: ?Location;
58+
name: Name;
59+
fields: Array<FieldDefinition>;
60+
}
61+
62+
export type UnionDefinition = {
63+
kind: 'UnionDefinition';
64+
loc?: ?Location;
65+
name: Name;
66+
types: Array<NamedType>;
67+
}
68+
69+
export type ScalarDefinition = {
70+
kind: 'ScalarDefinition';
6371
loc?: ?Location;
6472
name: Name;
65-
type: Type;
6673
}
6774

6875
export type EnumDefinition = {
@@ -78,15 +85,16 @@ export type EnumValueDefinition = {
7885
name: Name;
7986
}
8087

81-
export type ScalarDefinition = {
82-
kind: 'ScalarDefinition';
88+
export type InputObjectDefinition = {
89+
kind: 'InputObjectDefinition';
8390
loc?: ?Location;
8491
name: Name;
92+
fields: Array<InputFieldDefinition>;
8593
}
8694

87-
export type InputObjectDefinition = {
88-
kind: 'InputObjectDefinition';
95+
export type InputFieldDefinition = {
96+
kind: 'InputFieldDefinition';
8997
loc?: ?Location;
9098
name: Name;
91-
fields: Array<InputFieldDefinition>;
99+
type: Type;
92100
}

src/language/schema/kinds.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
// Schema
1111

1212
export const SCHEMA_DOCUMENT = 'SchemaDocument';
13-
export const FIELD_DEFINITION = 'FieldDefinition';
1413
export const TYPE_DEFINITION = 'TypeDefinition';
15-
export const INTERFACE_DEFINITION = 'InterfaceDefinition';
16-
export const ENUM_DEFINITION = 'EnumDefinition';
17-
export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
14+
export const FIELD_DEFINITION = 'FieldDefinition';
1815
export const ARGUMENT_DEFINITION = 'ArgumentDefinition';
16+
export const INTERFACE_DEFINITION = 'InterfaceDefinition';
1917
export const UNION_DEFINITION = 'UnionDefinition';
2018
export const SCALAR_DEFINITION = 'ScalarDefinition';
19+
export const ENUM_DEFINITION = 'EnumDefinition';
20+
export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
2121
export const INPUT_OBJECT_DEFINITION = 'InputObjectDefinition';
2222
export const INPUT_FIELD_DEFINITION = 'InputFieldDefinition';

src/language/schema/parser.js

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* of patent rights can be found in the PATENTS file in the same directory.
99
*/
1010

11-
import { Source } from './../source';
11+
import { Source } from '../source';
1212

13-
import { TokenKind } from './../lexer';
13+
import { TokenKind } from '../lexer';
1414

1515
import {
1616
ParseOptions,
@@ -24,27 +24,29 @@ import {
2424
unexpected,
2525
expectKeyword,
2626
advance,
27-
} from './../parserCore';
27+
} from '../parserCore';
2828

2929
import {
3030
parseName,
31-
parseNamedType,
3231
parseType,
33-
} from './../parser';
32+
parseNamedType,
33+
} from '../parser';
3434

35-
import { NamedType } from '../ast';
35+
import type { NamedType } from '../ast';
3636

3737
import type {
3838
SchemaDocument,
39+
SchemaDefinition,
3940
TypeDefinition,
4041
FieldDefinition,
42+
ArgumentDefinition,
4143
EnumDefinition,
4244
EnumValueDefinition,
4345
InterfaceDefinition,
44-
ArgumentDefinition,
45-
ScalarDefinition,
46+
UnionDefinition,
4647
InputObjectDefinition,
4748
InputFieldDefinition,
49+
ScalarDefinition,
4850
} from './ast';
4951

5052
import {
@@ -84,24 +86,23 @@ function parseSchemaDocument(parser): SchemaDocument {
8486
};
8587
}
8688

87-
88-
function parseSchemaDefinition(parser): any {
89+
function parseSchemaDefinition(parser): SchemaDefinition {
8990
if (!peek(parser, TokenKind.NAME)) {
9091
throw unexpected(parser);
9192
}
9293
switch (parser.token.value) {
9394
case 'type':
9495
return parseTypeDefinition(parser);
95-
case 'enum':
96-
return parseEnumDefinition(parser);
9796
case 'interface':
9897
return parseInterfaceDefinition(parser);
9998
case 'union':
10099
return parseUnionDefinition(parser);
101-
case 'input':
102-
return parseInputObjectDefinition(parser);
103100
case 'scalar':
104101
return parseScalarDefinition(parser);
102+
case 'enum':
103+
return parseEnumDefinition(parser);
104+
case 'input':
105+
return parseInputObjectDefinition(parser);
105106
default:
106107
throw unexpected(parser);
107108
}
@@ -126,6 +127,17 @@ function parseTypeDefinition(parser): TypeDefinition {
126127
};
127128
}
128129

130+
function parseInterfaces(parser): Array<NamedType> {
131+
var types = [];
132+
if (parser.token.value === 'implements') {
133+
advance(parser);
134+
do {
135+
types.push(parseNamedType(parser));
136+
} while (!peek(parser, TokenKind.BRACE_L));
137+
}
138+
return types;
139+
}
140+
129141
function parseFieldDefinition(parser): FieldDefinition {
130142
var start = parser.token.start;
131143
var name = parseName(parser);
@@ -168,46 +180,6 @@ function parseArgumentDef(parser): ArgumentDefinition {
168180
};
169181
}
170182

171-
function parseInterfaces(parser): Array<NamedType> {
172-
var types = [];
173-
if (parser.token.value === 'implements') {
174-
advance(parser);
175-
do {
176-
types.push(parseType(parser));
177-
} while (!peek(parser, TokenKind.BRACE_L));
178-
}
179-
return types;
180-
}
181-
182-
function parseEnumDefinition(parser): EnumDefinition {
183-
var start = parser.token.start;
184-
expectKeyword(parser, 'enum');
185-
var name = parseName(parser);
186-
var values = many(
187-
parser,
188-
TokenKind.BRACE_L,
189-
parseEnumValueDefinition,
190-
TokenKind.BRACE_R);
191-
var location = loc(parser, start);
192-
return {
193-
kind: ENUM_DEFINITION,
194-
name: name,
195-
values: values,
196-
loc: location,
197-
};
198-
}
199-
200-
function parseEnumValueDefinition(parser) : EnumValueDefinition {
201-
var start = parser.token.start;
202-
var name = parseName(parser);
203-
var location = loc(parser, start);
204-
return {
205-
kind: ENUM_VALUE_DEFINITION,
206-
name: name,
207-
loc: location,
208-
};
209-
}
210-
211183
function parseInterfaceDefinition(parser): InterfaceDefinition {
212184
var start = parser.token.start;
213185
expectKeyword(parser, 'interface');
@@ -225,7 +197,7 @@ function parseInterfaceDefinition(parser): InterfaceDefinition {
225197
};
226198
}
227199

228-
function parseUnionDefinition(parser) {
200+
function parseUnionDefinition(parser): UnionDefinition {
229201
var start = parser.token.start;
230202
expectKeyword(parser, 'union');
231203
var name = parseName(parser);
@@ -249,6 +221,46 @@ function parseUnionMembers(parser) {
249221
return members;
250222
}
251223

224+
function parseScalarDefinition(parser): ScalarDefinition {
225+
var start = parser.token.start;
226+
expectKeyword(parser, 'scalar');
227+
var name = parseName(parser);
228+
var location = loc(parser, start);
229+
return {
230+
kind: SCALAR_DEFINITION,
231+
name: name,
232+
loc: location,
233+
};
234+
}
235+
236+
function parseEnumDefinition(parser): EnumDefinition {
237+
var start = parser.token.start;
238+
expectKeyword(parser, 'enum');
239+
var name = parseName(parser);
240+
var values = many(
241+
parser,
242+
TokenKind.BRACE_L,
243+
parseEnumValueDefinition,
244+
TokenKind.BRACE_R);
245+
var location = loc(parser, start);
246+
return {
247+
kind: ENUM_DEFINITION,
248+
name: name,
249+
values: values,
250+
loc: location,
251+
};
252+
}
253+
254+
function parseEnumValueDefinition(parser) : EnumValueDefinition {
255+
var start = parser.token.start;
256+
var name = parseName(parser);
257+
var location = loc(parser, start);
258+
return {
259+
kind: ENUM_VALUE_DEFINITION,
260+
name: name,
261+
loc: location,
262+
};
263+
}
252264

253265
function parseInputObjectDefinition(parser): InputObjectDefinition {
254266
var start = parser.token.start;
@@ -280,15 +292,3 @@ function parseInputFieldDefinition(parser): InputFieldDefinition {
280292
loc: location,
281293
};
282294
}
283-
284-
function parseScalarDefinition(parser): ScalarDefinition {
285-
var start = parser.token.start;
286-
expectKeyword(parser, 'scalar');
287-
var name = parseName(parser);
288-
var location = loc(parser, start);
289-
return {
290-
kind: SCALAR_DEFINITION,
291-
name: name,
292-
loc: location,
293-
};
294-
}

0 commit comments

Comments
 (0)