@@ -12,6 +12,7 @@ import { GraphQLError, locatedError } from '../error';
12
12
import invariant from '../jsutils/invariant' ;
13
13
import isInvalid from '../jsutils/isInvalid' ;
14
14
import isNullish from '../jsutils/isNullish' ;
15
+ import memoize3 from '../jsutils/memoize3' ;
15
16
import type { ObjMap } from '../jsutils/ObjMap' ;
16
17
import type { MaybePromise } from '../jsutils/MaybePromise' ;
17
18
@@ -1245,25 +1246,12 @@ function collectAndExecuteSubfields(
1245
1246
* type. Memoizing ensures the subfields are not repeatedly calculated, which
1246
1247
* saves overhead when resolving lists of values.
1247
1248
*/
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 (
1253
1251
exeContext : ExecutionContext ,
1254
1252
returnType : GraphQLObjectType ,
1255
1253
fieldNodes : $ReadOnlyArray < FieldNode > ,
1256
1254
) : 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
- }
1267
1255
let subFieldNodes = Object . create ( null ) ;
1268
1256
const visitedFragmentNames = Object . create ( null ) ;
1269
1257
for ( let i = 0 ; i < fieldNodes . length ; i ++ ) {
@@ -1278,7 +1266,6 @@ function collectSubfields(
1278
1266
) ;
1279
1267
}
1280
1268
}
1281
- cacheByReturnType . set ( returnType , subFieldNodes ) ;
1282
1269
return subFieldNodes ;
1283
1270
}
1284
1271
0 commit comments