|
| 1 | +import type { ObjMap } from '../../jsutils/ObjMap.js'; |
| 2 | +import { addPath, pathToArray } from '../../jsutils/Path.js'; |
| 3 | + |
| 4 | +import type { GraphQLError } from '../../error/GraphQLError.js'; |
| 5 | + |
| 6 | +import type { |
| 7 | + DeliveryGroup, |
| 8 | + ExecutionGroupValue, |
| 9 | + IncrementalWork, |
| 10 | + ItemStream, |
| 11 | + StreamItemValue, |
| 12 | +} from '../incremental/IncrementalExecutor.js'; |
| 13 | +import type { WorkQueueEvent } from '../incremental/WorkQueue.js'; |
| 14 | +import { createWorkQueue } from '../incremental/WorkQueue.js'; |
| 15 | +import { mapAsyncIterable } from '../mapAsyncIterable.js'; |
| 16 | +import { withConcurrentAbruptClose } from '../withConcurrentAbruptClose.js'; |
| 17 | + |
| 18 | +import type { |
| 19 | + ExperimentalIncrementalExecutionResults, |
| 20 | + IncrementalDeferResult, |
| 21 | + IncrementalResult, |
| 22 | + IncrementalStreamResult, |
| 23 | + InitialIncrementalExecutionResult, |
| 24 | + SubsequentIncrementalExecutionResult, |
| 25 | +} from './BranchingIncrementalExecutor.js'; |
| 26 | + |
| 27 | +interface SubsequentIncrementalExecutionResultContext { |
| 28 | + incremental: Array<IncrementalResult>; |
| 29 | + hasNext: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * @internal |
| 34 | + */ |
| 35 | +export class BranchingIncrementalPublisher { |
| 36 | + private _indices: Map<ItemStream, number>; |
| 37 | + |
| 38 | + constructor() { |
| 39 | + this._indices = new Map(); |
| 40 | + } |
| 41 | + |
| 42 | + buildResponse( |
| 43 | + data: ObjMap<unknown>, |
| 44 | + errors: ReadonlyArray<GraphQLError>, |
| 45 | + work: IncrementalWork, |
| 46 | + abortSignal: AbortSignal | undefined, |
| 47 | + ): ExperimentalIncrementalExecutionResults { |
| 48 | + const { initialStreams, events } = createWorkQueue< |
| 49 | + ExecutionGroupValue, |
| 50 | + StreamItemValue, |
| 51 | + DeliveryGroup, |
| 52 | + ItemStream |
| 53 | + >(work); |
| 54 | + |
| 55 | + for (const stream of initialStreams) { |
| 56 | + this._indices.set(stream, stream.initialCount); |
| 57 | + } |
| 58 | + |
| 59 | + function abort(): void { |
| 60 | + subsequentResults.throw(abortSignal?.reason).catch(() => { |
| 61 | + // Ignore errors |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + let onWorkQueueFinished: (() => void) | undefined; |
| 66 | + if (abortSignal) { |
| 67 | + abortSignal.addEventListener('abort', abort); |
| 68 | + onWorkQueueFinished = () => |
| 69 | + abortSignal.removeEventListener('abort', abort); |
| 70 | + } |
| 71 | + |
| 72 | + const initialResult: InitialIncrementalExecutionResult = errors.length |
| 73 | + ? { errors, data, hasNext: true } |
| 74 | + : { data, hasNext: true }; |
| 75 | + |
| 76 | + const subsequentResults = withConcurrentAbruptClose( |
| 77 | + mapAsyncIterable(events, (batch) => |
| 78 | + this._handleBatch(batch, onWorkQueueFinished), |
| 79 | + ), |
| 80 | + () => onWorkQueueFinished?.(), |
| 81 | + ); |
| 82 | + |
| 83 | + return { |
| 84 | + initialResult, |
| 85 | + subsequentResults, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + private _handleBatch( |
| 90 | + batch: ReadonlyArray< |
| 91 | + WorkQueueEvent< |
| 92 | + ExecutionGroupValue, |
| 93 | + StreamItemValue, |
| 94 | + DeliveryGroup, |
| 95 | + ItemStream |
| 96 | + > |
| 97 | + >, |
| 98 | + onWorkQueueFinished: (() => void) | undefined, |
| 99 | + ): SubsequentIncrementalExecutionResult { |
| 100 | + const context: SubsequentIncrementalExecutionResultContext = { |
| 101 | + incremental: [], |
| 102 | + hasNext: true, |
| 103 | + }; |
| 104 | + |
| 105 | + for (const event of batch) { |
| 106 | + this._handleWorkQueueEvent(event, context, onWorkQueueFinished); |
| 107 | + } |
| 108 | + |
| 109 | + const { incremental, hasNext } = context; |
| 110 | + |
| 111 | + const result: SubsequentIncrementalExecutionResult = { hasNext }; |
| 112 | + if (incremental.length > 0) { |
| 113 | + result.incremental = incremental; |
| 114 | + } |
| 115 | + |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + private _handleWorkQueueEvent( |
| 120 | + event: WorkQueueEvent< |
| 121 | + ExecutionGroupValue, |
| 122 | + StreamItemValue, |
| 123 | + DeliveryGroup, |
| 124 | + ItemStream |
| 125 | + >, |
| 126 | + context: SubsequentIncrementalExecutionResultContext, |
| 127 | + onWorkQueueFinished: (() => void) | undefined, |
| 128 | + ): void { |
| 129 | + switch (event.kind) { |
| 130 | + case 'GROUP_VALUES': { |
| 131 | + const group = event.group; |
| 132 | + for (const value of event.values) { |
| 133 | + context.incremental.push( |
| 134 | + buildIncrementalResult( |
| 135 | + { |
| 136 | + data: value.data, |
| 137 | + path: pathToArray(group.path), |
| 138 | + }, |
| 139 | + group.label, |
| 140 | + value.errors, |
| 141 | + ), |
| 142 | + ); |
| 143 | + } |
| 144 | + break; |
| 145 | + } |
| 146 | + case 'GROUP_SUCCESS': { |
| 147 | + break; |
| 148 | + } |
| 149 | + case 'GROUP_FAILURE': { |
| 150 | + const group = event.group; |
| 151 | + context.incremental.push( |
| 152 | + buildIncrementalResult( |
| 153 | + { |
| 154 | + data: null, |
| 155 | + path: pathToArray(group.path), |
| 156 | + }, |
| 157 | + group.label, |
| 158 | + [event.error as GraphQLError], |
| 159 | + ), |
| 160 | + ); |
| 161 | + break; |
| 162 | + } |
| 163 | + case 'STREAM_VALUES': { |
| 164 | + const stream = event.stream; |
| 165 | + const { values } = event; |
| 166 | + const items: Array<unknown> = []; |
| 167 | + const errors: Array<GraphQLError> = []; |
| 168 | + for (const value of values) { |
| 169 | + items.push(value.item); |
| 170 | + if (value.errors !== undefined) { |
| 171 | + errors.push(...value.errors); |
| 172 | + } |
| 173 | + } |
| 174 | + let index = this._indices.get(stream); |
| 175 | + if (index === undefined) { |
| 176 | + index = stream.initialCount; |
| 177 | + this._indices.set(stream, index); |
| 178 | + } |
| 179 | + this._indices.set(stream, index + items.length); |
| 180 | + context.incremental.push( |
| 181 | + buildIncrementalResult( |
| 182 | + { |
| 183 | + items, |
| 184 | + path: pathToArray(addPath(stream.path, index, undefined)), |
| 185 | + }, |
| 186 | + stream.label, |
| 187 | + errors.length > 0 ? errors : undefined, |
| 188 | + ), |
| 189 | + ); |
| 190 | + break; |
| 191 | + } |
| 192 | + case 'STREAM_SUCCESS': { |
| 193 | + this._indices.delete(event.stream); |
| 194 | + break; |
| 195 | + } |
| 196 | + case 'STREAM_FAILURE': { |
| 197 | + this._indices.delete(event.stream); |
| 198 | + const stream = event.stream; |
| 199 | + context.incremental.push( |
| 200 | + buildIncrementalResult( |
| 201 | + { |
| 202 | + items: null, |
| 203 | + path: pathToArray(stream.path), |
| 204 | + }, |
| 205 | + stream.label, |
| 206 | + [event.error as GraphQLError], |
| 207 | + ), |
| 208 | + ); |
| 209 | + break; |
| 210 | + } |
| 211 | + case 'WORK_QUEUE_TERMINATION': { |
| 212 | + onWorkQueueFinished?.(); |
| 213 | + context.hasNext = false; |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +function buildIncrementalResult( |
| 221 | + originalIncrementalResult: |
| 222 | + | Omit<IncrementalDeferResult, 'label' | 'errors'> |
| 223 | + | Omit<IncrementalStreamResult, 'label' | 'errors'>, |
| 224 | + label: string | undefined, |
| 225 | + errors: ReadonlyArray<GraphQLError> | undefined, |
| 226 | +): IncrementalResult { |
| 227 | + const incrementalResult: IncrementalResult = originalIncrementalResult; |
| 228 | + if (errors !== undefined) { |
| 229 | + incrementalResult.errors = errors; |
| 230 | + } |
| 231 | + if (label !== undefined) { |
| 232 | + incrementalResult.label = label; |
| 233 | + } |
| 234 | + return incrementalResult; |
| 235 | +} |
0 commit comments