@@ -53,6 +53,7 @@ import {
53
53
collectSubfields as _collectSubfields ,
54
54
} from './collectFields.js' ;
55
55
import { mapAsyncIterable } from './mapAsyncIterable.js' ;
56
+ import { Publisher } from './publisher.js' ;
56
57
import {
57
58
getArgumentValues ,
58
59
getDirectiveValues ,
@@ -2052,154 +2053,6 @@ async function executeStreamIterator(
2052
2053
}
2053
2054
}
2054
2055
2055
- /**
2056
- * @internal
2057
- */
2058
- class Publisher {
2059
- subsequentPayloads : Set < AsyncPayloadRecord > ;
2060
-
2061
- constructor ( ) {
2062
- this . subsequentPayloads = new Set ( ) ;
2063
- }
2064
-
2065
- add ( payload : AsyncPayloadRecord ) {
2066
- this . subsequentPayloads . add ( payload ) ;
2067
- }
2068
-
2069
- filterSubsequentPayloads (
2070
- nullPath : Path ,
2071
- currentAsyncRecord : AsyncPayloadRecord | undefined ,
2072
- ) : void {
2073
- const nullPathArray = pathToArray ( nullPath ) ;
2074
- this . subsequentPayloads . forEach ( ( asyncRecord ) => {
2075
- if ( asyncRecord === currentAsyncRecord ) {
2076
- // don't remove payload from where error originates
2077
- return ;
2078
- }
2079
- for ( let i = 0 ; i < nullPathArray . length ; i ++ ) {
2080
- if ( asyncRecord . path [ i ] !== nullPathArray [ i ] ) {
2081
- // asyncRecord points to a path unaffected by this payload
2082
- return ;
2083
- }
2084
- }
2085
- // asyncRecord path points to nulled error field
2086
- if ( isStreamPayload ( asyncRecord ) && asyncRecord . iterator ?. return ) {
2087
- asyncRecord . iterator . return ( ) . catch ( ( ) => {
2088
- // ignore error
2089
- } ) ;
2090
- }
2091
- this . subsequentPayloads . delete ( asyncRecord ) ;
2092
- } ) ;
2093
- }
2094
-
2095
- getCompletedIncrementalResults ( ) : Array < IncrementalResult > {
2096
- const incrementalResults : Array < IncrementalResult > = [ ] ;
2097
- for ( const asyncPayloadRecord of this . subsequentPayloads ) {
2098
- const incrementalResult : IncrementalResult = { } ;
2099
- if ( ! asyncPayloadRecord . isCompleted ) {
2100
- continue ;
2101
- }
2102
- this . subsequentPayloads . delete ( asyncPayloadRecord ) ;
2103
- if ( isStreamPayload ( asyncPayloadRecord ) ) {
2104
- const items = asyncPayloadRecord . items ;
2105
- if ( asyncPayloadRecord . isCompletedIterator ) {
2106
- // async iterable resolver just finished but there may be pending payloads
2107
- continue ;
2108
- }
2109
- ( incrementalResult as IncrementalStreamResult ) . items = items ;
2110
- } else {
2111
- const data = asyncPayloadRecord . data ;
2112
- ( incrementalResult as IncrementalDeferResult ) . data = data ?? null ;
2113
- }
2114
-
2115
- incrementalResult . path = asyncPayloadRecord . path ;
2116
- if ( asyncPayloadRecord . label ) {
2117
- incrementalResult . label = asyncPayloadRecord . label ;
2118
- }
2119
- if ( asyncPayloadRecord . errors . length > 0 ) {
2120
- incrementalResult . errors = asyncPayloadRecord . errors ;
2121
- }
2122
- incrementalResults . push ( incrementalResult ) ;
2123
- }
2124
- return incrementalResults ;
2125
- }
2126
-
2127
- yieldSubsequentPayloads ( ) : AsyncGenerator <
2128
- SubsequentIncrementalExecutionResult ,
2129
- void ,
2130
- void
2131
- > {
2132
- let isDone = false ;
2133
-
2134
- const next = async ( ) : Promise <
2135
- IteratorResult < SubsequentIncrementalExecutionResult , void >
2136
- > => {
2137
- if ( isDone ) {
2138
- return { value : undefined , done : true } ;
2139
- }
2140
-
2141
- await Promise . race (
2142
- Array . from ( this . subsequentPayloads ) . map ( ( p ) => p . promise ) ,
2143
- ) ;
2144
-
2145
- if ( isDone ) {
2146
- // a different call to next has exhausted all payloads
2147
- return { value : undefined , done : true } ;
2148
- }
2149
-
2150
- const incremental = this . getCompletedIncrementalResults ( ) ;
2151
- const hasNext = this . subsequentPayloads . size > 0 ;
2152
-
2153
- if ( ! incremental . length && hasNext ) {
2154
- return next ( ) ;
2155
- }
2156
-
2157
- if ( ! hasNext ) {
2158
- isDone = true ;
2159
- }
2160
-
2161
- return {
2162
- value : incremental . length ? { incremental, hasNext } : { hasNext } ,
2163
- done : false ,
2164
- } ;
2165
- } ;
2166
-
2167
- const returnStreamIterators = ( ) => {
2168
- const promises : Array < Promise < IteratorResult < unknown > > > = [ ] ;
2169
- this . subsequentPayloads . forEach ( ( asyncPayloadRecord ) => {
2170
- if (
2171
- isStreamPayload ( asyncPayloadRecord ) &&
2172
- asyncPayloadRecord . iterator ?. return
2173
- ) {
2174
- promises . push ( asyncPayloadRecord . iterator . return ( ) ) ;
2175
- }
2176
- } ) ;
2177
- return Promise . all ( promises ) ;
2178
- } ;
2179
-
2180
- return {
2181
- [ Symbol . asyncIterator ] ( ) {
2182
- return this ;
2183
- } ,
2184
- next,
2185
- async return ( ) : Promise <
2186
- IteratorResult < SubsequentIncrementalExecutionResult , void >
2187
- > {
2188
- await returnStreamIterators ( ) ;
2189
- isDone = true ;
2190
- return { value : undefined , done : true } ;
2191
- } ,
2192
- async throw (
2193
- error ?: unknown ,
2194
- ) : Promise < IteratorResult < SubsequentIncrementalExecutionResult , void > > {
2195
- await returnStreamIterators ( ) ;
2196
- isDone = true ;
2197
- return Promise . reject ( error ) ;
2198
- } ,
2199
- } ;
2200
- }
2201
- }
2202
-
2203
2056
class DeferredFragmentRecord {
2204
2057
type : 'defer' ;
2205
2058
errors : Array < GraphQLError > ;
@@ -2301,10 +2154,6 @@ class StreamRecord {
2301
2154
}
2302
2155
}
2303
2156
2304
- type AsyncPayloadRecord = DeferredFragmentRecord | StreamRecord ;
2157
+ export type { StreamRecord } ;
2305
2158
2306
- function isStreamPayload (
2307
- asyncPayload : AsyncPayloadRecord ,
2308
- ) : asyncPayload is StreamRecord {
2309
- return asyncPayload . type === 'stream' ;
2310
- }
2159
+ export type AsyncPayloadRecord = DeferredFragmentRecord | StreamRecord ;
0 commit comments