-
Notifications
You must be signed in to change notification settings - Fork 6
/
agent.ts
54 lines (41 loc) · 1.6 KB
/
agent.ts
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
import { Application, IBoot } from 'egg';
import * as merge from 'lodash.merge';
import * as clonedeep from 'lodash.clonedeep';
import { IApolloConfig } from '@gaoding/apollo-client/dist/interface/IApolloConfig';
import Apollo from './app/lib/apollo';
import * as path from 'path';
import loadTs from './lib/loadTs';
export default class FooBoot implements IBoot {
private app: Application & {apollo?: Apollo};
constructor(app: Application & {apollo?: Apollo}) {
this.app = app;
}
configWillLoad() {
const app = this.app;
const config: IApolloConfig = app.config.apollo;
if (config.init_on_start === false) {
return;
}
if (!app.apollo) {
app.apollo = new Apollo(app.config.apollo, app);
app.apollo.init();
const appConfig = this.app.config;
const apolloConfigPath = path.resolve(appConfig.baseDir, 'config/config.apollo.js');
try {
const apolloConfigFunc: Function = loadTs(apolloConfigPath).default || loadTs(apolloConfigPath);
const apolloConfig = apolloConfigFunc(app.apollo, clonedeep(app.config));
merge(app.config, apolloConfig);
app.apollo.emit('config.loaded');
return;
} catch (_) {
app.logger.warn('[egg-apollo-client] loader config/config.apollo.js error');
}
}
}
async willReady() {
const config: IApolloConfig = this.app.config.apollo;
if (config.watch) {
this.app.apollo.startNotification();
}
}
}