-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
index.js
62 lines (55 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import merge from 'lodash/merge';
import buildDataProvider from 'ra-data-graphql';
import { DELETE, DELETE_MANY, UPDATE, UPDATE_MANY } from 'ra-core';
import defaultBuildQuery from './buildQuery';
const defaultOptions = {
buildQuery: defaultBuildQuery,
};
export const buildQuery = defaultBuildQuery;
export default options => {
return buildDataProvider(merge({}, defaultOptions, options)).then(
defaultDataProvider => {
return (fetchType, resource, params) => {
// Graphcool does not support multiple deletions so instead we send multiple DELETE requests
// This can be optimized using the apollo-link-batch-http link
if (fetchType === DELETE_MANY) {
const { ids, ...otherParams } = params;
return Promise.all(
params.ids.map(id =>
defaultDataProvider(DELETE, resource, {
id,
...otherParams,
})
)
).then(results => {
const data = results.reduce(
(acc, { data }) => [...acc, data.id],
[]
);
return { data };
});
}
// Graphcool does not support multiple deletions so instead we send multiple UPDATE requests
// This can be optimized using the apollo-link-batch-http link
if (fetchType === UPDATE_MANY) {
const { ids, ...otherParams } = params;
return Promise.all(
params.ids.map(id =>
defaultDataProvider(UPDATE, resource, {
id,
...otherParams,
})
)
).then(results => {
const data = results.reduce(
(acc, { data }) => [...acc, data.id],
[]
);
return { data };
});
}
return defaultDataProvider(fetchType, resource, params);
};
}
);
};