@@ -17,7 +17,6 @@ import { print } from '../../language/printer';
1717import type {
1818 GraphQLNamedType ,
1919 GraphQLOutputType ,
20- GraphQLCompositeType ,
2120 GraphQLField ,
2221} from '../../type/definition' ;
2322import {
@@ -97,12 +96,14 @@ type ConflictReason = [string, ConflictReasonMessage];
9796type ConflictReasonMessage = string | Array < ConflictReason > ;
9897// Tuple defining a field node in a context.
9998type NodeAndDef = [
100- GraphQLCompositeType ,
99+ Maybe < GraphQLNamedType > ,
101100 FieldNode ,
102101 Maybe < GraphQLField < unknown , unknown > > ,
103102] ;
104103// Map of array of those.
105104type NodeAndDefCollection = ObjMap < Array < NodeAndDef > > ;
105+ type FragmentNames = Array < string > ;
106+ type FieldsAndFragmentNames = readonly [ NodeAndDefCollection , FragmentNames ] ;
106107
107108/**
108109 * Algorithm:
@@ -164,12 +165,12 @@ type NodeAndDefCollection = ObjMap<Array<NodeAndDef>>;
164165// GraphQL Document.
165166function findConflictsWithinSelectionSet (
166167 context : ValidationContext ,
167- cachedFieldsAndFragmentNames ,
168+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
168169 comparedFragmentPairs : PairSet ,
169170 parentType : Maybe < GraphQLNamedType > ,
170171 selectionSet : SelectionSetNode ,
171172) : Array < Conflict > {
172- const conflicts = [ ] ;
173+ const conflicts : Array < Conflict > = [ ] ;
173174
174175 const [ fieldMap , fragmentNames ] = getFieldsAndFragmentNames (
175176 context ,
@@ -226,7 +227,7 @@ function findConflictsWithinSelectionSet(
226227function collectConflictsBetweenFieldsAndFragment (
227228 context : ValidationContext ,
228229 conflicts : Array < Conflict > ,
229- cachedFieldsAndFragmentNames ,
230+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
230231 comparedFragmentPairs : PairSet ,
231232 areMutuallyExclusive : boolean ,
232233 fieldMap : NodeAndDefCollection ,
@@ -280,7 +281,7 @@ function collectConflictsBetweenFieldsAndFragment(
280281function collectConflictsBetweenFragments (
281282 context : ValidationContext ,
282283 conflicts : Array < Conflict > ,
283- cachedFieldsAndFragmentNames ,
284+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
284285 comparedFragmentPairs : PairSet ,
285286 areMutuallyExclusive : boolean ,
286287 fragmentName1 : string ,
@@ -366,15 +367,15 @@ function collectConflictsBetweenFragments(
366367// between the sub-fields of two overlapping fields.
367368function findConflictsBetweenSubSelectionSets (
368369 context : ValidationContext ,
369- cachedFieldsAndFragmentNames ,
370+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
370371 comparedFragmentPairs : PairSet ,
371372 areMutuallyExclusive : boolean ,
372373 parentType1 : Maybe < GraphQLNamedType > ,
373374 selectionSet1 : SelectionSetNode ,
374375 parentType2 : Maybe < GraphQLNamedType > ,
375376 selectionSet2 : SelectionSetNode ,
376377) : Array < Conflict > {
377- const conflicts = [ ] ;
378+ const conflicts : Array < Conflict > = [ ] ;
378379
379380 const [ fieldMap1 , fragmentNames1 ] = getFieldsAndFragmentNames (
380381 context ,
@@ -455,7 +456,7 @@ function findConflictsBetweenSubSelectionSets(
455456function collectConflictsWithin (
456457 context : ValidationContext ,
457458 conflicts : Array < Conflict > ,
458- cachedFieldsAndFragmentNames ,
459+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
459460 comparedFragmentPairs : PairSet ,
460461 fieldMap : NodeAndDefCollection ,
461462) : void {
@@ -496,7 +497,7 @@ function collectConflictsWithin(
496497function collectConflictsBetween (
497498 context : ValidationContext ,
498499 conflicts : Array < Conflict > ,
499- cachedFieldsAndFragmentNames ,
500+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
500501 comparedFragmentPairs : PairSet ,
501502 parentFieldsAreMutuallyExclusive : boolean ,
502503 fieldMap1 : NodeAndDefCollection ,
@@ -534,7 +535,7 @@ function collectConflictsBetween(
534535// comparing their sub-fields.
535536function findConflict (
536537 context : ValidationContext ,
537- cachedFieldsAndFragmentNames ,
538+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
538539 comparedFragmentPairs : PairSet ,
539540 parentFieldsAreMutuallyExclusive : boolean ,
540541 responseName : string ,
@@ -677,32 +678,33 @@ function doTypesConflict(
677678// referenced via fragment spreads.
678679function getFieldsAndFragmentNames (
679680 context : ValidationContext ,
680- cachedFieldsAndFragmentNames ,
681+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
681682 parentType : Maybe < GraphQLNamedType > ,
682683 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 ;
697688 }
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 ;
699701}
700702
701703// Given a reference to a fragment, return the represented collection of fields
702704// as well as a list of nested fragment names referenced via fragment spreads.
703705function getReferencedFieldsAndFragmentNames (
704706 context : ValidationContext ,
705- cachedFieldsAndFragmentNames ,
707+ cachedFieldsAndFragmentNames : Map < SelectionSetNode , FieldsAndFragmentNames > ,
706708 fragment : FragmentDefinitionNode ,
707709) {
708710 // Short-circuit building a type from the node if possible.
@@ -724,8 +726,8 @@ function _collectFieldsAndFragmentNames(
724726 context : ValidationContext ,
725727 parentType : Maybe < GraphQLNamedType > ,
726728 selectionSet : SelectionSetNode ,
727- nodeAndDefs ,
728- fragmentNames ,
729+ nodeAndDefs : NodeAndDefCollection ,
730+ fragmentNames : ObjMap < boolean > ,
729731) : void {
730732 for ( const selection of selectionSet . selections ) {
731733 switch ( selection . kind ) {
0 commit comments