@@ -258,6 +258,7 @@ describe('Parser', () => {
258
258
definitions : [
259
259
{
260
260
kind : Kind . OPERATION_DEFINITION ,
261
+ description : undefined ,
261
262
loc : { start : 0 , end : 40 } ,
262
263
operation : 'query' ,
263
264
name : undefined ,
@@ -349,6 +350,7 @@ describe('Parser', () => {
349
350
{
350
351
kind : Kind . OPERATION_DEFINITION ,
351
352
loc : { start : 0 , end : 29 } ,
353
+ description : undefined ,
352
354
operation : 'query' ,
353
355
name : undefined ,
354
356
variableDefinitions : [ ] ,
@@ -395,6 +397,75 @@ describe('Parser', () => {
395
397
} ) ;
396
398
} ) ;
397
399
400
+ it ( 'creates ast from nameless query with description' , ( ) => {
401
+ const result = parse ( dedent `
402
+ "Description"
403
+ query {
404
+ node {
405
+ id
406
+ }
407
+ }
408
+ ` ) ;
409
+
410
+ expectJSON ( result ) . toDeepEqual ( {
411
+ kind : Kind . DOCUMENT ,
412
+ loc : { start : 0 , end : 43 } ,
413
+ definitions : [
414
+ {
415
+ kind : Kind . OPERATION_DEFINITION ,
416
+ loc : { start : 0 , end : 43 } ,
417
+ description : {
418
+ kind : Kind . STRING ,
419
+ loc : { start : 0 , end : 13 } ,
420
+ value : 'Description' ,
421
+ block : false ,
422
+ } ,
423
+ operation : 'query' ,
424
+ name : undefined ,
425
+ variableDefinitions : [ ] ,
426
+ directives : [ ] ,
427
+ selectionSet : {
428
+ kind : Kind . SELECTION_SET ,
429
+ loc : { start : 20 , end : 43 } ,
430
+ selections : [
431
+ {
432
+ kind : Kind . FIELD ,
433
+ loc : { start : 24 , end : 41 } ,
434
+ alias : undefined ,
435
+ name : {
436
+ kind : Kind . NAME ,
437
+ loc : { start : 24 , end : 28 } ,
438
+ value : 'node' ,
439
+ } ,
440
+ arguments : [ ] ,
441
+ directives : [ ] ,
442
+ selectionSet : {
443
+ kind : Kind . SELECTION_SET ,
444
+ loc : { start : 29 , end : 41 } ,
445
+ selections : [
446
+ {
447
+ kind : Kind . FIELD ,
448
+ loc : { start : 35 , end : 37 } ,
449
+ alias : undefined ,
450
+ name : {
451
+ kind : Kind . NAME ,
452
+ loc : { start : 35 , end : 37 } ,
453
+ value : 'id' ,
454
+ } ,
455
+ arguments : [ ] ,
456
+ directives : [ ] ,
457
+ selectionSet : undefined ,
458
+ } ,
459
+ ] ,
460
+ } ,
461
+ } ,
462
+ ] ,
463
+ } ,
464
+ } ,
465
+ ] ,
466
+ } ) ;
467
+ } ) ;
468
+
398
469
it ( 'allows parsing without source location information' , ( ) => {
399
470
const result = parse ( '{ id }' , { noLocation : true } ) ;
400
471
expect ( 'loc' in result ) . to . equal ( false ) ;
@@ -657,4 +728,93 @@ describe('Parser', () => {
657
728
} ) ;
658
729
} ) ;
659
730
} ) ;
731
+
732
+ describe ( 'operation and variable definition descriptions' , ( ) => {
733
+ it ( 'parses operation with description and variable descriptions' , ( ) => {
734
+ const result = parse ( dedent `
735
+ "Operation description"
736
+ query myQuery(
737
+ "Variable a description"
738
+ $a: Int,
739
+ """Variable b\nmultiline description"""
740
+ $b: String
741
+ ) {
742
+ field(a: $a, b: $b)
743
+ }
744
+ ` ) ;
745
+ // Find the operation definition
746
+ const opDef = result . definitions . find (
747
+ ( d ) => d . kind === Kind . OPERATION_DEFINITION ,
748
+ ) ;
749
+ if ( ! opDef || opDef . kind !== Kind . OPERATION_DEFINITION ) {
750
+ throw new Error ( 'No operation definition found' ) ;
751
+ }
752
+ expect ( opDef . description ?. value ) . to . equal ( 'Operation description' ) ;
753
+ expect ( opDef . name ?. value ) . to . equal ( 'myQuery' ) ;
754
+ expect ( opDef . variableDefinitions ?. [ 0 ] . description ?. value ) . to . equal (
755
+ 'Variable a description' ,
756
+ ) ;
757
+ expect ( opDef . variableDefinitions ?. [ 0 ] . description ?. block ) . to . equal ( false ) ;
758
+ expect ( opDef . variableDefinitions ?. [ 1 ] . description ?. value ) . to . equal (
759
+ 'Variable b\nmultiline description' ,
760
+ ) ;
761
+ expect ( opDef . variableDefinitions ?. [ 1 ] . description ?. block ) . to . equal ( true ) ;
762
+ expect ( opDef . variableDefinitions ?. [ 0 ] . variable . name . value ) . to . equal ( 'a' ) ;
763
+ expect ( opDef . variableDefinitions ?. [ 1 ] . variable . name . value ) . to . equal ( 'b' ) ;
764
+ // Check type names safely
765
+ const typeA = opDef . variableDefinitions ?. [ 0 ] . type ;
766
+ if ( typeA && typeA . kind === Kind . NAMED_TYPE ) {
767
+ expect ( typeA . name . value ) . to . equal ( 'Int' ) ;
768
+ }
769
+ const typeB = opDef . variableDefinitions ?. [ 1 ] . type ;
770
+ if ( typeB && typeB . kind === Kind . NAMED_TYPE ) {
771
+ expect ( typeB . name . value ) . to . equal ( 'String' ) ;
772
+ }
773
+ } ) ;
774
+
775
+ it ( 'parses variable definition with description, default value, and directives' , ( ) => {
776
+ const result = parse ( dedent `
777
+ query (
778
+ "desc"
779
+ $foo: Int = 42 @dir
780
+ ) {
781
+ field(foo: $foo)
782
+ }
783
+ ` ) ;
784
+ const opDef = result . definitions . find (
785
+ ( d ) => d . kind === Kind . OPERATION_DEFINITION ,
786
+ ) ;
787
+ if ( ! opDef || opDef . kind !== Kind . OPERATION_DEFINITION ) {
788
+ throw new Error ( 'No operation definition found' ) ;
789
+ }
790
+ const varDef = opDef . variableDefinitions ?. [ 0 ] ;
791
+ expect ( varDef ?. description ?. value ) . to . equal ( 'desc' ) ;
792
+ expect ( varDef ?. variable . name . value ) . to . equal ( 'foo' ) ;
793
+ if ( varDef ?. type . kind === Kind . NAMED_TYPE ) {
794
+ expect ( varDef . type . name . value ) . to . equal ( 'Int' ) ;
795
+ }
796
+ if ( varDef ?. defaultValue && 'value' in varDef . defaultValue ) {
797
+ expect ( varDef . defaultValue . value ) . to . equal ( '42' ) ;
798
+ }
799
+ expect ( varDef ?. directives ?. [ 0 ] . name . value ) . to . equal ( 'dir' ) ;
800
+ } ) ;
801
+
802
+ it ( 'parses fragment with variable description (legacy)' , ( ) => {
803
+ const result = parse ( 'fragment Foo("desc" $foo: Int) on Bar { baz }' , {
804
+ allowLegacyFragmentVariables : true ,
805
+ } ) ;
806
+ const fragDef = result . definitions . find (
807
+ ( d ) => d . kind === Kind . FRAGMENT_DEFINITION ,
808
+ ) ;
809
+ if ( ! fragDef || fragDef . kind !== Kind . FRAGMENT_DEFINITION ) {
810
+ throw new Error ( 'No fragment definition found' ) ;
811
+ }
812
+ const varDef = fragDef . variableDefinitions ?. [ 0 ] ;
813
+ expect ( varDef ?. description ?. value ) . to . equal ( 'desc' ) ;
814
+ expect ( varDef ?. variable . name . value ) . to . equal ( 'foo' ) ;
815
+ if ( varDef ?. type . kind === Kind . NAMED_TYPE ) {
816
+ expect ( varDef . type . name . value ) . to . equal ( 'Int' ) ;
817
+ }
818
+ } ) ;
819
+ } ) ;
660
820
} ) ;
0 commit comments