@@ -231,6 +231,10 @@ function buildResponse(
231
231
return errors . length === 0 ? { data } : { errors, data } ;
232
232
}
233
233
234
+ function buildErrorResponse ( error : GraphQLError ) {
235
+ return { errors : [ error ] } ;
236
+ }
237
+
234
238
/**
235
239
* Constructs a ExecutionContext object from the arguments passed to
236
240
* execute, which we will pass throughout the other execution methods.
@@ -1094,29 +1098,22 @@ export function createSourceEventStream(
1094
1098
return { errors : exeContext } ;
1095
1099
}
1096
1100
1097
- try {
1098
- const eventStream = executeSubscription ( exeContext ) ;
1099
- if ( isPromise ( eventStream ) ) {
1100
- return eventStream . then ( undefined , ( error ) => ( { errors : [ error ] } ) ) ;
1101
- }
1102
-
1103
- return eventStream ;
1104
- } catch ( error ) {
1105
- return { errors : [ error ] } ;
1106
- }
1101
+ return executeSubscription ( exeContext ) ;
1107
1102
}
1108
1103
1109
1104
function executeSubscription (
1110
1105
exeContext : ExecutionContext ,
1111
- ) : PromiseOrValue < AsyncIterable < unknown > > {
1106
+ ) : PromiseOrValue < AsyncIterable < unknown > | ExecutionResult > {
1112
1107
const { schema, fragments, operation, variableValues, rootValue } =
1113
1108
exeContext ;
1114
1109
1115
1110
const rootType = schema . getSubscriptionType ( ) ;
1116
1111
if ( rootType == null ) {
1117
- throw new GraphQLError (
1118
- 'Schema is not configured to execute subscription operation.' ,
1119
- { nodes : operation } ,
1112
+ return buildErrorResponse (
1113
+ new GraphQLError (
1114
+ 'Schema is not configured to execute subscription operation.' ,
1115
+ { nodes : operation } ,
1116
+ ) ,
1120
1117
) ;
1121
1118
}
1122
1119
@@ -1168,14 +1165,20 @@ function executeSubscription(
1168
1165
const result = resolveFn ( rootValue , args , contextValue , info ) ;
1169
1166
1170
1167
if ( isPromise ( result ) ) {
1171
- return result . then ( assertEventStream ) . then ( undefined , ( error ) => {
1172
- throw locatedError ( error , fieldNodes , pathToArray ( path ) ) ;
1173
- } ) ;
1168
+ return result
1169
+ . then ( assertEventStream )
1170
+ . then ( undefined , ( error ) =>
1171
+ buildErrorResponse (
1172
+ locatedError ( error , fieldNodes , pathToArray ( path ) ) ,
1173
+ ) ,
1174
+ ) ;
1174
1175
}
1175
1176
1176
1177
return assertEventStream ( result ) ;
1177
1178
} catch ( error ) {
1178
- throw locatedError ( error , fieldNodes , pathToArray ( path ) ) ;
1179
+ return buildErrorResponse (
1180
+ locatedError ( error , fieldNodes , pathToArray ( path ) ) ,
1181
+ ) ;
1179
1182
}
1180
1183
}
1181
1184
0 commit comments