1
1
import type { Maybe } from '../jsutils/Maybe.js' ;
2
2
3
- import type { ConstValueNode , ValueNode } from '../language/ast.js' ;
3
+ import type {
4
+ ConstValueNode ,
5
+ ObjectFieldNode ,
6
+ ValueNode ,
7
+ } from '../language/ast.js' ;
4
8
import { Kind } from '../language/kinds.js' ;
5
- import { visit } from '../language/visitor.js' ;
6
9
7
10
import type { VariableValues } from '../execution/values.js' ;
8
11
@@ -21,9 +24,9 @@ export function replaceVariables(
21
24
variableValues ?: Maybe < VariableValues > ,
22
25
fragmentVariableValues ?: Maybe < VariableValues > ,
23
26
) : ConstValueNode {
24
- return visit ( valueNode , {
25
- Variable ( node ) {
26
- const varName = node . name . value ;
27
+ switch ( valueNode . kind ) {
28
+ case Kind . VARIABLE : {
29
+ const varName = valueNode . name . value ;
27
30
const scopedVariableValues = fragmentVariableValues ?. sources [ varName ]
28
31
? fragmentVariableValues
29
32
: variableValues ;
@@ -36,23 +39,19 @@ export function replaceVariables(
36
39
if ( scopedVariableSource . value === undefined ) {
37
40
const defaultValue = scopedVariableSource . signature . defaultValue ;
38
41
if ( defaultValue !== undefined ) {
39
- return defaultValue . literal ;
42
+ return defaultValue . literal as ConstValueNode ;
40
43
}
41
44
}
42
45
43
46
return valueToLiteral (
44
47
scopedVariableSource . value ,
45
48
scopedVariableSource . signature . type ,
46
- ) ;
47
- } ,
48
- ObjectValue ( node ) {
49
- return {
50
- ...node ,
51
- // Filter out any fields with a missing variable.
52
- fields : node . fields . filter ( ( field ) => {
53
- if ( field . value . kind !== Kind . VARIABLE ) {
54
- return true ;
55
- }
49
+ ) as ConstValueNode ;
50
+ }
51
+ case Kind . OBJECT : {
52
+ const newFields : Array < ObjectFieldNode > = [ ] ;
53
+ for ( const field of valueNode . fields ) {
54
+ if ( field . value . kind === Kind . VARIABLE ) {
56
55
const scopedVariableSource =
57
56
fragmentVariableValues ?. sources [ field . value . name . value ] ??
58
57
variableValues ?. sources [ field . value . name . value ] ;
@@ -61,11 +60,41 @@ export function replaceVariables(
61
60
scopedVariableSource ?. value === undefined &&
62
61
scopedVariableSource ?. signature . defaultValue === undefined
63
62
) {
64
- return false ;
63
+ continue ;
65
64
}
66
- return true ;
67
- } ) ,
68
- } ;
69
- } ,
70
- } ) as ConstValueNode ;
65
+ }
66
+ const newFieldNodeValue = replaceVariables (
67
+ field . value ,
68
+ variableValues ,
69
+ fragmentVariableValues ,
70
+ ) ;
71
+ newFields . push ( {
72
+ ...field ,
73
+ value : newFieldNodeValue ,
74
+ } ) ;
75
+ }
76
+ return {
77
+ ...valueNode ,
78
+ fields : newFields ,
79
+ } as ConstValueNode ;
80
+ }
81
+ case Kind . LIST : {
82
+ const newValues : Array < ValueNode > = [ ] ;
83
+ for ( const value of valueNode . values ) {
84
+ const newItemNodeValue = replaceVariables (
85
+ value ,
86
+ variableValues ,
87
+ fragmentVariableValues ,
88
+ ) ;
89
+ newValues . push ( newItemNodeValue ) ;
90
+ }
91
+ return {
92
+ ...valueNode ,
93
+ values : newValues ,
94
+ } as ConstValueNode ;
95
+ }
96
+ default : {
97
+ return valueNode ;
98
+ }
99
+ }
71
100
}
0 commit comments