-
Notifications
You must be signed in to change notification settings - Fork 53
/
prepareData.js
40 lines (32 loc) · 1.4 KB
/
prepareData.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
import Relay from 'react-relay';
import RelayQuery from 'react-relay/lib/RelayQuery';
import RelayStoreData from 'react-relay/lib/RelayStoreData';
import toGraphQL from 'react-relay/lib/toGraphQL';
// HACK: Do not memoize concrete node produced by toGraphQL,
// because it changes RelayQuery.Fragment.getConcreteFragmentID()
// whereas RelayQuery is supposed to be immutable.
// TODO: remove when it is fixed in Relay
RelayQuery.Node.prototype.getConcreteQueryNode = onCacheMiss => onCacheMiss();
const globalStoreData = RelayStoreData.getDefaultInstance();
export default function prepareData({Component, route}) {
return new Promise((resolve, reject) => {
const data = [];
const storeData = new class extends RelayStoreData {
handleQueryPayload(query, result, forceIndex) {
data.push({query: toGraphQL.Query(query), result});
globalStoreData.handleQueryPayload(query, result, forceIndex);
}
};
const querySet = Relay.getQueries(Component, route);
storeData.getQueryRunner().forceFetch(querySet, onReadyStateChange);
function onReadyStateChange({aborted, done, error, stale}) {
if (error) {
reject(error);
} else if (aborted) {
reject(new Error('Aborted'));
} else if (done && !stale) {
resolve(data);
}
}
});
}