Skip to content

Commit

Permalink
Merge pull request #893 from apollostack/batching-warning
Browse files Browse the repository at this point in the history
sanity-check server response in batching NI
  • Loading branch information
helfer authored Nov 11, 2016
2 parents 0a985dc + cfca01f commit 2ce9ea5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/transport/batchedNetworkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2ce9ea5

Please sign in to comment.