-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
executable file
·91 lines (79 loc) · 2.48 KB
/
app.js
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
require(`@babel/register`)({
cwd: __dirname,
plugins: [`@babel/plugin-transform-modules-commonjs`],
only: [
`./lib/*`
]
})
require(`./lib/cycle.js`)
const substruct = require(`@internalfx/substruct`)
const { ApolloServer, AuthenticationError, UserInputError } = require(`apollo-server-koa`)
const { typeDefs, resolvers } = require(`./graphql/index.js`)
const path = require(`path`)
const numeral = require(`numeral`)
const configPath = path.join(process.cwd(), `config.js`)
const userConfig = require(configPath)
substruct.configure({
...userConfig,
runDir: process.cwd(),
appDir: __dirname
})
const main = async function () {
const apollo = new ApolloServer({
typeDefs,
resolvers,
formatError: function (error) {
const data = JSON.decycle(error)
console.log(`================================================================== GRAPHQL ERROR`)
console.dir(data, { colors: true, depth: null })
console.log(`================================================================================`)
return data
},
context: async function ({ ctx }) {
const session = ctx.state.session
const { arango, aql, getNumber } = substruct.services.arango
const afs = substruct.services.arangofs
const utils = substruct.services.utils
const config = substruct.config
const user = await arango.qNext(aql`
for u in users
filter u._key == ${session.userKey || null}
return u
`)
if (user == null) {
throw new AuthenticationError(`You are not logged in`)
}
const userInputError = function (message, data) {
throw new UserInputError(message, data)
}
return {
session,
arango,
aql,
getNumber,
afs,
utils,
config,
user,
services: substruct.services,
userInputError
}
}
})
await substruct.load()
await substruct.start()
apollo.applyMiddleware({ app: substruct.koa, path: `/api/graphql` })
console.log(`Server Started...`)
// setInterval(function () {
// const stats = {
// rss: numeral(process.memoryUsage().rss).format('0.00b'),
// heapTotal: numeral(process.memoryUsage().heapTotal).format('0.00b'),
// heapUsed: numeral(process.memoryUsage().heapUsed).format('0.00b'),
// external: numeral(process.memoryUsage().external).format('0.00b')
// }
// console.log(stats)
// }, 600000)
}
main().catch(function (err) {
console.log(err)
})