Skip to content

Commit a1b86ac

Browse files
committed
step 1
1 parent dbd1118 commit a1b86ac

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/execution/execute.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ function buildResponse(
231231
return errors.length === 0 ? { data } : { errors, data };
232232
}
233233

234+
function buildErrorResponse(error: GraphQLError) {
235+
return { errors: [error] };
236+
}
237+
234238
/**
235239
* Constructs a ExecutionContext object from the arguments passed to
236240
* execute, which we will pass throughout the other execution methods.
@@ -1094,29 +1098,22 @@ export function createSourceEventStream(
10941098
return { errors: exeContext };
10951099
}
10961100

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);
11071102
}
11081103

11091104
function executeSubscription(
11101105
exeContext: ExecutionContext,
1111-
): PromiseOrValue<AsyncIterable<unknown>> {
1106+
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
11121107
const { schema, fragments, operation, variableValues, rootValue } =
11131108
exeContext;
11141109

11151110
const rootType = schema.getSubscriptionType();
11161111
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+
),
11201117
);
11211118
}
11221119

@@ -1168,14 +1165,20 @@ function executeSubscription(
11681165
const result = resolveFn(rootValue, args, contextValue, info);
11691166

11701167
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+
);
11741175
}
11751176

11761177
return assertEventStream(result);
11771178
} catch (error) {
1178-
throw locatedError(error, fieldNodes, pathToArray(path));
1179+
return buildErrorResponse(
1180+
locatedError(error, fieldNodes, pathToArray(path)),
1181+
);
11791182
}
11801183
}
11811184

0 commit comments

Comments
 (0)