Skip to content

Commit 0779c36

Browse files
robzhuleebyron
authored andcommitted
Update subscribe function to return a Promise of AsyncIterator or ExecutionResult (#918)
* Report or Reject when encountering Errors. Currently the `subscribe()` function throws Errors, however this is awkward when used along with async functions which would expect a rejected iteration to represent failure. Also GraphQLErrors should be reported back to the client since they represent client-provided errors. Updates test cases to represent this new behavior. Includes a new utility `asyncIteratorReject`, and extends the behavior of `mapAsyncIterator` to help implement this. * Subscriptions: Respond with error when failing to create source If a subscribe resolve function throws or returns an error, that typically indicates an issue to be returned to the requesting client. This coerces errors into located GraphQLErrors so they are correctly reported. * Subscriptions: Test source errors and execution errors. After discussion in #868, decided that errors emitted from a source event stream should be considered "internal" errors and pass through. However errors encountered during GraphQL execution on a source event should be considered "field" or "query" errors and be represented within that Response. * Update subscribe signature to return a promise of AsyncIterator/Error/ExecutionResult * Throw errors instead of returning them from Subscribe() * Lint, refactor error mapper and add comments * Report or Reject when encountering Errors. Currently the `subscribe()` function throws Errors, however this is awkward when used along with async functions which would expect a rejected iteration to represent failure. Also GraphQLErrors should be reported back to the client since they represent client-provided errors. Updates test cases to represent this new behavior. Includes a new utility `asyncIteratorReject`, and extends the behavior of `mapAsyncIterator` to help implement this. * Subscriptions: Respond with error when failing to create source If a subscribe resolve function throws or returns an error, that typically indicates an issue to be returned to the requesting client. This coerces errors into located GraphQLErrors so they are correctly reported. * Subscriptions: Test source errors and execution errors. After discussion in #868, decided that errors emitted from a source event stream should be considered "internal" errors and pass through. However errors encountered during GraphQL execution on a source event should be considered "field" or "query" errors and be represented within that Response. * Update subscribe signature to return a promise of AsyncIterator/Error/ExecutionResult * Throw errors instead of returning them from Subscribe() * Lint, refactor error mapper and add comments * Update test case assertions for stream errors to be more precise * Add test case for wrong variable types * Fix lint error (extra spaces) * Fix multi-line doc block format * Minor edits * Trim trailing whitespace
1 parent 24a5038 commit 0779c36

File tree

6 files changed

+769
-259
lines changed

6 files changed

+769
-259
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2017, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
import { expect } from 'chai';
11+
import { describe, it } from 'mocha';
12+
13+
import asyncIteratorReject from '../asyncIteratorReject';
14+
15+
describe('asyncIteratorReject', () => {
16+
17+
it('creates a failing async iterator', async () => {
18+
const error = new Error('Oh no, Mr. Bill!');
19+
const iter = asyncIteratorReject(error);
20+
21+
let caughtError;
22+
try {
23+
await iter.next();
24+
} catch (thrownError) {
25+
caughtError = thrownError;
26+
}
27+
expect(caughtError).to.equal(error);
28+
29+
expect(await iter.next()).to.deep.equal({ done: true, value: undefined });
30+
});
31+
32+
it('can be closed before failing', async () => {
33+
const error = new Error('Oh no, Mr. Bill!');
34+
const iter = asyncIteratorReject(error);
35+
36+
// Close iterator
37+
expect(await iter.return()).to.deep.equal({ done: true, value: undefined });
38+
39+
expect(await iter.next()).to.deep.equal({ done: true, value: undefined });
40+
});
41+
});

src/subscription/__tests__/mapAsyncIterator-test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,53 @@ describe('mapAsyncIterator', () => {
194194
).to.deep.equal({ value: undefined, done: true });
195195
});
196196

197+
it('does not normally map over thrown errors', async () => {
198+
async function* source() {
199+
yield 'Hello';
200+
throw new Error('Goodbye');
201+
}
202+
203+
const doubles = mapAsyncIterator(source(), x => x + x);
204+
205+
expect(
206+
await doubles.next()
207+
).to.deep.equal({ value: 'HelloHello', done: false });
208+
209+
let caughtError;
210+
try {
211+
await doubles.next();
212+
} catch (e) {
213+
caughtError = e;
214+
}
215+
expect(caughtError && caughtError.message).to.equal('Goodbye');
216+
});
217+
218+
it('maps over thrown errors if second callback provided', async () => {
219+
async function* source() {
220+
yield 'Hello';
221+
throw new Error('Goodbye');
222+
}
223+
224+
const doubles = mapAsyncIterator(
225+
source(),
226+
x => x + x,
227+
error => error
228+
);
229+
230+
expect(
231+
await doubles.next()
232+
).to.deep.equal({ value: 'HelloHello', done: false });
233+
234+
const result = await doubles.next();
235+
expect(result.value).to.be.instanceof(Error);
236+
expect(result.value && result.value.message).to.equal('Goodbye');
237+
expect(result.done).to.equal(false);
238+
239+
expect(
240+
await doubles.next()
241+
).to.deep.equal({ value: undefined, done: true });
242+
});
243+
197244
async function testClosesSourceWithMapper(mapper) {
198245
let didVisitFinally = false;
199246

@@ -251,4 +298,45 @@ describe('mapAsyncIterator', () => {
251298
});
252299
});
253300

301+
async function testClosesSourceWithRejectMapper(mapper) {
302+
async function* source() {
303+
yield 1;
304+
throw new Error(2);
305+
}
306+
307+
const throwOver1 = mapAsyncIterator(source(), x => x, mapper);
308+
309+
expect(
310+
await throwOver1.next()
311+
).to.deep.equal({ value: 1, done: false });
312+
313+
let expectedError;
314+
try {
315+
await throwOver1.next();
316+
} catch (error) {
317+
expectedError = error;
318+
}
319+
320+
expect(expectedError).to.be.an('error');
321+
if (expectedError) {
322+
expect(expectedError.message).to.equal('Cannot count to 2');
323+
}
324+
325+
expect(
326+
await throwOver1.next()
327+
).to.deep.equal({ value: undefined, done: true });
328+
}
329+
330+
it('closes source if mapper throws an error', async () => {
331+
await testClosesSourceWithRejectMapper(error => {
332+
throw new Error('Cannot count to ' + error.message);
333+
});
334+
});
335+
336+
it('closes source if mapper rejects', async () => {
337+
await testClosesSourceWithRejectMapper(async error => {
338+
throw new Error('Cannot count to ' + error.message);
339+
});
340+
});
341+
254342
});

0 commit comments

Comments
 (0)