Skip to content

Commit

Permalink
feat: implemented unit tests for express and fastify graphql apis
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Jul 7, 2022
1 parent fda262a commit 601b96c
Show file tree
Hide file tree
Showing 16 changed files with 695 additions and 453 deletions.
70 changes: 21 additions & 49 deletions example/express-graphql/src/network/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createServer, Server as HttpServer } from 'http'
import express from 'express'
import mongoose from 'mongoose'
import cors from 'cors'
import pino, { HttpLogger } from 'express-pino-logger'
import { ApolloServer } from 'apollo-server-express'
Expand All @@ -10,31 +9,34 @@ import {
ApolloServerPluginLandingPageGraphQLPlayground
} from 'apollo-server-core'

import { dbConnection } from 'database'
import { mergedSchema as schema } from 'graphQL'
import { applyRoutes } from './router'

const PORT = (process.env.PORT as string) || 1996
const ENVIRONMENTS_WITHOUT_PRETTY_PRINT = ['production', 'ci']

class Server {
#app: express.Application
#connection: mongoose.Connection | undefined
#server: HttpServer
#log: HttpLogger
#server: HttpServer
#connection: Awaited<ReturnType<typeof dbConnection>> | undefined

constructor() {
this.#app = express()
this.#server = createServer(this.#app)
this.#log = pino({
transport:
process.env.ENVIRONMENT !== 'production'
? {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
transport: !ENVIRONMENTS_WITHOUT_PRETTY_PRINT.includes(
process.env.NODE_ENV as string
)
? {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
: undefined
}
: undefined
})
this.#config()
}
Expand Down Expand Up @@ -62,38 +64,8 @@ class Server {
)
}

async #mongo(): Promise<void> {
this.#connection = mongoose.connection
const connection = {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
}
this.#connection.on('connected', () => {
this.#log.logger.info('Mongo connection established.')
})
this.#connection.on('reconnected', () => {
this.#log.logger.info('Mongo connection reestablished')
})
this.#connection.on('disconnected', () => {
this.#log.logger.info('Mongo connection disconnected')
this.#log.logger.info('Trying to reconnected to Mongo...')
setTimeout(() => {
mongoose.connect(process.env.MONGO_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
})
}, 3000)
})
this.#connection.on('close', () => {
this.#log.logger.info('Mongo connection closed')
})
this.#connection.on('error', (e: Error) => {
this.#log.logger.info('Mongo connection error:')
this.#log.logger.error(e)
})
await mongoose.connect(process.env.MONGO_URI as string, connection)
async #dbConnection() {
this.#connection = await dbConnection(this.#log.logger)
}

public async start(): Promise<void> {
Expand All @@ -117,7 +89,8 @@ class Server {
path: '/api'
})
applyRoutes(this.#app)
await this.#mongo()
await this.#dbConnection()
await this.#connection?.connect()
this.#server.listen(PORT, () => {
this.#log.logger.info(`Server listening at port: ${PORT}`)
this.#log.logger.info(
Expand All @@ -129,11 +102,10 @@ class Server {
}
}

public stop(): void {
public async stop(): Promise<void> {
try {
if (this.#server) this.#server.close()

process.exit(0)
await this.#connection?.disconnect()
this.#server?.close()
} catch (e) {
this.#log.logger.error(e)
}
Expand Down
Loading

0 comments on commit 601b96c

Please sign in to comment.