From 8b5bd997f28ae148e4b038776d6db57dfde7d853 Mon Sep 17 00:00:00 2001 From: Doug Swain Date: Tue, 28 Feb 2017 22:29:33 -0500 Subject: [PATCH 1/2] Invoke afterware after any kind of response. Addresses https://github.com/apollographql/apollo-client/issues/1303 --- src/transport/batchedNetworkInterface.ts | 9 ++-- test/batchedNetworkInterface.ts | 54 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/transport/batchedNetworkInterface.ts b/src/transport/batchedNetworkInterface.ts index 5eceff1f094..359a370f4d5 100644 --- a/src/transport/batchedNetworkInterface.ts +++ b/src/transport/batchedNetworkInterface.ts @@ -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? diff --git a/test/batchedNetworkInterface.ts b/test/batchedNetworkInterface.ts index a3daf463904..d230e36fc02 100644 --- a/test/batchedNetworkInterface.ts +++ b/test/batchedNetworkInterface.ts @@ -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(); + }); + }); + }); }); From fa49ee1ce4c1410575ac9be7f87471fba0b411b8 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sat, 4 Mar 2017 20:06:08 -0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e16a764997..b1f513199d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)