Skip to content

Commit

Permalink
feat(BatchRequest): Add support for apollo server batch format
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Mar 4, 2017
1 parent 6eb3616 commit 7d2e622
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/relay/queriesBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ export default function queriesBatch(relayRequestList, fetchWithMiddleware) {
);

return fetchWithMiddleware(req)
.then(payloadList => {
payloadList.forEach(({ id, payload }) => {
const relayRequest = requestMap[id];
.then(batchResponses => {
batchResponses.forEach((res) => {
if (!res) return;
const relayRequest = requestMap[res.id];

if (relayRequest) {
queryPost(
relayRequest,
new Promise(resolve => { resolve(payload); })
new Promise(resolve => {
if (res.payload) {
resolve(res.payload);
return;
}
// compatibility with graphene-django and apollo-server batch format
resolve(res);
})
);
}
});
Expand Down
26 changes: 26 additions & 0 deletions test/batch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,30 @@ describe('Batch tests', () => {
const req2 = mockReq(2);
assert.isFulfilled(rnl.sendQueries([req1, req2]));
});

it('should handle responces without payload wrapper', () => {
fetchMock.mock({
matcher: '/graphql/batch',
response: {
status: 200,
body: [
{
id: 1,
errors: [
{ location: 1, message: 'major error' },
],
},
{ id: 2, data: {} },
],
},
method: 'POST',
});

const req1 = mockReq(1);
req1.reject = (err) => {
assert(err instanceof Error, 'should be an error');
};
const req2 = mockReq(2);
assert.isFulfilled(rnl.sendQueries([req1, req2]));
});
});

0 comments on commit 7d2e622

Please sign in to comment.