From 20b0486f0b5485ee63692d577b36eaf3dfa3b204 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Thu, 23 Mar 2017 14:04:32 -0700 Subject: [PATCH 1/4] Fix query deduplication so it handles fetch errors --- src/transport/Deduplicator.ts | 4 ++++ test/deduplicator.ts | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/transport/Deduplicator.ts b/src/transport/Deduplicator.ts index b704f880033..8a172e24fc1 100644 --- a/src/transport/Deduplicator.ts +++ b/src/transport/Deduplicator.ts @@ -32,6 +32,10 @@ export class Deduplicator { .then( res => { delete this.inFlightRequestPromises[key]; return res; + }) + .catch( err => { + delete this.inFlightRequestPromises[key]; + throw err; }); } diff --git a/test/deduplicator.ts b/test/deduplicator.ts index a4c9e892db9..6f74236c994 100644 --- a/test/deduplicator.ts +++ b/test/deduplicator.ts @@ -45,6 +45,47 @@ describe('query deduplication', () => { }); + it(`will not deduplicate requests following an errored query`, () => { + + const document: DocumentNode = gql`query test1($x: String){ + test(x: $x) + }`; + const variables = { x: 'Hello World' }; + + const request: Request = { + query: document, + variables: variables, + operationName: getOperationName(document), + }; + + let called = 0; + const deduper = new Deduplicator({ + query: () => { + called += 1; + switch (called) { + case 1: + return new Promise((resolve, reject) => { + setTimeout(reject); + }); + case 2: + return new Promise((resolve, reject) => { + setTimeout(resolve); + }); + default: + return assert(false, 'Should not have been called more than twice'); + } + + }, + } as any ); + + return deduper.query(request) + .catch( () => { + deduper.query(request); + return assert.equal(called, 2); + }); + + }); + it(`deduplicates identical queries`, () => { const document: DocumentNode = gql`query test1($x: String){ From 8e77c2980e9be30df60e2f8e1290c42704621ee8 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Thu, 23 Mar 2017 14:04:58 -0700 Subject: [PATCH 2/4] patch tests that depended on timing of promise rejections --- test/fetchMore.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fetchMore.ts b/test/fetchMore.ts index cf063411c00..323ba565b72 100644 --- a/test/fetchMore.ts +++ b/test/fetchMore.ts @@ -52,7 +52,7 @@ describe('updateQuery on a simple query', () => { }, }); - return new Promise((resolve) => setTimeout(resolve)) + return new Promise((resolve) => setTimeout(resolve, 5)) .then(() => obsHandle) .then((watchedQuery: ObservableQuery) => { assert.equal(latestResult.data.entry.value, 1); @@ -119,7 +119,7 @@ describe('updateQuery on a query with required and optional variables', () => { }, }); - return new Promise((resolve) => setTimeout(resolve)) + return new Promise((resolve) => setTimeout(resolve, 5)) .then(() => obsHandle) .then((watchedQuery: ObservableQuery) => { assert.equal(latestResult.data.entry.value, 1); From 88b0f40b29cdbb07b97442796168d93080fb8922 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Thu, 23 Mar 2017 14:11:43 -0700 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 683c3044448..bc68db99393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +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 `cachePolicy: cache-and-network` queries never dispatching `APOLLO_QUERY_RESULT_CLIENT` [PR #1463](https://github.com/apollographql/apollo-client/pull/1463) +- Fix: `cachePolicy: cache-and-network` queries now dispatch `APOLLO_QUERY_RESULT_CLIENT` [PR #1463](https://github.com/apollographql/apollo-client/pull/1463) +- Fix: query deduplication should not cause query errors to prevent subsequent successful execution of the same query [PR #1481](https://github.com/apollographql/apollo-client/pull/1481) ### 1.0.0-rc.6 From 2f32c4d3aaabf424e79bd68848cfe5fc7c1b41da Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Thu, 23 Mar 2017 14:12:15 -0700 Subject: [PATCH 4/4] update changelog again --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc68db99393..12793481793 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Expect active development and potentially significant breaking changes in the `0 ### vNEXT - Fix: `cachePolicy: cache-and-network` queries now dispatch `APOLLO_QUERY_RESULT_CLIENT` [PR #1463](https://github.com/apollographql/apollo-client/pull/1463) -- Fix: query deduplication should not cause query errors to prevent subsequent successful execution of the same query [PR #1481](https://github.com/apollographql/apollo-client/pull/1481) +- Fix: query deduplication no longer causes query errors to prevent subsequent successful execution of the same query [PR #1481](https://github.com/apollographql/apollo-client/pull/1481) ### 1.0.0-rc.6