Skip to content

Commit

Permalink
Merge pull request #886 from apollostack/subscription-errors
Browse files Browse the repository at this point in the history
provide simple error callback in subscribeToMore
  • Loading branch information
helfer authored Nov 9, 2016
2 parents 522de12 + 292a066 commit 7dc395c
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Expect active development and potentially significant breaking changes in the `0
(https://github.com/apollostack/apollo-client/pull/866)
- Expose `SubscriptionOptions` [PR #868](https://github.com/apollostack/apollo-client/pull/868)
- Fix issue with `currentResult` and optimistic responses [Issue #877](https://github.com/apollostack/apollo-client/issues/877)
- Provide an onError callback for subscribeToMore [PR #886](https://github.com/apollostack/apollo-client/issues/886)
- Bind `resetStore` to ApolloClient [PR #882](https://github.com/apollostack/apollo-client/issues/882)
- Fix a bug with `resetStore` that caused existing queries to fail [PR #885](https://github.com/apollostack/apollo-client/issues/885)

Expand Down
7 changes: 5 additions & 2 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,11 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
this.updateQuery(mapFn);
},
error: (err) => {
// TODO implement something smart here when improving error handling
console.error(err);
if (options.onError) {
options.onError(err);
} else {
console.error('Unhandled GraphQL subscription errror', err);
}
},
});

Expand Down
1 change: 1 addition & 0 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type SubscribeToMoreOptions = {
subscriptionData: { data: any },
variables: { [key: string]: any },
}) => Object;
onError?: (error: Error) => void;
}

export interface DeprecatedSubscriptionOptions {
Expand Down
137 changes: 137 additions & 0 deletions test/subscribeToMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ describe('subscribeToMore', () => {
results: [...results],
};

const results2 = [
{ error: new Error('You cant touch this'), delay: 10 },
{ result: { name: 'Amanda Liu' }, delay: 10 },
];

const sub2 = {
request: {
query: gql`
subscription newValues {
notAnActualField
}
`,
},
id: 0,
results: [...results2],
};

const results3 = [
{ error: new Error('You cant touch this'), delay: 10 },
{ result: { name: 'Amanda Liu' }, delay: 10 },
];

const sub3 = {
request: {
query: gql`
subscription newValues {
notAnActualField
}
`,
},
id: 0,
results: [...results3],
};

it('triggers new result from subscription data', (done) => {
let latestResult: any = null;
const networkInterface = mockSubscriptionNetworkInterface([sub1], req1);
Expand Down Expand Up @@ -91,5 +125,108 @@ describe('subscribeToMore', () => {
}
});


it('calls error callback on error', (done) => {
let latestResult: any = null;
const networkInterface = mockSubscriptionNetworkInterface([sub2], req1);
let counter = 0;

const client = new ApolloClient({
networkInterface,
addTypename: false,
});

const obsHandle = client.watchQuery({
query,
});
const sub = obsHandle.subscribe({
next(queryResult) {
latestResult = queryResult;
counter++;
},
});

let errorCount = 0;

obsHandle.subscribeToMore({
document: gql`
subscription newValues {
notAnActualField
}
`,
updateQuery: (prev, { subscriptionData }) => {
return { entry: { value: subscriptionData.data.name } };
},
onError: (err) => { errorCount += 1; },
});

setTimeout(() => {
sub.unsubscribe();
assert.equal(counter, 2);
assert.deepEqual(
latestResult,
{ data: { entry: { value: 'Amanda Liu' } }, loading: false, networkStatus: 7 }
);
assert.equal(errorCount, 1);
done();
}, 50);

for (let i = 0; i < 2; i++) {
networkInterface.fireResult(0); // 0 is the id of the subscription for the NI
}
});

it('prints unhandled subscription errors to the console', (done) => {
let latestResult: any = null;
const networkInterface = mockSubscriptionNetworkInterface([sub3], req1);
let counter = 0;

const client = new ApolloClient({
networkInterface,
addTypename: false,
});

const obsHandle = client.watchQuery({
query,
});
const sub = obsHandle.subscribe({
next(queryResult) {
latestResult = queryResult;
counter++;
},
});

let errorCount = 0;
const consoleErr = console.error;
console.error = (err: Error) => { errorCount += 1; };

obsHandle.subscribeToMore({
document: gql`
subscription newValues {
notAnActualField
}
`,
updateQuery: (prev, { subscriptionData }) => {
return { entry: { value: subscriptionData.data.name } };
},
});

setTimeout(() => {
sub.unsubscribe();
assert.equal(counter, 2);
assert.deepEqual(
latestResult,
{ data: { entry: { value: 'Amanda Liu' } }, loading: false, networkStatus: 7 }
);
assert.equal(errorCount, 1);
console.error = consoleErr;
done();
}, 50);

for (let i = 0; i < 2; i++) {
networkInterface.fireResult(0); // 0 is the id of the subscription for the NI
}
});

// TODO add a test that checks that subscriptions are cancelled when obs is unsubscribed from.
});

0 comments on commit 7dc395c

Please sign in to comment.