Skip to content

Commit 80ac728

Browse files
committed
Docs and Alterations to Schema Grammar
This PR tackles a few things: 1. Adds doc block illustrating grammar for every parse rule. 2. Changes Union syntax to match what we use in GraphQL spec. 3. Add default value to field arguments, along with test. 4. Edit tests to be of form "expect test-value to be expected-value" 5. Consistency through rules w.r.t `loc` vs `location` and short-hand object definitions.
1 parent 2a4d18b commit 80ac728

File tree

4 files changed

+184
-76
lines changed

4 files changed

+184
-76
lines changed

src/language/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ function parseVariableValue(parser): Value {
311311
return parseValue(parser, false);
312312
}
313313

314-
function parseConstValue(parser): Value {
314+
export function parseConstValue(parser): Value {
315315
return parseValue(parser, true);
316316
}
317317

src/language/schema/__tests__/parser.js

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,16 @@ function fieldNodeWithArgs(name, type, args, loc) {
5050
return {
5151
kind: 'FieldDefinition',
5252
name: name,
53-
type: type,
5453
arguments: args,
54+
type: type,
55+
loc: loc,
56+
};
57+
}
58+
59+
function enumValueNode(name, loc) {
60+
return {
61+
kind: 'EnumValueDefinition',
62+
name: nameNode(name, loc),
5563
loc: loc,
5664
};
5765
}
@@ -92,7 +100,7 @@ type Hello {
92100
],
93101
loc: loc(1, 31),
94102
};
95-
expect(printJson(expected)).to.equal(printJson(doc));
103+
expect(printJson(doc)).to.equal(printJson(expected));
96104
});
97105

98106
it('Simple non-null type', () => {
@@ -125,7 +133,7 @@ type Hello {
125133
],
126134
loc: loc(1, 32),
127135
};
128-
expect(printJson(expected)).to.equal(printJson(doc));
136+
expect(printJson(doc)).to.equal(printJson(expected));
129137
});
130138

131139

@@ -172,14 +180,6 @@ type Hello {
172180
expect(printJson(doc)).to.equal(printJson(expected));
173181
});
174182

175-
function enumValueNode(name, loc) {
176-
return {
177-
kind: 'EnumValueDefinition',
178-
name: nameNode(name, loc),
179-
loc: loc,
180-
};
181-
}
182-
183183
it('Single value enum', () => {
184184
var body = `enum Hello { WORLD }`;
185185
var loc = createLocFn(body);
@@ -246,7 +246,7 @@ interface Hello {
246246
],
247247
loc: loc(1, 36),
248248
};
249-
expect(printJson(expected)).to.equal(printJson(doc));
249+
expect(printJson(doc)).to.equal(printJson(expected));
250250
});
251251

252252
it('Simple field with arg', () => {
@@ -272,6 +272,7 @@ type Hello {
272272
kind: 'ArgumentDefinition',
273273
name: nameNode('flag', loc(22, 26)),
274274
type: typeNode('Boolean', loc(28, 35)),
275+
defaultValue: null,
275276
loc: loc(22, 35),
276277
}
277278
],
@@ -283,7 +284,49 @@ type Hello {
283284
],
284285
loc: loc(1, 46),
285286
};
286-
expect(printJson(expected)).to.equal(printJson(doc));
287+
expect(printJson(doc)).to.equal(printJson(expected));
288+
});
289+
290+
it('Simple field with arg with default value', () => {
291+
var body = `
292+
type Hello {
293+
world(flag: Boolean = true): String
294+
}`;
295+
var doc = parseSchema(body);
296+
var loc = createLocFn(body);
297+
var expected = {
298+
kind: 'SchemaDocument',
299+
definitions: [
300+
{
301+
kind: 'TypeDefinition',
302+
name: nameNode('Hello', loc(6, 11)),
303+
interfaces: [],
304+
fields: [
305+
fieldNodeWithArgs(
306+
nameNode('world', loc(16, 21)),
307+
typeNode('String', loc(45, 51)),
308+
[
309+
{
310+
kind: 'ArgumentDefinition',
311+
name: nameNode('flag', loc(22, 26)),
312+
type: typeNode('Boolean', loc(28, 35)),
313+
defaultValue: {
314+
kind: 'BooleanValue',
315+
value: true,
316+
loc: loc(38, 42),
317+
},
318+
loc: loc(22, 42),
319+
}
320+
],
321+
loc(16, 51)
322+
)
323+
],
324+
loc: loc(1, 53),
325+
}
326+
],
327+
loc: loc(1, 53),
328+
};
329+
expect(printJson(doc)).to.equal(printJson(expected));
287330
});
288331

