@@ -165,61 +165,13 @@ export function OverlappingFieldsCanBeMerged(context: ValidationContext): any {
165
165
] ;
166
166
}
167
167
168
- const subfieldMap = getSubfieldMap ( ast1 , type1 , ast2 , type2 ) ;
168
+ const subfieldMap = getSubfieldMap ( context , ast1 , type1 , ast2 , type2 ) ;
169
169
if ( subfieldMap ) {
170
170
const conflicts = findConflicts ( fieldsAreMutuallyExclusive , subfieldMap ) ;
171
171
return subfieldConflicts ( conflicts , responseName , ast1 , ast2 ) ;
172
172
}
173
173
}
174
174
175
- function getSubfieldMap (
176
- ast1 : Field ,
177
- type1 : ?GraphQLOutputType ,
178
- ast2 : Field ,
179
- type2 : ?GraphQLOutputType
180
- ) : ?AstAndDefCollection {
181
- const selectionSet1 = ast1 . selectionSet ;
182
- const selectionSet2 = ast2 . selectionSet ;
183
- if ( selectionSet1 && selectionSet2 ) {
184
- const visitedFragmentNames = { } ;
185
- let subfieldMap = collectFieldASTsAndDefs (
186
- context ,
187
- getNamedType ( type1 ) ,
188
- selectionSet1 ,
189
- visitedFragmentNames
190
- ) ;
191
- subfieldMap = collectFieldASTsAndDefs (
192
- context ,
193
- getNamedType ( type2 ) ,
194
- selectionSet2 ,
195
- visitedFragmentNames ,
196
- subfieldMap
197
- ) ;
198
- return subfieldMap ;
199
- }
200
- }
201
-
202
- function subfieldConflicts (
203
- conflicts : Array < Conflict > ,
204
- responseName : string ,
205
- ast1 : Field ,
206
- ast2 : Field
207
- ) : ?Conflict {
208
- if ( conflicts . length > 0 ) {
209
- return [
210
- [ responseName , conflicts . map ( ( [ reason ] ) => reason ) ] ,
211
- conflicts . reduce (
212
- ( allFields , [ , fields1 ] ) => allFields . concat ( fields1 ) ,
213
- [ ast1 ]
214
- ) ,
215
- conflicts . reduce (
216
- ( allFields , [ , , fields2 ] ) => allFields . concat ( fields2 ) ,
217
- [ ast2 ]
218
- )
219
- ] ;
220
- }
221
- }
222
-
223
175
return {
224
176
SelectionSet : {
225
177
// Note: we validate on the reverse traversal so deeper conflicts will be
@@ -310,6 +262,37 @@ function doTypesConflict(
310
262
return false ;
311
263
}
312
264
265
+ /**
266
+ * Given two overlapping fields, produce the combined collection of subfields.
267
+ */
268
+ function getSubfieldMap (
269
+ context : ValidationContext ,
270
+ ast1 : Field ,
271
+ type1 : ?GraphQLOutputType ,
272
+ ast2 : Field ,
273
+ type2 : ?GraphQLOutputType
274
+ ) : ?AstAndDefCollection {
275
+ const selectionSet1 = ast1 . selectionSet ;
276
+ const selectionSet2 = ast2 . selectionSet ;
277
+ if ( selectionSet1 && selectionSet2 ) {
278
+ const visitedFragmentNames = { } ;
279
+ let subfieldMap = collectFieldASTsAndDefs (
280
+ context ,
281
+ getNamedType ( type1 ) ,
282
+ selectionSet1 ,
283
+ visitedFragmentNames
284
+ ) ;
285
+ subfieldMap = collectFieldASTsAndDefs (
286
+ context ,
287
+ getNamedType ( type2 ) ,
288
+ selectionSet2 ,
289
+ visitedFragmentNames ,
290
+ subfieldMap
291
+ ) ;
292
+ return subfieldMap ;
293
+ }
294
+ }
295
+
313
296
/**
314
297
* Given a selectionSet, adds all of the fields in that selection to
315
298
* the passed in map of fields, and returns it at the end.
@@ -382,6 +365,31 @@ function collectFieldASTsAndDefs(
382
365
return _astAndDefs ;
383
366
}
384
367
368
+ /**
369
+ * Given a series of Conflicts which occurred between two sub-fields, generate
370
+ * a single Conflict.
371
+ */
372
+ function subfieldConflicts (
373
+ conflicts : Array < Conflict > ,
374
+ responseName : string ,
375
+ ast1 : Field ,
376
+ ast2 : Field
377
+ ) : ?Conflict {
378
+ if ( conflicts . length > 0 ) {
379
+ return [
380
+ [ responseName , conflicts . map ( ( [ reason ] ) => reason ) ] ,
381
+ conflicts . reduce (
382
+ ( allFields , [ , fields1 ] ) => allFields . concat ( fields1 ) ,
383
+ [ ast1 ]
384
+ ) ,
385
+ conflicts . reduce (
386
+ ( allFields , [ , , fields2 ] ) => allFields . concat ( fields2 ) ,
387
+ [ ast2 ]
388
+ )
389
+ ] ;
390
+ }
391
+ }
392
+
385
393
/**
386
394
* A way to keep track of pairs of things when the ordering of the pair does
387
395
* not matter. We do this by maintaining a sort of double adjacency sets.
0 commit comments