# All files / utils/services/bootstrap bootstrap.service.ts

17.24% Statements 10/58
0% Branches 0/2
0% Functions 0/16
17.24% Lines 10/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    923x
    3x

    3x
    3x
    3x
    3x
    3x
    3x
    3x

    3x

     import Container, { Service } from '../../../utils/container/index';
    import { ControllerContainerService } from '../../services/controller-service/controller.service';
    import { ServerUtilService } from '../../services/server/server.service';
    import { GraphQLSchema, GraphQLObjectType } from 'graphql';
    import { ConfigService } from '../../services/config/config.service';
    import { SchemaService } from '../../services/schema/schema.service';
    import { GapiServerModule } from '../../../modules/server/server.module';
    import { HookService } from '../../services/hook/hook.service';
    import { controllerHooks } from '../controller-service/controller-hooks';
    import { ModuleContainerService } from '../module/module.service';

    async function getAllFields() {
        const controllerContainerService = Container.get(ControllerContainerService);
        const moduleContainerService = Container.get(ModuleContainerService);
        return new Promise((resolve, reject) => {
            Array.from(moduleContainerService.modules.keys()).forEach(module => {
                const currentModule = moduleContainerService.getModule(module);
                currentModule.resolveDependencyHandlers();
            });

            const Fields = { query: {}, mutation: {}, subscription: {} };
            Array.from(controllerContainerService.controllers.keys())
                .forEach(controller => {
                    const currentCtrl = controllerContainerService.getController(controller);
                    currentCtrl.getAllDescriptors().forEach(descriptor => {
                        const desc = currentCtrl.getDescriptor(descriptor).value();
                        Fields[desc.method_type][desc.method_name] = desc;
                        const c = controllerHooks.getHook(controller);
                        const originalResolve = desc.resolve.bind(c);
                        desc.resolve = function resolve(...args: any[]) {
                            return originalResolve.apply(c, args);
                        };
                    });
                });
            function generateType(query, name, description) {
                if (!Object.keys(query).length) {
                    return;
                }
                return new GraphQLObjectType({
                    name: name,
                    description: description,
                    fields: query
                });
            }
            const query = generateType(Fields.query, 'Query', 'Query type for all get requests which will not change persistent data');
            const mutation = generateType(Fields.mutation, 'Mutation', 'Mutation type for all requests which will change persistent data');
            const subscription = generateType(Fields.subscription, 'Subscription', 'Subscription type for all rabbitmq subscriptions via pub sub');
            HookService.AttachHooks([query, mutation, subscription ]);
            const schema = Container.get(SchemaService).generateSchema(query, mutation, subscription);
            resolve(schema);
        });
    }

    function onExitProcess(server: GapiServerModule) {
        process.on(<any>'cleanup', () => {
            console.log('App stopped');
            server.stop();
        });
        process.on('exit', function () {
            process.emit(<any>'cleanup');
        });
        process.on('SIGINT', function () {
            process.exit(2);
        });
        process.on('uncaughtException', function (e) {
            console.log(e.stack);
            process.exit(99);
        });
    }

    export const Bootstrap = (App) => {
        console.log(`Bootstrapping application...`);
        Object.defineProperty(App, 'name', { value: 'AppModule', writable: true });
        Container.get(App);
        console.log('Finished!\nStarting application...');
        getAllFields()
            .then((schema: GraphQLSchema) => {
                const configService = Container.get(ConfigService);
                configService.APP_CONFIG.schema = schema;
                const server = Container.get(GapiServerModule.forRoot(configService.APP_CONFIG));
                server.start()
                .then((data) => {
                    onExitProcess(server);
                    console.log('Application started!');
                })
                .catch(e => console.log(e));
            });

        return App;
    };