289332
it('Simple field with list arg', () => {
@@ -313,6 +356,7 @@ type Hello {
313356
type: typeNode('String', loc(31, 37)),
314357
loc: loc(30, 38)
315358
},
359+
defaultValue: null,
316360
loc: loc(22, 38),
317361
}
318362
],
@@ -324,7 +368,7 @@ type Hello {
324368
],
325369
loc: loc(1, 49),
326370
};
327-
expect(printJson(expected)).to.equal(printJson(doc));
371+
expect(printJson(doc)).to.equal(printJson(expected));
328372
});
329373

330374
it('Simple field with two args', () => {
@@ -350,12 +394,14 @@ type Hello {
350394
kind: 'ArgumentDefinition',
351395
name: nameNode('argOne', loc(22, 28)),
352396
type: typeNode('Boolean', loc(30, 37)),
397+
defaultValue: null,
353398
loc: loc(22, 37),
354399
},
355400
{
356401
kind: 'ArgumentDefinition',
357402
name: nameNode('argTwo', loc(39, 45)),
358403
type: typeNode('Int', loc(47, 50)),
404+
defaultValue: null,
359405
loc: loc(39, 50),
360406
},
361407
],
@@ -367,11 +413,11 @@ type Hello {
367413
],
368414
loc: loc(1, 61),
369415
};
370-
expect(printJson(expected)).to.equal(printJson(doc));
416+
expect(printJson(doc)).to.equal(printJson(expected));
371417
});
372418

373419
it('Simple union', () => {
374-
var body = `union Hello { World }`;
420+
var body = `union Hello = World`;
375421
var doc = parseSchema(body);
376422
var loc = createLocFn(body);
377423
var expected = {
@@ -381,16 +427,16 @@ type Hello {
381427
kind: 'UnionDefinition',
382428
name: nameNode('Hello', loc(6, 11)),
383429
types: [typeNode('World', loc(14, 19))],
384-
loc: loc(0, 21),
430+
loc: loc(0, 19),
385431
}
386432
],
387-
loc: loc(0, 21),
433+
loc: loc(0, 19),
388434
};
389-
expect(printJson(expected)).to.equal(printJson(doc));
435+
expect(printJson(doc)).to.equal(printJson(expected));
390436
});
391437

392438
it('Union with two types', () => {
393-
var body = `union Hello { Wo | Rld }`;
439+
var body = `union Hello = Wo | Rld`;
394440
var doc = parseSchema(body);
395441
var loc = createLocFn(body);
396442
var expected = {
@@ -403,12 +449,12 @@ type Hello {
403449
typeNode('Wo', loc(14, 16)),
404450
typeNode('Rld', loc(19, 22)),
405451
],
406-
loc: loc(0, 24),
452+
loc: loc(0, 22),
407453
}
408454
],
409-
loc: loc(0, 24),
455+
loc: loc(0, 22),
410456
};
411-
expect(printJson(expected)).to.equal(printJson(doc));
457+
expect(printJson(doc)).to.equal(printJson(expected));
412458
});
413459

414460
it('Scalar', () => {
@@ -426,7 +472,7 @@ type Hello {
426472
],
427473
loc: loc(0, 12),
428474
};
429-
expect(printJson(expected)).to.equal(printJson(doc));
475+
expect(printJson(doc)).to.equal(printJson(expected));
430476
});
431477

432478
it('Simple input object', () => {
@@ -454,7 +500,7 @@ input Hello {
454500
],
455501
loc: loc(1, 32),
456502
};
457-
expect(printJson(expected)).to.equal(printJson(doc));
503+
expect(printJson(doc)).to.equal(printJson(expected));
458504
});
459505

460506
it('Simple input object with args should fail', () => {

src/language/schema/ast.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {
1212
Location,
1313
Name,
14+
Value,
1415
Type,
1516
NamedType
1617
} from '../ast';
@@ -41,15 +42,16 @@ export type FieldDefinition = {
4142
kind: 'FieldDefinition';
4243
loc?: ?Location;
4344
name: Name;
44-
type: Type;
4545
arguments: Array<ArgumentDefinition>;
46+
type: Type;
4647
}
4748

4849
export type ArgumentDefinition = {
4950
kind: 'ArgumentDefinition';
5051
loc?: ?Location;
5152
name: Name;
5253
type: Type;
54+
defaultValue?: ?Value;
5355
}
5456

5557
export type InterfaceDefinition = {

0 commit comments

Comments
 (0)