@@ -38,8 +38,9 @@ export type SubscriptionArgs = {|
38
38
* Implements the "Subscribe" algorithm described in the GraphQL specification.
39
39
*
40
40
* Returns a Promise which resolves to either an AsyncIterator (if successful)
41
- * or an ExecutionResult (client error). The promise will be rejected if a
42
- * server error occurs.
41
+ * or an ExecutionResult (error). The promise will be rejected if the schema or
42
+ * other arguments to this function are invalid, or if the resolved event stream
43
+ * is not an async iterable.
43
44
*
44
45
* If the client-provided arguments to this function do not result in a
45
46
* compliant subscription, a GraphQL Response (ExecutionResult) with
@@ -160,19 +161,28 @@ function subscribeImpl(
160
161
reportGraphQLError ,
161
162
)
162
163
: ( ( resultOrStream : any ) : ExecutionResult ) ,
163
- reportGraphQLError ,
164
164
) ;
165
165
}
166
166
167
167
/**
168
168
* Implements the "CreateSourceEventStream" algorithm described in the
169
169
* GraphQL specification, resolving the subscription source event stream.
170
170
*
171
- * Returns a Promise<AsyncIterable>.
171
+ * Returns a Promise which resolves to either an AsyncIterable (if successful)
172
+ * or an ExecutionResult (error). The promise will be rejected if the schema or
173
+ * other arguments to this function are invalid, or if the resolved event stream
174
+ * is not an async iterable.
172
175
*
173
- * If the client-provided invalid arguments, the source stream could not be
174
- * created, or the resolver did not return an AsyncIterable, this function will
175
- * will throw an error, which should be caught and handled by the caller.
176
+ * If the client-provided arguments to this function do not result in a
177
+ * compliant subscription, a GraphQL Response (ExecutionResult) with
178
+ * descriptive errors and no data will be returned.
179
+ *
180
+ * If the the source stream could not be created due to faulty subscription
181
+ * resolver logic or underlying systems, the promise will resolve to a single
182
+ * ExecutionResult containing `errors` and no `data`.
183
+ *
184
+ * If the operation succeeded, the promise resolves to the AsyncIterable for the
185
+ * event stream returned by the resolver.
176
186
*
177
187
* A Source Event Stream represents a sequence of events, each of which triggers
178
188
* a GraphQL execution for that event.
@@ -259,7 +269,11 @@ export function createSourceEventStream(
259
269
return Promise . resolve ( result ) . then ( eventStream => {
260
270
// If eventStream is an Error, rethrow a located error.
261
271
if ( eventStream instanceof Error ) {
262
- throw locatedError ( eventStream , fieldNodes , responsePathAsArray ( path ) ) ;
272
+ return {
273
+ errors : [
274
+ locatedError ( eventStream , fieldNodes , responsePathAsArray ( path ) ) ,
275
+ ] ,
276
+ } ;
263
277
}
264
278
265
279
// Assert field returned an event stream, otherwise yield an error.
@@ -273,6 +287,11 @@ export function createSourceEventStream(
273
287
) ;
274
288
} ) ;
275
289
} catch ( error ) {
276
- return Promise . reject ( error ) ;
290
+ // As with reportGraphQLError above, if the error is a GraphQLError, report
291
+ // it as an ExecutionResult; otherwise treat it as a system-class error and
292
+ // re-throw it.
293
+ return error instanceof GraphQLError
294
+ ? Promise . resolve ( { errors : [ error ] } )
295
+ : Promise . reject ( error ) ;
277
296
}
278
297
}
0 commit comments