11import { AccumulatorMap } from '../jsutils/AccumulatorMap.js' ;
22import { invariant } from '../jsutils/invariant.js' ;
3+ import { mapValue } from '../jsutils/mapValue.js' ;
34import type { ObjMap } from '../jsutils/ObjMap.js' ;
45
56import type {
@@ -25,22 +26,18 @@ import type { GraphQLSchema } from '../type/schema.js';
2526import { typeFromAST } from '../utilities/typeFromAST.js' ;
2627
2728import type { GraphQLVariableSignature } from './getVariableSignature.js' ;
29+ import type { VariableValues } from './values.js' ;
2830import { experimentalGetArgumentValues , getDirectiveValues } from './values.js' ;
2931
3032export interface DeferUsage {
3133 label : string | undefined ;
3234 parentDeferUsage : DeferUsage | undefined ;
3335}
3436
35- export interface FragmentVariables {
36- signatures : ObjMap < GraphQLVariableSignature > ;
37- values : ObjMap < unknown > ;
38- }
39-
4037export interface FieldDetails {
4138 node : FieldNode ;
4239 deferUsage ?: DeferUsage | undefined ;
43- fragmentVariables ?: FragmentVariables | undefined ;
40+ fragmentVariableValues ?: VariableValues | undefined ;
4441}
4542
4643export type FieldGroup = ReadonlyArray < FieldDetails > ;
@@ -55,7 +52,7 @@ export interface FragmentDetails {
5552interface CollectFieldsContext {
5653 schema : GraphQLSchema ;
5754 fragments : ObjMap < FragmentDetails > ;
58- variableValues : { [ variable : string ] : unknown } ;
55+ variableValues : VariableValues ;
5956 operation : OperationDefinitionNode ;
6057 runtimeType : GraphQLObjectType ;
6158 visitedFragmentNames : Set < string > ;
@@ -73,7 +70,7 @@ interface CollectFieldsContext {
7370export function collectFields (
7471 schema : GraphQLSchema ,
7572 fragments : ObjMap < FragmentDetails > ,
76- variableValues : { [ variable : string ] : unknown } ,
73+ variableValues : VariableValues ,
7774 runtimeType : GraphQLObjectType ,
7875 operation : OperationDefinitionNode ,
7976) : {
@@ -114,7 +111,7 @@ export function collectFields(
114111export function collectSubfields (
115112 schema : GraphQLSchema ,
116113 fragments : ObjMap < FragmentDetails > ,
117- variableValues : { [ variable : string ] : unknown } ,
114+ variableValues : VariableValues ,
118115 operation : OperationDefinitionNode ,
119116 returnType : GraphQLObjectType ,
120117 fieldGroup : FieldGroup ,
@@ -136,14 +133,14 @@ export function collectSubfields(
136133 for ( const fieldDetail of fieldGroup ) {
137134 const selectionSet = fieldDetail . node . selectionSet ;
138135 if ( selectionSet ) {
139- const { deferUsage, fragmentVariables } = fieldDetail ;
136+ const { deferUsage, fragmentVariableValues } = fieldDetail ;
140137 collectFieldsImpl (
141138 context ,
142139 selectionSet ,
143140 subGroupedFieldSet ,
144141 newDeferUsages ,
145142 deferUsage ,
146- fragmentVariables ,
143+ fragmentVariableValues ,
147144 ) ;
148145 }
149146 }
@@ -161,7 +158,7 @@ function collectFieldsImpl(
161158 groupedFieldSet : AccumulatorMap < string , FieldDetails > ,
162159 newDeferUsages : Array < DeferUsage > ,
163160 deferUsage ?: DeferUsage ,
164- fragmentVariables ?: FragmentVariables ,
161+ fragmentVariableValues ?: VariableValues ,
165162) : void {
166163 const {
167164 schema,
@@ -175,19 +172,25 @@ function collectFieldsImpl(
175172 for ( const selection of selectionSet . selections ) {
176173 switch ( selection . kind ) {
177174 case Kind . FIELD : {
178- if ( ! shouldIncludeNode ( selection , variableValues , fragmentVariables ) ) {
175+ if (
176+ ! shouldIncludeNode ( selection , variableValues , fragmentVariableValues )
177+ ) {
179178 continue ;
180179 }
181180 groupedFieldSet . add ( getFieldEntryKey ( selection ) , {
182181 node : selection ,
183182 deferUsage,
184- fragmentVariables ,
183+ fragmentVariableValues ,
185184 } ) ;
186185 break ;
187186 }
188187 case Kind . INLINE_FRAGMENT : {
189188 if (
190- ! shouldIncludeNode ( selection , variableValues , fragmentVariables ) ||
189+ ! shouldIncludeNode (
190+ selection ,
191+ variableValues ,
192+ fragmentVariableValues ,
193+ ) ||
191194 ! doesFragmentConditionMatch ( schema , selection , runtimeType )
192195 ) {
193196 continue ;
@@ -196,7 +199,7 @@ function collectFieldsImpl(
196199 const newDeferUsage = getDeferUsage (
197200 operation ,
198201 variableValues ,
199- fragmentVariables ,
202+ fragmentVariableValues ,
200203 selection ,
201204 deferUsage ,
202205 ) ;
@@ -208,7 +211,7 @@ function collectFieldsImpl(
208211 groupedFieldSet ,
209212 newDeferUsages ,
210213 deferUsage ,
211- fragmentVariables ,
214+ fragmentVariableValues ,
212215 ) ;
213216 } else {
214217 newDeferUsages . push ( newDeferUsage ) ;
@@ -218,7 +221,7 @@ function collectFieldsImpl(
218221 groupedFieldSet ,
219222 newDeferUsages ,
220223 newDeferUsage ,
221- fragmentVariables ,
224+ fragmentVariableValues ,
222225 ) ;
223226 }
224227
@@ -230,15 +233,19 @@ function collectFieldsImpl(
230233 const newDeferUsage = getDeferUsage (
231234 operation ,
232235 variableValues ,
233- fragmentVariables ,
236+ fragmentVariableValues ,
234237 selection ,
235238 deferUsage ,
236239 ) ;
237240
238241 if (
239242 ! newDeferUsage &&
240243 ( visitedFragmentNames . has ( fragName ) ||
241- ! shouldIncludeNode ( selection , variableValues , fragmentVariables ) )
244+ ! shouldIncludeNode (
245+ selection ,
246+ variableValues ,
247+ fragmentVariableValues ,
248+ ) )
242249 ) {
243250 continue ;
244251 }
@@ -252,15 +259,20 @@ function collectFieldsImpl(
252259 }
253260
254261 const fragmentVariableSignatures = fragment . variableSignatures ;
255- let newFragmentVariables : FragmentVariables | undefined ;
262+ let newFragmentVariableValues : VariableValues | undefined ;
256263 if ( fragmentVariableSignatures ) {
257- newFragmentVariables = {
258- signatures : fragmentVariableSignatures ,
259- values : experimentalGetArgumentValues (
264+ newFragmentVariableValues = {
265+ sources : mapValue ( fragmentVariableSignatures , ( varSignature ) => ( {
266+ signature : varSignature ,
267+ value : fragmentVariableValues ?. sources [ varSignature . name ]
268+ ? fragmentVariableValues . coerced [ varSignature . name ]
269+ : variableValues . coerced [ varSignature . name ] ,
270+ } ) ) ,
271+ coerced : experimentalGetArgumentValues (
260272 selection ,
261273 Object . values ( fragmentVariableSignatures ) ,
262274 variableValues ,
263- fragmentVariables ,
275+ fragmentVariableValues ,
264276 ) ,
265277 } ;
266278 }
@@ -273,7 +285,7 @@ function collectFieldsImpl(
273285 groupedFieldSet ,
274286 newDeferUsages ,
275287 deferUsage ,
276- newFragmentVariables ,
288+ newFragmentVariableValues ,
277289 ) ;
278290 } else {
279291 newDeferUsages . push ( newDeferUsage ) ;
@@ -283,7 +295,7 @@ function collectFieldsImpl(
283295 groupedFieldSet ,
284296 newDeferUsages ,
285297 newDeferUsage ,
286- newFragmentVariables ,
298+ newFragmentVariableValues ,
287299 ) ;
288300 }
289301 break ;
@@ -299,16 +311,16 @@ function collectFieldsImpl(
299311 */
300312function getDeferUsage (
301313 operation : OperationDefinitionNode ,
302- variableValues : { [ variable : string ] : unknown } ,
303- fragmentVariables : FragmentVariables | undefined ,
314+ variableValues : VariableValues ,
315+ fragmentVariableValues : VariableValues | undefined ,
304316 node : FragmentSpreadNode | InlineFragmentNode ,
305317 parentDeferUsage : DeferUsage | undefined ,
306318) : DeferUsage | undefined {
307319 const defer = getDirectiveValues (
308320 GraphQLDeferDirective ,
309321 node ,
310322 variableValues ,
311- fragmentVariables ,
323+ fragmentVariableValues ,
312324 ) ;
313325
314326 if ( ! defer ) {
@@ -336,14 +348,14 @@ function getDeferUsage(
336348 */
337349function shouldIncludeNode (
338350 node : FragmentSpreadNode | FieldNode | InlineFragmentNode ,
339- variableValues : { [ variable : string ] : unknown } ,
340- fragmentVariables : FragmentVariables | undefined ,
351+ variableValues : VariableValues ,
352+ fragmentVariableValues : VariableValues | undefined ,
341353) : boolean {
342354 const skip = getDirectiveValues (
343355 GraphQLSkipDirective ,
344356 node ,
345357 variableValues ,
346- fragmentVariables ,
358+ fragmentVariableValues ,
347359 ) ;
348360 if ( skip ?. if === true ) {
349361 return false ;
@@ -353,7 +365,7 @@ function shouldIncludeNode(
353365 GraphQLIncludeDirective ,
354366 node ,
355367 variableValues ,
356- fragmentVariables ,
368+ fragmentVariableValues ,
357369 ) ;
358370 if ( include ?. if === false ) {
359371 return false ;
0 commit comments