Skip to content

Commit f28863c

Browse files
authored
Update execute.ts
issue-:graphql#4001
1 parent 2aedf25 commit f28863c

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

Diff for: src/execution/execute.ts

+37-7
Original file line numberDiff line numberDiff line change
@@ -1781,18 +1781,48 @@ export function createSourceEventStream(
17811781
function createSourceEventStreamImpl(
17821782
exeContext: ExecutionContext,
17831783
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
1784-
try {
1785-
const eventStream = executeSubscription(exeContext);
1786-
if (isPromise(eventStream)) {
1787-
return eventStream.then(undefined, (error) => ({ errors: [error] }));
1784+
try {
1785+
1786+
const eventStream = executeSubscription(exeContext);
1787+
1788+
if (isPromise(eventStream)) {
1789+
// Handle promise errors
1790+
return eventStream.then(
1791+
data => ({ data }),
1792+
error => ({ errors: [error] })
1793+
);
1794+
}
1795+
1796+
const wrappedStream = {
1797+
async *[Symbol.asyncIterator]() {
1798+
try {
1799+
yield* eventStream;
1800+
} catch (error) {
1801+
yield { errors: [error] };
1802+
}
17881803
}
1804+
};
17891805

1790-
return eventStream;
1791-
} catch (error) {
1792-
return { errors: [error] };
1806+
return wrappedStream;
1807+
1808+
} catch (error) {
1809+
// Handle sync errors
1810+
return { errors: [error] };
1811+
}
1812+
1813+
function resolve(payload) {
1814+
if (payload.errors) {
1815+
throw payload.errors[0];
1816+
} else {
1817+
return payload.data;
17931818
}
17941819
}
17951820

1821+
const stream = executeSubscription();
1822+
mapAsyncIterable(stream, resolve);
1823+
1824+
}
1825+
17961826
function executeSubscription(
17971827
exeContext: ExecutionContext,
17981828
): PromiseOrValue<AsyncIterable<unknown>> {

0 commit comments

Comments
 (0)