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

23.4% Statements 11/47
0% Branches 0/32
0% Functions 0/11
21.74% Lines 10/46

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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    1283x

    3x
    3x

    3x
    3x

    3x

    3x

    3x

    3x

    3x
     import * as Boom from 'boom';
    import { Server, Response, Request, ReplyNoContinue } from 'hapi';
    import * as GraphiQL from 'apollo-server-module-graphiql';
    import { runHttpQuery, HttpQueryError } from 'apollo-server-core';
    import { ServerUtilService } from '../server/server.service';
    import { Container, Service } from '../../../utils/container/index';
    import { AuthService } from '../../services/auth/auth.service';

    export interface IRegister {
      (server: Server, options: any, next: any): void;
      attributes?: any;
    }
    export type HapiOptionsFunction = (req?: Request) => any | Promise<any>;

    export interface HapiPluginOptions {
      path: string;
      route?: any;
      graphqlOptions: any | HapiOptionsFunction;
    }

    function runHttpQueryWrapper(options: any | HapiOptionsFunction, request: Request, reply: ReplyNoContinue): Promise<Response> {
      return runHttpQuery([request], {
        method: request.method.toUpperCase(),
        options: options,
        query: request.method === 'post' ? request.payload : request.query,
      }).then((gqlResponse) => {
        return reply(gqlResponse).type('application/json');
      }, (error: HttpQueryError) => {
        if ('HttpQueryError' !== error.name) {
          throw error;
        }

        if (true === error.isGraphQLError) {
          return reply(error.message).code(error.statusCode).type('application/json');
        }

        const err = Boom.create(error.statusCode);
        err.output.payload.message = error.message;
        if (error.headers) {
          Object.keys(error.headers).forEach((header) => {
            err.output.headers[header] = error.headers[header];
          });
        }

        return reply(err);
      });
    }

    const graphqlHapi: IRegister = function (server: Server, options: HapiPluginOptions, next) {
      if (!options || !options.graphqlOptions) {
        throw new Error('Apollo Server requires options.');
      }

      if (arguments.length !== 3) {
        throw new Error(`Apollo Server expects exactly 3 argument, got ${arguments.length}`);
      }

      server.route({
        method: ['GET', 'POST'],
        path: options.path || '/graphql',
        config: options.route || {},
        handler: async (request, reply) => {
          if (request.headers.authorization && request.headers.authorization !== 'undefined') {
            try {

              const serviceUtilsService: AuthService = Container.get(AuthService);

              options.graphqlOptions.context = await serviceUtilsService.modifyFunctions.validateToken(request.headers.authorization);

            } catch (e) {
              return reply(Boom.unauthorized());
            }
          } else {
            options.graphqlOptions.context = null;
          }
          return runHttpQueryWrapper(options.graphqlOptions, request, reply);
        },
      });

      return next();
    };

    graphqlHapi.attributes = {
      name: 'graphql',
      version: '0.0.1',
    };

    export type HapiGraphiQLOptionsFunction = (req?: Request) => GraphiQL.GraphiQLData | Promise<GraphiQL.GraphiQLData>;

    export interface HapiGraphiQLPluginOptions {
      path: string;
      route?: any;
      graphiqlOptions: GraphiQL.GraphiQLData | HapiGraphiQLOptionsFunction;
    }

    const graphiqlHapi: IRegister = function (server: Server, options: HapiGraphiQLPluginOptions, next) {
      if (!options || !options.graphiqlOptions) {
        throw new Error('Apollo Server GraphiQL requires options.');
      }

      if (arguments.length !== 3) {
        throw new Error(`Apollo Server GraphiQL expects exactly 3 arguments, got ${arguments.length}`);
      }

      server.route({
        method: 'GET',
        path: options.path || '/graphiql',
        config: options.route || {},
        handler: (request, reply) => {
          const query = request.query;
          GraphiQL.resolveGraphiQLString(query, options.graphiqlOptions, request)
            .then(graphiqlString => {
              reply(graphiqlString).header('Content-Type', 'text/html');
            }, error => reply(error));
        },
      });

      return next();
    };

    graphiqlHapi.attributes = {
      name: 'graphiql',
      version: '0.0.1',
    };

    export { graphqlHapi, graphiqlHapi };