Skip to content

Commit 30f6596

Browse files
committed
change incorrect subscribe return type to a GraphQLError rather than a systems error
to match spec (in parallel to field-level resolver errors in execute) note the addition of the path, as execution has begun
1 parent 338e3dd commit 30f6596

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/execution/__tests__/subscribe-test.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,17 @@ describe('Subscription Initialization Phase', () => {
446446

447447
const document = parse('subscription { foo }');
448448

449-
(await expectPromise(subscribe({ schema, document }))).toRejectWith(
450-
'Subscription field must return Async Iterable. Received: "test".',
451-
);
449+
const result = await subscribe({ schema, document });
450+
expectJSON(result).toDeepEqual({
451+
errors: [
452+
{
453+
message:
454+
'Subscription field must return Async Iterable. Received: "test".',
455+
locations: [{ line: 1, column: 16 }],
456+
path: ['foo'],
457+
},
458+
],
459+
});
452460
});
453461

454462
it('resolves to an error for subscription resolver errors', async () => {

src/execution/subscribe.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,15 @@ export async function createSourceEventStream(
157157
try {
158158
const eventStream = await executeSubscription(exeContext);
159159

160-
// Assert field returned an event stream, otherwise yield an error.
161-
if (!isAsyncIterable(eventStream)) {
162-
throw new Error(
163-
'Subscription field must return Async Iterable. ' +
164-
`Received: ${inspect(eventStream)}.`,
165-
);
166-
}
167-
168160
return eventStream;
169161
} catch (error) {
170-
// If it GraphQLError, report it as an ExecutionResult, containing only errors and no data.
171-
// Otherwise treat the error as a system-class error and re-throw it.
172-
if (error instanceof GraphQLError) {
173-
return { errors: [error] };
174-
}
175-
throw error;
162+
return { errors: [error] };
176163
}
177164
}
178165

179166
async function executeSubscription(
180167
exeContext: ExecutionContext,
181-
): Promise<unknown> {
168+
): Promise<AsyncIterable<unknown>> {
182169
const { schema, fragments, operation, variableValues, rootValue } =
183170
exeContext;
184171

@@ -238,6 +225,15 @@ async function executeSubscription(
238225
if (eventStream instanceof Error) {
239226
throw eventStream;
240227
}
228+
229+
// Assert field returned an event stream, otherwise yield an error.
230+
if (!isAsyncIterable(eventStream)) {
231+
throw new GraphQLError(
232+
'Subscription field must return Async Iterable. ' +
233+
`Received: ${inspect(eventStream)}.`,
234+
);
235+
}
236+
241237
return eventStream;
242238
} catch (error) {
243239
throw locatedError(error, fieldNodes, pathToArray(path));

0 commit comments

Comments
 (0)