@@ -35,8 +35,9 @@ import { getOperationRootType } from '../utilities/getOperationRootType';
35
35
* Implements the "Subscribe" algorithm described in the GraphQL specification.
36
36
*
37
37
* Returns a Promise which resolves to either an AsyncIterator (if successful)
38
- * or an ExecutionResult (client error). The promise will be rejected if a
39
- * server error occurs.
38
+ * or an ExecutionResult (error). The promise will be rejected if the schema or
39
+ * other arguments to this function are invalid, or if the resolved event stream
40
+ * is not an async iterable.
40
41
*
41
42
* If the client-provided arguments to this function do not result in a
42
43
* compliant subscription, a GraphQL Response (ExecutionResult) with
@@ -171,19 +172,28 @@ function subscribeImpl(
171
172
reportGraphQLError ,
172
173
)
173
174
: ( ( resultOrStream : any ) : ExecutionResult ) ,
174
- reportGraphQLError ,
175
175
) ;
176
176
}
177
177
178
178
/**
179
179
* Implements the "CreateSourceEventStream" algorithm described in the
180
180
* GraphQL specification, resolving the subscription source event stream.
181
181
*
182
- * Returns a Promise<AsyncIterable>.
182
+ * Returns a Promise which resolves to either an AsyncIterable (if successful)
183
+ * or an ExecutionResult (error). The promise will be rejected if the schema or
184
+ * other arguments to this function are invalid, or if the resolved event stream
185
+ * is not an async iterable.
183
186
*
184
- * If the client-provided invalid arguments, the source stream could not be
185
- * created, or the resolver did not return an AsyncIterable, this function will
186
- * will throw an error, which should be caught and handled by the caller.
187
+ * If the client-provided arguments to this function do not result in a
188
+ * compliant subscription, a GraphQL Response (ExecutionResult) with
189
+ * descriptive errors and no data will be returned.
190
+ *
191
+ * If the the source stream could not be created due to faulty subscription
192
+ * resolver logic or underlying systems, the promise will resolve to a single
193
+ * ExecutionResult containing `errors` and no `data`.
194
+ *
195
+ * If the operation succeeded, the promise resolves to the AsyncIterable for the
196
+ * event stream returned by the resolver.
187
197
*
188
198
* A Source Event Stream represents a sequence of events, each of which triggers
189
199
* a GraphQL execution for that event.
@@ -270,7 +280,11 @@ export function createSourceEventStream(
270
280
return Promise . resolve ( result ) . then ( eventStream => {
271
281
// If eventStream is an Error, rethrow a located error.
272
282
if ( eventStream instanceof Error ) {
273
- throw locatedError ( eventStream , fieldNodes , responsePathAsArray ( path ) ) ;
283
+ return {
284
+ errors : [
285
+ locatedError ( eventStream , fieldNodes , responsePathAsArray ( path ) ) ,
286
+ ] ,
287
+ } ;
274
288
}
275
289
276
290
// Assert field returned an event stream, otherwise yield an error.
@@ -284,6 +298,8 @@ export function createSourceEventStream(
284
298
) ;
285
299
} ) ;
286
300
} catch ( error ) {
287
- return Promise . reject ( error ) ;
301
+ return error instanceof GraphQLError
302
+ ? Promise . resolve ( { errors : [ error ] } )
303
+ : Promise . reject ( error ) ;
288
304
}
289
305
}
0 commit comments