From ef63a2f41d341d8d5cc726ef9837fceb660b1d34 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Thu, 10 Nov 2016 19:33:48 -0800 Subject: [PATCH 1/2] sanity-check server response in batching NI --- src/transport/batchedNetworkInterface.ts | 5 ++ test/client.ts | 61 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/transport/batchedNetworkInterface.ts b/src/transport/batchedNetworkInterface.ts index 0fd776b74f1..56a28c373ec 100644 --- a/src/transport/batchedNetworkInterface.ts +++ b/src/transport/batchedNetworkInterface.ts @@ -69,6 +69,11 @@ export class HTTPBatchedNetworkInterface extends HTTPFetchNetworkInterface { }) .then(responses => { + + if (typeof responses.map !== 'function') { + throw new Error('BatchingNetworkInterface: server response is not an array'); + } + type ResponseAndOptions = { response: IResponse; options: RequestInit; diff --git a/test/client.ts b/test/client.ts index 279a404f85c..fe378532693 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1718,6 +1718,67 @@ describe('client', () => { }); }); + it('should throw an error if response to batch request is not an array', (done) => { + const firstQuery = gql` + query { + author { + firstName + lastName + } + }`; + const firstResult = { + data: { + author: { + firstName: 'John', + lastName: 'Smith', + }, + }, + loading: false, + }; + const secondQuery = gql` + query { + person { + name + } + }`; + const url = 'http://not-a-real-url.com'; + const oldFetch = fetch; + fetch = createMockFetch({ + url, + opts: { + body: JSON.stringify([ + { + query: print(firstQuery), + }, + { + query: print(secondQuery), + }, + ]), + headers: { + Accept: '*/*', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + result: createMockedIResponse(firstResult), + }); + const networkInterface = createBatchingNetworkInterface({ + uri: 'http://not-a-real-url.com', + batchInterval: 5, + opts: {}, + }); + Promise.all([ + networkInterface.query({ query: firstQuery }), + networkInterface.query({ query: secondQuery }), + ]).then((results) => { + assert.equal(true, false, 'expected response to throw an error'); + }).catch( e => { + assert.equal(e.message, 'BatchingNetworkInterface: server response is not an array'); + fetch = oldFetch; + done(); + }); + }); + it('should not do transport-level batching when the interval is exceeded', (done) => { const firstQuery = gql` query { From cfca01f8d4a9ebdd5a790e2eb678465889c76598 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Thu, 10 Nov 2016 19:40:32 -0800 Subject: [PATCH 2/2] update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 924690aa5ca..46679abd58a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,11 @@ 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 +- Print a warning if server response is not an array when using transport batching [PR #893](https://github.com/apollostack/apollo-client/pull/893) ### 0.5.1 - **new Feature**: Enable chaining of `use` and `useAfter` function calls in network interface. [PR #860](https://github.com/apollostack/apollo-client/pull/860) [Issue #564](https://github.com/apollostack/apollo-client/issues/564) -- Create and expose the `MutationOptions` [PR #866] -(https://github.com/apollostack/apollo-client/pull/866) +- Create and expose the `MutationOptions` [PR #866](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)