Skip to content
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

Invoke afterware after any kind of response. #1351

Merged
merged 4 commits into from
Mar 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API.

### vNEXT
- Fix: Invoke afterware even on requests that error [PR #1351](https://github.com/apollographql/apollo-client/pull/1351)


### 0.10.1
- Address deprecation warnings coming from `graphql-tag` [graphql-tag#54](https://github.com/apollographql/graphql-tag/issues/54)
Expand Down
9 changes: 6 additions & 3 deletions src/transport/batchedNetworkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ export class HTTPBatchedNetworkInterface extends BaseNetworkInterface {
const httpResponse = result as Response;

if (!httpResponse.ok) {
const httpError = new Error(`Network request failed with status ${httpResponse.status} - "${httpResponse.statusText}"`);
(httpError as any).response = httpResponse;
return this.applyBatchAfterwares({ responses: [httpResponse], options: batchRequestAndOptions })
.then(() => {
const httpError = new Error(`Network request failed with status ${httpResponse.status} - "${httpResponse.statusText}"`);
(httpError as any).response = httpResponse;

throw httpError;
throw httpError;
});
}

// XXX can we be stricter with the type here?
Expand Down
54 changes: 54 additions & 0 deletions test/batchedNetworkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,58 @@ describe('HTTPBatchedNetworkInterface', () => {
opts: options,
});
});

describe('afterware execution', () => {
const afterwareStub = sinon.stub();
const testAfterwares: BatchAfterwareInterface[] = [
{
applyBatchAfterware(response, next) {
afterwareStub();
next();
},
},
{
applyBatchAfterware(response, next) {
afterwareStub();
next();
},
},
];

afterEach(() => afterwareStub.reset());

it('executes afterware when valid responses given back', done => {
assertRoundtrip({
requestResultPairs: [{
request: { query: authorQuery },
result: authorResult,
}],
afterwares: testAfterwares,
}).then(() => {
assert.equal(afterwareStub.callCount, testAfterwares.length, 'Afterwares provided were not invoked');
done();
}).catch(err => {
done(err);
});
});

it('executes afterware when an invalid response is given back', done => {
const fakeForbiddenResponse = createMockedIResponse([], { status: 401, statusText: 'Unauthorized'});
const fetchFunc = () => Promise.resolve(fakeForbiddenResponse);

assertRoundtrip({
requestResultPairs: [{
request: { query: authorQuery },
result: authorResult,
}],
fetchFunc,
afterwares: testAfterwares,
}).then(() => {
done(new Error('The networkInterface did not reject as expected'));
}).catch(err => {
assert.equal(afterwareStub.callCount, testAfterwares.length, 'Afterwares provided were not invoked');
done();
});
});
});
});