Skip to content

Commit

Permalink
feat: refactor gateway to nestjs
Browse files Browse the repository at this point in the history
  • Loading branch information
temarusanov committed Jun 29, 2023
1 parent 6af1b01 commit d76144e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 41 deletions.
38 changes: 38 additions & 0 deletions apps/gateway/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Module } from '@nestjs/common'
import { EventloopFrozenDetectorModule } from 'core/eventloop-frozen-detector'
import { GraphQLModule } from '@nestjs/graphql'
import * as env from 'env-var'
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default'
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo'
import { IntrospectAndCompose } from '@apollo/gateway'

@Module({
imports: [
EventloopFrozenDetectorModule.forRoot({
delay: 3000,
}),
GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
driver: ApolloGatewayDriver,
server: {
playground: false,
plugins: [ApolloServerPluginLandingPageLocalDefault()],
},
gateway: {
supergraphSdl: new IntrospectAndCompose({
subgraphs: env
.get('GATEWAY_SUBGRAPHS')
.required()
.asJsonArray() as {
name: string
url: string
}[],
subgraphHealthCheck: true,
pollIntervalInMs: 10000,
}),
},
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
77 changes: 36 additions & 41 deletions apps/gateway/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Logger } from '@nestjs/common'
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { ApolloGateway, IntrospectAndCompose } from '@apollo/gateway'
import env from 'env-var'
import { INestApplication, Logger } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app/app.module'
import { SpelunkerModule } from 'nestjs-spelunker'

const logger = new Logger('Application')

//do something when app is closing
process.on('exit', exitHandler.bind(null, { cleanup: true }))
async function bootstrap() {
const app = await NestFactory.create(AppModule)

//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, { exit: true }))
// const diGraph = buildDIGraph(app)

// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }))

//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))
const port = env.get('GATEWAY_PORT').default(3052).asPortNumber()

import env from 'env-var'
await app.listen(port, () => {
logger.log(`🚀 Gateway is running on: http://localhost:${port}/graphql`)
// logger.debug(`DI Graph tree\n${diGraph}`)
})
}

//do something when app is closing
process.on('exit', exitHandler.bind(null, { cleanup: true }))
Expand All @@ -32,32 +32,6 @@ process.on('SIGUSR2', exitHandler.bind(null, { exit: true }))
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))

async function bootstrap() {
const port = env.get('GATEWAY_PORT').default(3052).asPortNumber()
const subgraphs = env.get('GATEWAY_SUBGRAPHS').required().asJsonArray() as {
name: string
url: string
}[]

const server = new ApolloServer({
gateway: new ApolloGateway({
supergraphSdl: new IntrospectAndCompose({
subgraphs,
subgraphHealthCheck: true,
pollIntervalInMs: 10000,
}),
}),
})

const { url } = await startStandaloneServer(server, {
listen: {
port,
},
})

logger.log(`Gateway ready at ${url}`)
}

try {
bootstrap().catch((err) => {
logger.error(err, err.stack)
Expand All @@ -81,3 +55,24 @@ function exitHandler(options, exitCode) {
process.exit()
}
}

// Copy output to mermaid.live
function buildDIGraph(app: INestApplication) {
const tree = SpelunkerModule.explore(app)
const root = SpelunkerModule.graph(tree)
const edges = SpelunkerModule.findGraphEdges(root)

const mermaidEdges = edges
.filter(
({ from, to }) =>
!(
from.module.name === 'ConfigHostModule' ||
from.module.name === 'LoggerModule' ||
to.module.name === 'ConfigHostModule' ||
to.module.name === 'LoggerModule'
),
)
.map(({ from, to }) => `${from.module.name}-->${to.module.name}`)

return `graph TD\n\t${mermaidEdges.join('\n\t')}`
}

0 comments on commit d76144e

Please sign in to comment.