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

17.39% Statements 4/23
0% Branches 0/14
0% Functions 0/11
19.05% Lines 4/21

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
    443x
    3x
    3x

    3x

     import { createError } from '../error/error.service';
    import { Container } from '../../../utils/container/index';
    import { ConfigService } from '../../services/config/config.service';

    function MakeError() {
        throw new createError('unauthorized', 'You are unable to fetch data');
    }

    export class HookService {
        static AttachHooks(graphQLFields) {
            graphQLFields.forEach(type => {
                if (!type) {
                    return;
                }
                const resolvers = type.getFields();
                Object.keys(resolvers).forEach(resolver => {
                    resolvers[resolver].scope = resolvers[resolver].scope || [process.env.APP_DEFAULT_SCOPE || 'ADMIN'];
                    if (!resolvers[resolver].public) {
                        HookService.AddHooks(resolvers[resolver]);
                    }
                });
            });
        }
        static canAccess(routeScope, context) {
            return context && routeScope.filter(scope => scope === context.type).length ? true : MakeError();
        }
        static AuthenticationHooks(resolver, root, args, context, info) {
            HookService.canAccess(resolver.scope, context);
        }
        static ResolverHooks(resolver, root, args, context, info) {
            HookService.AuthenticationHooks(resolver, root, args, context, info);
        }
        static AddHooks(resolver) {
            if (Container.get(ConfigService).cert) {
                const resolve = resolver.resolve;
                resolver.resolve = async (root, args, context, info) => {
                    HookService.ResolverHooks(resolver, root, args, context, info);
                    return await resolve(root, args, context, info);
                };
            }
        }
    }