Newer
Older
import { HttpModule, HttpService } from '@nestjs/axios';
import { DynamicModule, Global, Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { ENGINE_MODULE_OPTIONS, ENGINE_SERVICE } from './engine.constants';
import { EngineController } from './engine.controller';
import { IEngineOptions, IEngineService } from './engine.interfaces';
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
import { EngineResolver } from './engine.resolver';
@Global()
@Module({})
export class EngineModule {
static async forRootAsync(options: IEngineOptions): Promise<DynamicModule> {
const optionsProvider = {
provide: ENGINE_MODULE_OPTIONS,
useValue: options,
};
const engineProvider = {
provide: ENGINE_SERVICE,
useFactory: async (httpService: HttpService) => {
return await this.createEngineConnection(options, httpService)
},
inject: [HttpService]
};
return {
module: EngineModule,
imports: [
HttpModule,
GraphQLModule.forRoot({
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
}),
],
providers: [
optionsProvider,
engineProvider,
EngineResolver
],
controllers: [
EngineController
],
exports: [optionsProvider, engineProvider],
}
}
private static async createEngineConnection(options: IEngineOptions, httpService: HttpService): Promise<IEngineService> {
let service = await import(`./connectors/${options.type}/main.connector`);
return new service.default(options, httpService);
}
}