From 464af2be6c7c47cdf781cbb85f845fc5379d1501 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Tue, 19 Dec 2023 12:22:56 +0200 Subject: [PATCH] create new deferred grouped field set records lazily --- src/execution/execute.ts | 80 ++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/src/execution/execute.ts b/src/execution/execute.ts index baf6b41ea1..2d28124b6c 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -426,13 +426,6 @@ function executeOperation( const path = undefined; - const newDeferredGroupedFieldSetRecords = addNewDeferredGroupedFieldSets( - incrementalPublisher, - newGroupedFieldSetDetailsMap, - newDeferMap, - path, - ); - let result; switch (operation.operation) { case OperationTypeNode.QUERY: @@ -476,7 +469,7 @@ function executeOperation( rootType, rootValue, path, - newDeferredGroupedFieldSetRecords, + newGroupedFieldSetDetailsMap, newDeferMap, ); @@ -1501,36 +1494,28 @@ function deferredFragmentRecordFromDeferUsage( return deferMap.get(deferUsage)!; } -function addNewDeferredGroupedFieldSets( +function addNewDeferredGroupedFieldSet( incrementalPublisher: IncrementalPublisher, - newGroupedFieldSetDetailsMap: Map, + deferUsageSet: DeferUsageSet, + groupedFieldSet: GroupedFieldSet, + shouldInitiateDefer: boolean, deferMap: ReadonlyMap, path?: Path | undefined, -): ReadonlyArray { - const newDeferredGroupedFieldSetRecords: Array = - []; - - for (const [ +): DeferredGroupedFieldSetRecord { + const deferredFragmentRecords = getDeferredFragmentRecords( deferUsageSet, - { groupedFieldSet, shouldInitiateDefer }, - ] of newGroupedFieldSetDetailsMap) { - const deferredFragmentRecords = getDeferredFragmentRecords( - deferUsageSet, - deferMap, - ); - const deferredGroupedFieldSetRecord = new DeferredGroupedFieldSetRecord({ - path, - deferredFragmentRecords, - groupedFieldSet, - shouldInitiateDefer, - }); - incrementalPublisher.reportNewDeferredGroupedFieldSetRecord( - deferredGroupedFieldSetRecord, - ); - newDeferredGroupedFieldSetRecords.push(deferredGroupedFieldSetRecord); - } - - return newDeferredGroupedFieldSetRecords; + deferMap, + ); + const deferredGroupedFieldSetRecord = new DeferredGroupedFieldSetRecord({ + path, + deferredFragmentRecords, + groupedFieldSet, + shouldInitiateDefer, + }); + incrementalPublisher.reportNewDeferredGroupedFieldSetRecord( + deferredGroupedFieldSetRecord, + ); + return deferredGroupedFieldSetRecord; } function getDeferredFragmentRecords( @@ -1565,13 +1550,6 @@ function collectAndExecuteSubfields( path, ); - const newDeferredGroupedFieldSetRecords = addNewDeferredGroupedFieldSets( - incrementalPublisher, - newGroupedFieldSetDetailsMap, - newDeferMap, - path, - ); - const subFields = executeFields( exeContext, returnType, @@ -1587,7 +1565,7 @@ function collectAndExecuteSubfields( returnType, result, path, - newDeferredGroupedFieldSetRecords, + newGroupedFieldSetDetailsMap, newDeferMap, ); @@ -1891,11 +1869,23 @@ function executeDeferredGroupedFieldSets( parentType: GraphQLObjectType, sourceValue: unknown, path: Path | undefined, - newDeferredGroupedFieldSetRecords: ReadonlyArray, + newGroupedFieldSetDetailsMap: Map, deferMap: ReadonlyMap, ): void { - for (const deferredGroupedFieldSetRecord of newDeferredGroupedFieldSetRecords) { - if (deferredGroupedFieldSetRecord.shouldInitiateDefer) { + for (const [ + deferUsageSet, + { groupedFieldSet, shouldInitiateDefer }, + ] of newGroupedFieldSetDetailsMap) { + const deferredGroupedFieldSetRecord = addNewDeferredGroupedFieldSet( + exeContext.incrementalPublisher, + deferUsageSet, + groupedFieldSet, + shouldInitiateDefer, + deferMap, + path, + ); + + if (shouldInitiateDefer) { // eslint-disable-next-line @typescript-eslint/no-floating-promises Promise.resolve().then(() => executeDeferredGroupedFieldSet(