@@ -17,7 +17,6 @@ import { print } from '../../language/printer';
17
17
import type {
18
18
GraphQLNamedType ,
19
19
GraphQLOutputType ,
20
- GraphQLCompositeType ,
21
20
GraphQLField ,
22
21
} from '../../type/definition' ;
23
22
import {
@@ -97,12 +96,14 @@ type ConflictReason = [string, ConflictReasonMessage];
97
96
type ConflictReasonMessage = string | Array < ConflictReason > ;
98
97
// Tuple defining a field node in a context.
99
98
type NodeAndDef = [
100
- GraphQLCompositeType ,
99
+ Maybe < GraphQLNamedType > ,
101
100
FieldNode ,
102
101
Maybe < GraphQLField < unknown , unknown > > ,
103
102
] ;
104
103
// Map of array of those.
105
104
type NodeAndDefCollection = ObjMap < Array < NodeAndDef > > ;
105
+ type FragmentNames = Array < string > ;
106
+ type FieldsAndFragmentNames = readonly [ NodeAndDefCollection , FragmentNames ] ;
106
107
107
108
/**
108
109
* Algorithm:
@@ -164,12 +165,12 @@ type NodeAndDefCollection = ObjMap<Array<NodeAndDef>>;
164
165
// GraphQL Document.
165
166
function findConflictsWithinSelectionSet (
166
167
context : ValidationContext ,
167
- cachedFieldsAndFragmentNames ,
168
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
168
169
comparedFragmentPairs : PairSet ,
169
170
parentType : Maybe < GraphQLNamedType > ,
170
171
selectionSet : SelectionSetNode ,
171
172
) : Array < Conflict > {
172
- const conflicts = [ ] ;
173
+ const conflicts : Array < Conflict > = [ ] ;
173
174
174
175
const [ fieldMap , fragmentNames ] = getFieldsAndFragmentNames (
175
176
context ,
@@ -226,7 +227,7 @@ function findConflictsWithinSelectionSet(
226
227
function collectConflictsBetweenFieldsAndFragment (
227
228
context : ValidationContext ,
228
229
conflicts : Array < Conflict > ,
229
- cachedFieldsAndFragmentNames ,
230
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
230
231
comparedFragmentPairs : PairSet ,
231
232
areMutuallyExclusive : boolean ,
232
233
fieldMap : NodeAndDefCollection ,
@@ -280,7 +281,7 @@ function collectConflictsBetweenFieldsAndFragment(
280
281
function collectConflictsBetweenFragments (
281
282
context : ValidationContext ,
282
283
conflicts : Array < Conflict > ,
283
- cachedFieldsAndFragmentNames ,
284
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
284
285
comparedFragmentPairs : PairSet ,
285
286
areMutuallyExclusive : boolean ,
286
287
fragmentName1 : string ,
@@ -366,15 +367,15 @@ function collectConflictsBetweenFragments(
366
367
// between the sub-fields of two overlapping fields.
367
368
function findConflictsBetweenSubSelectionSets (
368
369
context : ValidationContext ,
369
- cachedFieldsAndFragmentNames ,
370
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
370
371
comparedFragmentPairs : PairSet ,
371
372
areMutuallyExclusive : boolean ,
372
373
parentType1 : Maybe < GraphQLNamedType > ,
373
374
selectionSet1 : SelectionSetNode ,
374
375
parentType2 : Maybe < GraphQLNamedType > ,
375
376
selectionSet2 : SelectionSetNode ,
376
377
) : Array < Conflict > {
377
- const conflicts = [ ] ;
378
+ const conflicts : Array < Conflict > = [ ] ;
378
379
379
380
const [ fieldMap1 , fragmentNames1 ] = getFieldsAndFragmentNames (
380
381
context ,
@@ -455,7 +456,7 @@ function findConflictsBetweenSubSelectionSets(
455
456
function collectConflictsWithin (
456
457
context : ValidationContext ,
457
458
conflicts : Array < Conflict > ,
458
- cachedFieldsAndFragmentNames ,
459
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
459
460
comparedFragmentPairs : PairSet ,
460
461
fieldMap : NodeAndDefCollection ,
461
462
) : void {
@@ -496,7 +497,7 @@ function collectConflictsWithin(
496
497
function collectConflictsBetween (
497
498
context : ValidationContext ,
498
499
conflicts : Array < Conflict > ,
499
- cachedFieldsAndFragmentNames ,
500
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
500
501
comparedFragmentPairs : PairSet ,
501
502
parentFieldsAreMutuallyExclusive : boolean ,
502
503
fieldMap1 : NodeAndDefCollection ,
@@ -534,7 +535,7 @@ function collectConflictsBetween(
534
535
// comparing their sub-fields.
535
536
function findConflict (
536
537
context : ValidationContext ,
537
- cachedFieldsAndFragmentNames ,
538
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
538
539
comparedFragmentPairs : PairSet ,
539
540
parentFieldsAreMutuallyExclusive : boolean ,
540
541
responseName : string ,
@@ -677,32 +678,33 @@ function doTypesConflict(
677
678
// referenced via fragment spreads.
678
679
function getFieldsAndFragmentNames (
679
680
context : ValidationContext ,
680
- cachedFieldsAndFragmentNames ,
681
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
681
682
parentType : Maybe < GraphQLNamedType > ,
682
683
selectionSet : SelectionSetNode ,
683
- ) : [ NodeAndDefCollection , Array < string > ] {
684
- let cached = cachedFieldsAndFragmentNames . get ( selectionSet ) ;
685
- if ( ! cached ) {
686
- const nodeAndDefs = Object . create ( null ) ;
687
- const fragmentNames = Object . create ( null ) ;
688
- _collectFieldsAndFragmentNames (
689
- context ,
690
- parentType ,
691
- selectionSet ,
692
- nodeAndDefs ,
693
- fragmentNames ,
694
- ) ;
695
- cached = [ nodeAndDefs , Object . keys ( fragmentNames ) ] ;
696
- cachedFieldsAndFragmentNames . set ( selectionSet , cached ) ;
684
+ ) : FieldsAndFragmentNames {
685
+ const cached = cachedFieldsAndFragmentNames . get ( selectionSet ) ;
686
+ if ( cached ) {
687
+ return cached ;
697
688
}
698
- return cached ;
689
+ const nodeAndDefs : NodeAndDefCollection = Object . create ( null ) ;
690
+ const fragmentNames : ObjMap < boolean > = Object . create ( null ) ;
691
+ _collectFieldsAndFragmentNames (
692
+ context ,
693
+ parentType ,
694
+ selectionSet ,
695
+ nodeAndDefs ,
696
+ fragmentNames ,
697
+ ) ;
698
+ const result = [ nodeAndDefs , Object . keys ( fragmentNames ) ] as const ;
699
+ cachedFieldsAndFragmentNames . set ( selectionSet , result ) ;
700
+ return result ;
699
701
}
700
702
701
703
// Given a reference to a fragment, return the represented collection of fields
702
704
// as well as a list of nested fragment names referenced via fragment spreads.
703
705
function getReferencedFieldsAndFragmentNames (
704
706
context : ValidationContext ,
705
- cachedFieldsAndFragmentNames ,
707
+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
706
708
fragment : FragmentDefinitionNode ,
707
709
) {
708
710
// Short-circuit building a type from the node if possible.
@@ -724,8 +726,8 @@ function _collectFieldsAndFragmentNames(
724
726
context : ValidationContext ,
725
727
parentType : Maybe < GraphQLNamedType > ,
726
728
selectionSet : SelectionSetNode ,
727
- nodeAndDefs ,
728
- fragmentNames ,
729
+ nodeAndDefs : NodeAndDefCollection ,
730
+ fragmentNames : ObjMap < boolean > ,
729
731
) : void {
730
732
for ( const selection of selectionSet . selections ) {
731
733
switch ( selection . kind ) {
0 commit comments