Skip to content

Commit 7d2e622

Browse files
committed
feat(BatchRequest): Add support for apollo server batch format
1 parent 6eb3616 commit 7d2e622

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/relay/queriesBatch.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ export default function queriesBatch(relayRequestList, fetchWithMiddleware) {
2727
);
2828

2929
return fetchWithMiddleware(req)
30-
.then(payloadList => {
31-
payloadList.forEach(({ id, payload }) => {
32-
const relayRequest = requestMap[id];
30+
.then(batchResponses => {
31+
batchResponses.forEach((res) => {
32+
if (!res) return;
33+
const relayRequest = requestMap[res.id];
34+
3335
if (relayRequest) {
3436
queryPost(
3537
relayRequest,
36-
new Promise(resolve => { resolve(payload); })
38+
new Promise(resolve => {
39+
if (res.payload) {
40+
resolve(res.payload);
41+
return;
42+
}
43+
// compatibility with graphene-django and apollo-server batch format
44+
resolve(res);
45+
})
3746
);
3847
}
3948
});

test/batch.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,30 @@ describe('Batch tests', () => {
5858
const req2 = mockReq(2);
5959
assert.isFulfilled(rnl.sendQueries([req1, req2]));
6060
});
61+
62+
it('should handle responces without payload wrapper', () => {
63+
fetchMock.mock({
64+
matcher: '/graphql/batch',
65+
response: {
66+
status: 200,
67+
body: [
68+
{
69+
id: 1,
70+
errors: [
71+
{ location: 1, message: 'major error' },
72+
],
73+
},
74+
{ id: 2, data: {} },
75+
],
76+
},
77+
method: 'POST',
78+
});
79+
80+
const req1 = mockReq(1);
81+
req1.reject = (err) => {
82+
assert(err instanceof Error, 'should be an error');
83+
};
84+
const req2 = mockReq(2);
85+
assert.isFulfilled(rnl.sendQueries([req1, req2]));
86+
});
6187
});

0 commit comments

Comments
 (0)