Skip to content

Update subscribe function to return a Promise of AsyncIterator or ExecutionResult #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 14, 2017
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
leebyron committed May 30, 2017
commit 1d937d294262d711589ca304624d85d7d9074428
130 changes: 130 additions & 0 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,134 @@ describe('Subscribe', () => {
await subscription.next()
).to.deep.equal({ value: undefined, done: true });
});

it('should handle error during execuction of source event', async () => {
const erroringEmailSchema = emailSchemaWithResolvers(
async function* () {
yield { email: { subject: 'Hello' } };
yield { email: { subject: 'Goodbye' } };
yield { email: { subject: 'Bonjour' } };
},
event => {
if (event.email.subject === 'Goodbye') {
throw new Error('Never leave.');
}
return event;
}
);

const subscription = subscribe(
erroringEmailSchema,
parse(`
subscription {
importantEmail {
email {
subject
}
}
}
`)
);

const payload1 = await subscription.next();
expect(payload1).to.deep.equal({
done: false,
value: {
data: {
importantEmail: {
email: {
subject: 'Hello'
}
}
}
}
});

// An error in execution is presented as such.
const payload2 = await subscription.next();
expect(payload2).to.deep.equal({
done: false,
value: {
errors: [
{
message: 'Never leave.',
locations: [ { line: 3, column: 11 } ],
path: [ 'importantEmail' ],
}
],
data: {
importantEmail: null,
}
}
});

// However that does not close the response event stream. Subsequent
// events are still executed.
const payload3 = await subscription.next();
expect(payload3).to.deep.equal({
done: false,
value: {
data: {
importantEmail: {
email: {
subject: 'Bonjour'
}
}
}
}
});

});

it('should pass through error thrown in source event stream', async () => {
const erroringEmailSchema = emailSchemaWithResolvers(
async function* () {
yield { email: { subject: 'Hello' } };
throw new Error('test error');
},
email => email
);

const subscription = subscribe(
erroringEmailSchema,
parse(`
subscription {
importantEmail {
email {
subject
}
}
}
`)
);

const payload1 = await subscription.next();
expect(payload1).to.deep.equal({
done: false,
value: {
data: {
importantEmail: {
email: {
subject: 'Hello'
}
}
}
}
});

let expectedError;
try {
await subscription.next();
} catch (error) {
expectedError = error;
}

expect(expectedError).to.deep.equal(new Error('test error'));

const payload2 = await subscription.next();
expect(payload2).to.deep.equal({
done: true,
value: undefined
});
});
});