๐Ÿ“ฆ flaviendelangle / expense-manager

๐Ÿ“„ App.ts ยท 70 lines
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
70import cors from '@koa/cors'
import { ApolloServer } from 'apollo-server-koa'
import { Server } from 'http'
import Knex from 'knex'
import Koa from 'koa'
import { Model } from 'objection'

import knexFile from '../knexfile'

import { RequestContext } from './globalTypes'
import { schema } from './middlewares/graphql'
import { DataLoaderService } from './utils/DataLoaderService'

const HTTP_PORT = 3001
const PLAYGROUND_URL = `http://localhost:${HTTP_PORT}/graphql`

export class App {
  private readonly koa: Koa
  private readonly apollo: ApolloServer
  private server: Server

  private corsOptions: cors.Options = {
    origin: (ctx) =>
      new RegExp('http://localhost:.*').test(ctx.header.origin)
        ? ctx.header.origin
        : '',
    credentials: true,
  }

  constructor() {
    this.apollo = new ApolloServer({
      schema,
      tracing: true,
      debug: true,
      introspection: true,
      playground: {
        settings: {
          'editor.cursorShape': 'line',
          'request.credentials': 'same-origin',
        },
        endpoint: '/graphql',
      },
      context: () => {
        const c: RequestContext = {}

        c.loaders = Object.freeze(new DataLoaderService(c))

        return c
      },
    })

    this.koa = new Koa()

    this.koa.use(cors(this.corsOptions))

    const knex = Knex(knexFile)

    Model.knex(knex)

    this.apollo.applyMiddleware({ app: this.koa })

    this.server = this.koa.listen({ port: HTTP_PORT }, () => {
      // eslint-disable-next-line
      console.log(`GraphQL Server ready at ${PLAYGROUND_URL}`)
    })
  }

  async test() {}
}