From c3aacf8ad89771ebc1c1abcbb5b3d12c29210bb4 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Fri, 26 Jul 2024 08:10:36 +0300 Subject: [PATCH] extract to separate file --- src/execution/DeferredFragmentFactory.ts | 65 ++++++++++++++++++++++ src/execution/IncrementalGraph.ts | 69 ++---------------------- 2 files changed, 68 insertions(+), 66 deletions(-) create mode 100644 src/execution/DeferredFragmentFactory.ts diff --git a/src/execution/DeferredFragmentFactory.ts b/src/execution/DeferredFragmentFactory.ts new file mode 100644 index 0000000000..77fb75b553 --- /dev/null +++ b/src/execution/DeferredFragmentFactory.ts @@ -0,0 +1,65 @@ +import type { Path } from '../jsutils/Path.js'; + +import type { DeferUsage } from './collectFields.js'; +import { DeferredFragmentRecord } from './types.js'; + +/** + * @internal + */ +export class DeferredFragmentFactory { + private _rootDeferredFragments = new Map< + DeferUsage, + DeferredFragmentRecord + >(); + + get(deferUsage: DeferUsage, path: Path | undefined): DeferredFragmentRecord { + const deferUsagePath = this._pathAtDepth(path, deferUsage.depth); + let deferredFragmentRecords: + | Map + | undefined; + if (deferUsagePath === undefined) { + deferredFragmentRecords = this._rootDeferredFragments; + } else { + deferredFragmentRecords = ( + deferUsagePath as unknown as { + deferredFragmentRecords: Map; + } + ).deferredFragmentRecords; + if (deferredFragmentRecords === undefined) { + deferredFragmentRecords = new Map(); + ( + deferUsagePath as unknown as { + deferredFragmentRecords: Map; + } + ).deferredFragmentRecords = deferredFragmentRecords; + } + } + let deferredFragmentRecord = deferredFragmentRecords.get(deferUsage); + if (deferredFragmentRecord === undefined) { + const { label, parentDeferUsage } = deferUsage; + deferredFragmentRecord = new DeferredFragmentRecord( + deferUsagePath, + label, + parentDeferUsage, + ); + deferredFragmentRecords.set(deferUsage, deferredFragmentRecord); + } + return deferredFragmentRecord; + } + + private _pathAtDepth( + path: Path | undefined, + depth: number, + ): Path | undefined { + if (depth === 0) { + return; + } + const stack: Array = []; + let currentPath = path; + while (currentPath !== undefined) { + stack.unshift(currentPath); + currentPath = currentPath.prev; + } + return stack[depth - 1]; + } +} diff --git a/src/execution/IncrementalGraph.ts b/src/execution/IncrementalGraph.ts index 83ce78089b..f11a7794bb 100644 --- a/src/execution/IncrementalGraph.ts +++ b/src/execution/IncrementalGraph.ts @@ -7,7 +7,9 @@ import { promiseWithResolvers } from '../jsutils/promiseWithResolvers.js'; import type { GraphQLError } from '../error/GraphQLError.js'; import type { DeferUsage } from './collectFields.js'; +import { DeferredFragmentFactory } from './DeferredFragmentFactory.js'; import type { + DeferredFragmentRecord, DeliveryGroup, IncrementalDataRecord, IncrementalDataRecordResult, @@ -16,72 +18,7 @@ import type { StreamRecord, SuccessfulExecutionGroup, } from './types.js'; -import { - DeferredFragmentRecord, - isDeferredFragmentRecord, - isPendingExecutionGroup, -} from './types.js'; - -/** - * @internal - */ -class DeferredFragmentFactory { - private _rootDeferredFragments = new Map< - DeferUsage, - DeferredFragmentRecord - >(); - - get(deferUsage: DeferUsage, path: Path | undefined): DeferredFragmentRecord { - const deferUsagePath = this._pathAtDepth(path, deferUsage.depth); - let deferredFragmentRecords: - | Map - | undefined; - if (deferUsagePath === undefined) { - deferredFragmentRecords = this._rootDeferredFragments; - } else { - deferredFragmentRecords = ( - deferUsagePath as unknown as { - deferredFragmentRecords: Map; - } - ).deferredFragmentRecords; - if (deferredFragmentRecords === undefined) { - deferredFragmentRecords = new Map(); - ( - deferUsagePath as unknown as { - deferredFragmentRecords: Map; - } - ).deferredFragmentRecords = deferredFragmentRecords; - } - } - let deferredFragmentRecord = deferredFragmentRecords.get(deferUsage); - if (deferredFragmentRecord === undefined) { - const { label, parentDeferUsage } = deferUsage; - deferredFragmentRecord = new DeferredFragmentRecord( - deferUsagePath, - label, - parentDeferUsage, - ); - deferredFragmentRecords.set(deferUsage, deferredFragmentRecord); - } - return deferredFragmentRecord; - } - - private _pathAtDepth( - path: Path | undefined, - depth: number, - ): Path | undefined { - if (depth === 0) { - return; - } - const stack: Array = []; - let currentPath = path; - while (currentPath !== undefined) { - stack.unshift(currentPath); - currentPath = currentPath.prev; - } - return stack[depth - 1]; - } -} +import { isDeferredFragmentRecord, isPendingExecutionGroup } from './types.js'; /** * @internal