···
import { parse, parseType, parseValue } from '../parser';
import { Kind } from '../kind';
8
-
describe('print', () => {
9
-
it('prints the kitchen sink document like graphql.js does', () => {
8
+
describe('parse', () => {
9
+
it('parses the kitchen sink document like graphql.js does', () => {
const sink = readFileSync(__dirname + '/../../benchmark/kitchen_sink.graphql', {
···
expect(doc).toEqual(graphql_parse(sink, { noLocation: true }));
18
-
it('parse provides errors', () => {
18
+
it('parses basic documents', () => {
expect(() => parse('{')).toThrow();
20
+
expect(() => parse('{}x ')).toThrow();
21
+
expect(() => parse('{ field }')).not.toThrow();
22
+
expect(() => parse({ body: '{ field }' })).not.toThrow();
it('parses variable inline values', () => {
···
105
+
it('parses fragment definitions', () => {
106
+
expect(() => parse('fragment { test }')).toThrow();
107
+
expect(() => parse('fragment name { test }')).toThrow();
108
+
expect(() => parse('fragment name on name')).toThrow();
109
+
expect(() => parse('fragment Name on Type { field }')).not.toThrow();
112
+
it('parses fields', () => {
113
+
expect(() => parse('{ field: }')).toThrow();
114
+
expect(() => parse('{ alias: field() }')).toThrow();
116
+
expect(parse('{ alias: field { child } }').definitions[0]).toHaveProperty(
117
+
'selectionSet.selections.0',
131
+
kind: Kind.SELECTION_SET,
148
+
it('parses arguments', () => {
149
+
expect(() => parse('{ field() }')).toThrow();
150
+
expect(() => parse('{ field(name) }')).toThrow();
151
+
expect(() => parse('{ field(name:) }')).toThrow();
152
+
expect(() => parse('{ field(name: null }')).toThrow();
154
+
expect(parse('{ field(name: null) }').definitions[0]).toMatchObject({
155
+
kind: Kind.OPERATION_DEFINITION,
157
+
kind: Kind.SELECTION_SET,
167
+
kind: Kind.ARGUMENT,
183
+
it('parses directives', () => {
184
+
expect(() => parse('{ field @ }')).toThrow();
185
+
expect(() => parse('{ field @(test: null) }')).toThrow();
187
+
expect(parse('{ field @test(name: null) }')).toHaveProperty(
188
+
'definitions.0.selectionSet.selections.0.directives.0',
190
+
kind: Kind.DIRECTIVE,
197
+
kind: Kind.ARGUMENT,
211
+
it('parses inline fragments', () => {
212
+
expect(() => parse('{ ... on Test }')).toThrow();
213
+
expect(() => parse('{ ... {} }')).toThrow();
214
+
expect(() => parse('{ ... }')).toThrow();
216
+
expect(parse('{ ... on Test { field } }')).toHaveProperty(
217
+
'definitions.0.selectionSet.selections.0',
219
+
kind: Kind.INLINE_FRAGMENT,
222
+
kind: Kind.NAMED_TYPE,
228
+
selectionSet: expect.any(Object),
232
+
expect(parse('{ ... { field } }')).toHaveProperty('definitions.0.selectionSet.selections.0', {
233
+
kind: Kind.INLINE_FRAGMENT,
235
+
typeCondition: undefined,
236
+
selectionSet: expect.any(Object),
240
+
it('parses variable definitions', () => {
241
+
expect(() => parse('query ( { test }')).toThrow();
242
+
expect(() => parse('query ($var) { test }')).toThrow();
243
+
expect(() => parse('query ($var:) { test }')).toThrow();
244
+
expect(() => parse('query ($var: Int =) { test }')).toThrow();
246
+
expect(parse('query ($var: Int = 1) { test }').definitions[0]).toMatchObject({
247
+
kind: Kind.OPERATION_DEFINITION,
248
+
operation: 'query',
250
+
selectionSet: expect.any(Object),
251
+
variableDefinitions: [
253
+
kind: Kind.VARIABLE_DEFINITION,
255
+
kind: Kind.NAMED_TYPE,
262
+
kind: Kind.VARIABLE,
it('creates ast', () => {
···
const result = parse('{ id }', { noLocation: true });
expect('loc' in result).toBe(false);
239
-
describe('parseValue', () => {
240
-
it('parses null value', () => {
241
-
const result = parseValue('null');
242
-
expect(result).toEqual({ kind: Kind.NULL });
415
+
describe('parseValue', () => {
416
+
it('parses basic values', () => {
417
+
expect(() => parseValue('')).toThrow();
418
+
expect(parseValue('null')).toEqual({ kind: Kind.NULL });
419
+
expect(parseValue({ body: 'null' })).toEqual({ kind: Kind.NULL });
422
+
it('parses list values', () => {
423
+
const result = parseValue('[123 "abc"]');
424
+
expect(result).toEqual({
245
-
it('parses list values', () => {
246
-
const result = parseValue('[123 "abc"]');
247
-
expect(result).toEqual({
440
+
it('parses integers', () => {
441
+
expect(parseValue('12')).toEqual({
263
-
it('parses block strings', () => {
264
-
const result = parseValue('["""long""" "short"]');
265
-
expect(result).toEqual({
446
+
expect(parseValue('-12')).toEqual({
452
+
it('parses floats', () => {
453
+
expect(parseValue('12e2')).toEqual({
458
+
expect(parseValue('0.2E3')).toEqual({
463
+
expect(parseValue('-1.2e+3')).toEqual({
469
+
it('parses strings', () => {
470
+
expect(parseValue('"test"')).toEqual({
476
+
expect(parseValue('"\\t\\t"')).toEqual({
483
+
it('parses objects', () => {
484
+
expect(parseValue('{}')).toEqual({
489
+
expect(() => parseValue('{name}')).toThrow();
490
+
expect(() => parseValue('{name:}')).toThrow();
491
+
expect(() => parseValue('{name:null')).toThrow();
493
+
expect(parseValue('{name:null}')).toEqual({
497
+
kind: Kind.OBJECT_FIELD,
510
+
it('parses lists', () => {
511
+
expect(parseValue('[]')).toEqual({
516
+
expect(() => parseValue('[')).toThrow();
517
+
expect(() => parseValue('[null')).toThrow();
519
+
expect(parseValue('[null]')).toEqual({
282
-
it('allows variables', () => {
283
-
const result = parseValue('{ field: $var }');
284
-
expect(result).toEqual({
288
-
kind: Kind.OBJECT_FIELD,
529
+
it('parses block strings', () => {
530
+
expect(parseValue('["""long""" "short"]')).toEqual({
546
+
expect(parseValue('"""\n\n first\n second\n"""')).toEqual({
548
+
value: 'first\nsecond',
553
+
it('allows variables', () => {
554
+
const result = parseValue('{ field: $var }');
555
+
expect(result).toEqual({
559
+
kind: Kind.OBJECT_FIELD,
565
+
kind: Kind.VARIABLE,
294
-
kind: Kind.VARIABLE,
305
-
it('correct message for incomplete variable', () => {
307
-
return parseValue('$');
576
+
it('correct message for incomplete variable', () => {
578
+
return parseValue('$');
582
+
it('correct message for unexpected token', () => {
584
+
return parseValue(':');
589
+
describe('parseType', () => {
590
+
it('parses basic types', () => {
591
+
expect(() => parseType('')).toThrow();
592
+
expect(() => parseType('Type')).not.toThrow();
593
+
expect(() => parseType({ body: 'Type' })).not.toThrow();
596
+
it('throws on invalid inputs', () => {
597
+
expect(() => parseType('!')).toThrow();
598
+
expect(() => parseType('[String')).toThrow();
599
+
expect(() => parseType('[String!')).toThrow();
602
+
it('parses well known types', () => {
603
+
const result = parseType('String');
604
+
expect(result).toEqual({
605
+
kind: Kind.NAMED_TYPE,
311
-
it('correct message for unexpected token', () => {
313
-
return parseValue(':');
613
+
it('parses custom types', () => {
614
+
const result = parseType('MyType');
615
+
expect(result).toEqual({
616
+
kind: Kind.NAMED_TYPE,
318
-
describe('parseType', () => {
319
-
it('parses well known types', () => {
320
-
const result = parseType('String');
321
-
expect(result).toEqual({
624
+
it('parses list types', () => {
625
+
const result = parseType('[MyType]');
626
+
expect(result).toEqual({
627
+
kind: Kind.LIST_TYPE,
330
-
it('parses custom types', () => {
331
-
const result = parseType('MyType');
332
-
expect(result).toEqual({
638
+
it('parses non-null types', () => {
639
+
const result = parseType('MyType!');
640
+
expect(result).toEqual({
641
+
kind: Kind.NON_NULL_TYPE,
341
-
it('parses list types', () => {
342
-
const result = parseType('[MyType]');
343
-
expect(result).toEqual({
344
-
kind: Kind.LIST_TYPE,
346
-
kind: Kind.NAMED_TYPE,
355
-
it('parses non-null types', () => {
356
-
const result = parseType('MyType!');
357
-
expect(result).toEqual({
652
+
it('parses nested types', () => {
653
+
const result = parseType('[MyType!]');
654
+
expect(result).toEqual({
655
+
kind: Kind.LIST_TYPE,
kind: Kind.NON_NULL_TYPE,
···
369
-
it('parses nested types', () => {
370
-
const result = parseType('[MyType!]');
371
-
expect(result).toEqual({
372
-
kind: Kind.LIST_TYPE,
374
-
kind: Kind.NON_NULL_TYPE,
376
-
kind: Kind.NAMED_TYPE,