forked from des-des/ra-postgraphile-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.35 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
const buildGraphQLProvider = require('ra-data-graphql').default
const { createHttpLink } = require('apollo-link-http')
const { InMemoryCache } = require('apollo-cache-inmemory')
const { ApolloClient } = require('apollo-client')
const postgraphileBuildQuery = require('./lib/build_query')
const introspection = require('./lib/introspection')
const buildPostgraphileProvider = ({
apolloHttpLinkOptions,
buildQuery=postgraphileBuildQuery
}) => {
const httpLink = createHttpLink(apolloHttpLinkOptions);
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache()
})
return buildGraphQLProvider({
client,
buildQuery,
introspection
}).then(defaultGraphQLProvider => (raFetchType, resourceName, params) => {
// https://github.com/marmelab/react-admin/blob/4cf148571b9ec80493bca6979b2825ab2dd5e603/packages/ra-data-graphcool/src/index.js#L39
if (raFetchType === 'GET_MANY') {
return Promise.all(
params.ids.map(id =>
defaultGraphQLProvider('GET_ONE', resourceName, { id }))
).then(results => ({
data: results.reduce(
(results, result) => ([...results, result.data]),
[])
})
)
}
return defaultGraphQLProvider(raFetchType, resourceName, params)
})
}
module.exports = buildPostgraphileProvider
module.exports.buildQuery = postgraphileBuildQuery