-
Notifications
You must be signed in to change notification settings - Fork 53
/
loadAndStoreData.js
37 lines (33 loc) · 1.07 KB
/
loadAndStoreData.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
import Relay from 'react-relay';
import {
queryResults,
wasInjected,
} from './InterceptingNetworkLayer';
export default function({Component, route}) {
if (!wasInjected()) {
throw new Error('NetworkLayer was not injected!');
}
return new Promise((resolve, reject) => {
const querySet = Relay.getQueries(Component, route);
Relay.Store.forceFetch(querySet, ({aborted, done, error, stale}) => {
if (error) {
reject(error);
} else if (aborted) {
reject(new Error('Aborted'));
} else if (done && !stale) {
resolve(getQueryResults());
}
});
function getQueryResults() {
const results = {};
Object.keys(querySet).forEach(name => {
const result = queryResults.get(querySet[name]);
if (!result) {
throw new Error(`No result for query "${name}"`);
}
results[name] = result;
});
return results;
}
});
}