From 7d2e62230b2423cedaa53ab44ac628151ddf9da7 Mon Sep 17 00:00:00 2001 From: nodkz Date: Sat, 4 Mar 2017 15:10:23 +0600 Subject: [PATCH] feat(BatchRequest): Add support for apollo server batch format --- src/relay/queriesBatch.js | 17 +++++++++++++---- test/batch.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/relay/queriesBatch.js b/src/relay/queriesBatch.js index 7c0d825..145248e 100644 --- a/src/relay/queriesBatch.js +++ b/src/relay/queriesBatch.js @@ -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); + }) ); } }); diff --git a/test/batch.test.js b/test/batch.test.js index 6438303..b8e4fb4 100644 --- a/test/batch.test.js +++ b/test/batch.test.js @@ -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])); + }); });