1
- import { expect } from 'chai' ;
1
+ import { assert , expect } from 'chai' ;
2
2
import { describe , it } from 'mocha' ;
3
3
4
4
import { dedent } from '../../__testUtils__/dedent' ;
@@ -11,6 +11,7 @@ import { Kind } from '../kinds';
11
11
import { parse , parseConstValue , parseType , parseValue } from '../parser' ;
12
12
import { Source } from '../source' ;
13
13
import { TokenKind } from '../tokenKind' ;
14
+ import { OperationDefinitionNode } from '../ast' ;
14
15
15
16
function expectSyntaxError ( text : string ) {
16
17
return expectToThrowJSON ( ( ) => parse ( text ) ) ;
@@ -163,25 +164,11 @@ describe('Parser', () => {
163
164
# This comment has a \u0A0A multi-byte character.
164
165
{ field(arg: "Has a \u0A0A multi-byte character.") }
165
166
` ) ;
166
- const opDef = ast . definitions . find (
167
- ( d ) => d . kind === Kind . OPERATION_DEFINITION ,
167
+
168
+ expect ( ast ) . to . have . nested . property (
169
+ 'definitions[0].selectionSet.selections[0].arguments[0].value.value' ,
170
+ 'Has a \u0A0A multi-byte character.' ,
168
171
) ;
169
- if ( ! opDef || opDef . kind !== Kind . OPERATION_DEFINITION ) {
170
- throw new Error ( 'No operation definition found' ) ;
171
- }
172
- const fieldSel = opDef . selectionSet . selections [ 0 ] ;
173
- if ( fieldSel . kind !== Kind . FIELD ) {
174
- throw new Error ( 'Expected a field selection' ) ;
175
- }
176
- const args = fieldSel . arguments ;
177
- if ( ! args || args . length === 0 ) {
178
- throw new Error ( 'No arguments found' ) ;
179
- }
180
- const argValueNode = args [ 0 ] . value ;
181
- if ( argValueNode . kind !== Kind . STRING ) {
182
- throw new Error ( 'Expected a string value' ) ;
183
- }
184
- expect ( argValueNode . value ) . to . equal ( 'Has a \u0A0A multi-byte character.' ) ;
185
172
} ) ;
186
173
187
174
it ( 'parses kitchen sink' , ( ) => {
@@ -350,7 +337,6 @@ describe('Parser', () => {
350
337
351
338
it ( 'creates ast from nameless query without variables' , ( ) => {
352
339
const result = parse ( dedent `
353
- "Query description"
354
340
query {
355
341
node {
356
342
id
@@ -360,47 +346,42 @@ describe('Parser', () => {
360
346
361
347
expectJSON ( result ) . toDeepEqual ( {
362
348
kind : Kind . DOCUMENT ,
363
- loc : { start : 0 , end : 49 } ,
349
+ loc : { start : 0 , end : 29 } ,
364
350
definitions : [
365
351
{
366
352
kind : Kind . OPERATION_DEFINITION ,
367
- loc : { start : 0 , end : 49 } ,
368
- description : {
369
- kind : Kind . STRING ,
370
- loc : { start : 0 , end : 19 } ,
371
- block : false ,
372
- value : 'Query description' ,
373
- } ,
353
+ loc : { start : 0 , end : 29 } ,
354
+ description : undefined ,
374
355
operation : 'query' ,
375
356
name : undefined ,
376
357
variableDefinitions : [ ] ,
377
358
directives : [ ] ,
378
359
selectionSet : {
379
360
kind : Kind . SELECTION_SET ,
380
- loc : { start : 26 , end : 49 } ,
361
+ loc : { start : 6 , end : 29 } ,
381
362
selections : [
382
363
{
383
364
kind : Kind . FIELD ,
384
- loc : { start : 30 , end : 47 } ,
365
+ loc : { start : 10 , end : 27 } ,
385
366
alias : undefined ,
386
367
name : {
387
368
kind : Kind . NAME ,
388
- loc : { start : 30 , end : 34 } ,
369
+ loc : { start : 10 , end : 14 } ,
389
370
value : 'node' ,
390
371
} ,
391
372
arguments : [ ] ,
392
373
directives : [ ] ,
393
374
selectionSet : {
394
375
kind : Kind . SELECTION_SET ,
395
- loc : { start : 35 , end : 47 } ,
376
+ loc : { start : 15 , end : 27 } ,
396
377
selections : [
397
378
{
398
379
kind : Kind . FIELD ,
399
- loc : { start : 41 , end : 43 } ,
380
+ loc : { start : 21 , end : 23 } ,
400
381
alias : undefined ,
401
382
name : {
402
383
kind : Kind . NAME ,
403
- loc : { start : 41 , end : 43 } ,
384
+ loc : { start : 21 , end : 23 } ,
404
385
value : 'id' ,
405
386
} ,
406
387
arguments : [ ] ,
@@ -693,13 +674,10 @@ describe('Parser', () => {
693
674
field(a: $a, b: $b)
694
675
}
695
676
` ) ;
696
- // Find the operation definition
697
- const opDef = result . definitions . find (
698
- ( d ) => d . kind === Kind . OPERATION_DEFINITION ,
699
- ) ;
700
- if ( ! opDef || opDef . kind !== Kind . OPERATION_DEFINITION ) {
701
- throw new Error ( 'No operation definition found' ) ;
702
- }
677
+
678
+ const opDef = result . definitions [ 0 ] ;
679
+ assert ( opDef . kind === Kind . OPERATION_DEFINITION ) ;
680
+
703
681
expect ( opDef . description ?. value ) . to . equal ( 'Operation description' ) ;
704
682
expect ( opDef . name ?. value ) . to . equal ( 'myQuery' ) ;
705
683
expect ( opDef . variableDefinitions ?. [ 0 ] . description ?. value ) . to . equal (
@@ -712,15 +690,16 @@ describe('Parser', () => {
712
690
expect ( opDef . variableDefinitions ?. [ 1 ] . description ?. block ) . to . equal ( true ) ;
713
691
expect ( opDef . variableDefinitions ?. [ 0 ] . variable . name . value ) . to . equal ( 'a' ) ;
714
692
expect ( opDef . variableDefinitions ?. [ 1 ] . variable . name . value ) . to . equal ( 'b' ) ;
715
- // Check type names safely
693
+
716
694
const typeA = opDef . variableDefinitions ?. [ 0 ] . type ;
717
- if ( typeA && typeA . kind === Kind . NAMED_TYPE ) {
718
- expect ( typeA . name . value ) . to . equal ( 'Int' ) ;
719
- }
695
+ assert ( typeA ?. kind === Kind . NAMED_TYPE ) ;
696
+
697
+ expect ( typeA . name . value ) . to . equal ( 'Int' ) ;
698
+
720
699
const typeB = opDef . variableDefinitions ?. [ 1 ] . type ;
721
- if ( typeB && typeB . kind === Kind . NAMED_TYPE ) {
722
- expect ( typeB . name . value ) . to . equal ( 'String' ) ;
723
- }
700
+ assert ( typeB ? .kind === Kind . NAMED_TYPE ) ;
701
+
702
+ expect ( typeB . name . value ) . to . equal ( 'String' ) ;
724
703
} ) ;
725
704
726
705
it ( 'parses variable definition with description, default value, and directives' , ( ) => {
@@ -732,40 +711,35 @@ describe('Parser', () => {
732
711
field(foo: $foo)
733
712
}
734
713
` ) ;
735
- const opDef = result . definitions . find (
736
- ( d ) => d . kind === Kind . OPERATION_DEFINITION ,
737
- ) ;
738
- if ( ! opDef || opDef . kind !== Kind . OPERATION_DEFINITION ) {
739
- throw new Error ( 'No operation definition found' ) ;
740
- }
714
+
715
+ const opDef = result . definitions [ 0 ] ;
716
+ assert ( opDef . kind === Kind . OPERATION_DEFINITION ) ;
741
717
const varDef = opDef . variableDefinitions ?. [ 0 ] ;
742
718
expect ( varDef ?. description ?. value ) . to . equal ( 'desc' ) ;
743
719
expect ( varDef ?. variable . name . value ) . to . equal ( 'foo' ) ;
744
- if ( varDef ?. type . kind === Kind . NAMED_TYPE ) {
745
- expect ( varDef . type . name . value ) . to . equal ( 'Int' ) ;
746
- }
747
- if ( varDef ?. defaultValue && 'value' in varDef . defaultValue ) {
748
- expect ( varDef . defaultValue . value ) . to . equal ( '42' ) ;
749
- }
720
+
721
+ assert ( varDef ?. type . kind === Kind . NAMED_TYPE ) ;
722
+ expect ( varDef . type . name . value ) . to . equal ( 'Int' ) ;
723
+
724
+ assert ( varDef ?. defaultValue ?. kind === Kind . INT ) ;
725
+ expect ( varDef . defaultValue . value ) . to . equal ( '42' ) ;
726
+
750
727
expect ( varDef ?. directives ?. [ 0 ] . name . value ) . to . equal ( 'dir' ) ;
751
728
} ) ;
752
729
753
730
it ( 'parses fragment with variable description (legacy)' , ( ) => {
754
731
const result = parse ( 'fragment Foo("desc" $foo: Int) on Bar { baz }' , {
755
732
allowLegacyFragmentVariables : true ,
756
733
} ) ;
757
- const fragDef = result . definitions . find (
758
- ( d ) => d . kind === Kind . FRAGMENT_DEFINITION ,
759
- ) ;
760
- if ( ! fragDef || fragDef . kind !== Kind . FRAGMENT_DEFINITION ) {
761
- throw new Error ( 'No fragment definition found' ) ;
762
- }
734
+ const fragDef = result . definitions [ 0 ]
735
+ assert ( fragDef . kind === Kind . FRAGMENT_DEFINITION ) ;
736
+
763
737
const varDef = fragDef . variableDefinitions ?. [ 0 ] ;
764
738
expect ( varDef ?. description ?. value ) . to . equal ( 'desc' ) ;
765
739
expect ( varDef ?. variable . name . value ) . to . equal ( 'foo' ) ;
766
- if ( varDef ?. type . kind === Kind . NAMED_TYPE ) {
767
- expect ( varDef . type . name . value ) . to . equal ( 'Int' ) ;
768
- }
740
+
741
+ assert ( varDef ? .type . kind === Kind . NAMED_TYPE ) ;
742
+ expect ( varDef . type . name . value ) . to . equal ( 'Int' ) ;
769
743
} ) ;
770
744
771
745
it ( 'produces sensible error for description on shorthand query' , ( ) => {
0 commit comments