@@ -514,6 +514,8 @@ function stringifyImportedFields(importedFields){
514
514
* @param ctxt
515
515
*/
516
516
function addExportedVariables ( isRoot , resVar , info , ctxt ) {
517
+ // check exports only for the root field
518
+ if ( ! isRoot ) return ;
517
519
// find all exported variables and the corresponding selection fields
518
520
for ( let fieldNode of info . fieldNodes ) {
519
521
for ( let selection of fieldNode . selectionSet . selections ) {
@@ -532,6 +534,9 @@ function addExportedVariables(isRoot, resVar, info, ctxt){
532
534
* @param ctxt
533
535
*/
534
536
function exportSelection ( isRoot , resVar , selection , info , ctxt ) {
537
+ // skip inline fragments
538
+ if ( selection . kind === 'InlineFragment' ) return ;
539
+
535
540
// skip non-root level, cannot export these fields
536
541
if ( selection . selectionSet !== undefined ) {
537
542
for ( let s of selection . selectionSet . selections ) {
@@ -544,7 +549,7 @@ function exportSelection(isRoot, resVar, selection, info, ctxt){
544
549
if ( directive . name . value !== 'export' ) continue ;
545
550
let varName = directive . arguments [ 0 ] . value . value ;
546
551
if ( ! isRoot ) {
547
- ctxt . trans . code . push ( `throw "Cannot export non-root field for variable " $${ varName } "` ) ;
552
+ ctxt . trans . code . push ( `throw "Cannot export non-root field for variable $${ varName } "; ` ) ;
548
553
}
549
554
for ( let argument of directive . arguments ) {
550
555
// Variable values will be injected instead of variable references. If a value is added to
@@ -581,9 +586,13 @@ function substituteExportedVariables(data, ctxt){
581
586
if ( typeof value === 'object' && value !== null ) {
582
587
if ( value . kind === 'VariableDefinition' ) {
583
588
const varName = value . variable . name . value ;
584
- // remove field from data object
585
- delete data [ fieldName ] ;
586
- substitutes [ fieldName ] = ctxt . trans . exportedVariables [ varName ] ;
589
+ if ( ctxt . trans . exportedVariables [ varName ] === undefined ) {
590
+ ctxt . trans . code . push ( `throw "Variable $${ varName } has not been exported";` ) ;
591
+ } else {
592
+ // remove field from data object
593
+ delete data [ fieldName ] ;
594
+ substitutes [ fieldName ] = ctxt . trans . exportedVariables [ varName ] ;
595
+ }
587
596
}
588
597
}
589
598
} ) ;
0 commit comments