@@ -12,6 +12,7 @@ import { GraphQLError, locatedError } from '../error';
1212import invariant from '../jsutils/invariant' ;
1313import isInvalid from '../jsutils/isInvalid' ;
1414import isNullish from '../jsutils/isNullish' ;
15+ import memoize3 from '../jsutils/memoize3' ;
1516import type { ObjMap } from '../jsutils/ObjMap' ;
1617import type { MaybePromise } from '../jsutils/MaybePromise' ;
1718
@@ -1245,25 +1246,12 @@ function collectAndExecuteSubfields(
12451246 * type. Memoizing ensures the subfields are not repeatedly calculated, which
12461247 * saves overhead when resolving lists of values.
12471248 */
1248- const subfieldCache : WeakMap <
1249- $ReadOnlyArray < FieldNode > ,
1250- WeakMap < GraphQLObjectType , ObjMap < Array < FieldNode >>> ,
1251- > = new WeakMap ( ) ;
1252- function collectSubfields (
1249+ const collectSubfields = memoize3 ( _collectSubfields ) ;
1250+ function _collectSubfields (
12531251 exeContext : ExecutionContext ,
12541252 returnType : GraphQLObjectType ,
12551253 fieldNodes : $ReadOnlyArray < FieldNode > ,
12561254) : ObjMap < Array < FieldNode >> {
1257- let cacheByReturnType = subfieldCache . get ( fieldNodes ) ;
1258- if ( cacheByReturnType ) {
1259- const cachedSubFieldNodes = cacheByReturnType . get ( returnType ) ;
1260- if ( cachedSubFieldNodes ) {
1261- return cachedSubFieldNodes ;
1262- }
1263- } else {
1264- cacheByReturnType = new WeakMap ( ) ;
1265- subfieldCache . set ( fieldNodes , cacheByReturnType ) ;
1266- }
12671255 let subFieldNodes = Object . create ( null ) ;
12681256 const visitedFragmentNames = Object . create ( null ) ;
12691257 for ( let i = 0 ; i < fieldNodes . length ; i ++ ) {
@@ -1278,7 +1266,6 @@ function collectSubfields(
12781266 ) ;
12791267 }
12801268 }
1281- cacheByReturnType . set ( returnType , subFieldNodes ) ;
12821269 return subFieldNodes ;
12831270}
12841271
0 commit comments