-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.ts
76 lines (69 loc) · 2.6 KB
/
index.ts
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
import * as apolloServerPlugins from './apollo_server_plugins'
import express from 'express'
import fs from 'fs'
import { getConfig } from './config'
import { Server } from './Server'
import depthLimit from 'graphql-depth-limit'
import { mergeSchemas } from '@graphql-tools/merge'
import { PluginDefinition } from 'apollo-server-core'
import { buildSchema as buildCardanoDbHasuraSchema, Db } from '@cardano-graphql/api-cardano-db-hasura'
import { buildSchema as buildGenesisSchema } from '@cardano-graphql/api-genesis'
import { GraphQLSchema } from 'graphql'
export * from './config'
export { apolloServerPlugins }
const { prometheusMetricsPlugin, allowListPlugin } = apolloServerPlugins
async function boot () {
const config = await getConfig()
const schemas: GraphQLSchema[] = []
const validationRules = []
const plugins: PluginDefinition[] = []
const app = express()
if (config.genesisFileByron !== undefined || config.genesisFileShelley !== undefined) {
schemas.push(buildGenesisSchema({
...config.genesisFileByron !== undefined ? { byron: require(config.genesisFileByron) } : {},
...config.genesisFileShelley !== undefined ? { shelley: require(config.genesisFileShelley) } : {}
}))
}
if (config.hasuraUri !== undefined) {
const db = new Db(config.hasuraUri)
await db.init()
schemas.push(await buildCardanoDbHasuraSchema(config.hasuraUri, db))
}
if (config.prometheusMetrics) {
plugins.push(prometheusMetricsPlugin(app))
}
if (config.allowListPath) {
const allowList = JSON.parse(fs.readFileSync(config.allowListPath, 'utf8'))
plugins.push(allowListPlugin(allowList))
}
if (config.queryDepthLimit) {
validationRules.push(depthLimit(config.queryDepthLimit))
}
const server = Server(app, {
cacheControl: config.cacheEnabled ? { defaultMaxAge: 20 } : undefined,
introspection: config.allowIntrospection,
playground: config.allowIntrospection,
plugins,
validationRules,
schema: mergeSchemas({
schemas
})
}, {
origin: config.allowedOrigins
})
try {
app.listen({ port: config.apiPort }, () => {
const serverUri = `http://localhost:${config.apiPort}`
console.log(`GraphQL HTTP server at ${serverUri}${server.graphqlPath}`)
if (process.env.NODE_ENV !== 'production' && config.allowListPath) {
console.warn('As an allow-list is in effect, the GraphQL Playground is available, but will not allow schema exploration')
}
if (config.prometheusMetrics) {
console.log(`Prometheus metrics at ${serverUri}/metrics`)
}
})
} catch (error) {
console.error(error.message)
}
}
boot()