Skip to content

Commit

Permalink
Update execute.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
xonx4l committed Dec 31, 2023
1 parent 2aedf25 commit f28863c
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/execution/execute.ts
Expand Up @@ -1781,18 +1781,48 @@ export function createSourceEventStream(
function createSourceEventStreamImpl(
exeContext: ExecutionContext,
): PromiseOrValue<AsyncIterable<unknown> | 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<AsyncIterable<unknown>> {
Expand Down

0 comments on commit f28863c

Please sign in to comment.