diff --git a/src/execution/execute.ts b/src/execution/execute.ts index baf6b41ea1..5e6c8dd95d 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -1781,18 +1781,48 @@ export function createSourceEventStream( function createSourceEventStreamImpl( exeContext: ExecutionContext, ): PromiseOrValue | ExecutionResult> { - try { - const eventStream = executeSubscription(exeContext); - if (isPromise(eventStream)) { - return eventStream.then(undefined, (error) => ({ errors: [error] })); +try { + + const eventStream = executeSubscription(exeContext); + + if (isPromise(eventStream)) { + // Handle promise errors + return eventStream.then( + data => ({ data }), + error => ({ errors: [error] }) + ); + } + + const wrappedStream = { + async *[Symbol.asyncIterator]() { + try { + yield* eventStream; + } catch (error) { + yield { errors: [error] }; + } } + }; - return eventStream; - } catch (error) { - return { errors: [error] }; + return wrappedStream; + +} catch (error) { + // Handle sync errors + return { errors: [error] }; +} + +function resolve(payload) { + if (payload.errors) { + throw payload.errors[0]; + } else { + return payload.data; } } +const stream = executeSubscription(); +mapAsyncIterable(stream, resolve); + +} + function executeSubscription( exeContext: ExecutionContext, ): PromiseOrValue> {