forked from ZeroBoundLabs/impact-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
159 lines (141 loc) · 4.54 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import 'reflect-metadata'
// import { ApolloServer } from 'apollo-server'
import { ApolloServer } from 'apollo-server-express'
import { Container } from 'typedi'
import * as TypeORM from 'typeorm'
import * as TypeGraphQL from 'type-graphql'
import { User } from './entities/user'
import { BankAccount, StripeTransaction } from './entities/bankAccount'
import { Project, Category } from './entities/project'
import { seedDatabase } from './helpers'
import { Organisation } from './entities/organisation'
import { OrganisationUser } from './entities/organisationUser'
import Notification from './entities/notification'
// import { OrganisationProject } from './entities/organisationProject'
import { UserResolver } from './resolvers/userResolver'
import { ProjectResolver } from './resolvers/projectResolver'
import { BankAccountResolver } from './resolvers/bankAccountResolver'
import { RegisterResolver } from './user/register/RegisterResolver'
import { LoginResolver } from './user/LoginResolver'
import { OrganisationResolver } from './resolvers/organisationResolver'
import { NotificationResolver } from './resolvers/notificationResolver'
import { ConfirmUserResolver } from './user/ConfirmUserResolver'
import { MeResolver } from './user/MeResolver'
import { userCheck } from './auth/userCheck'
import * as jwt from 'jsonwebtoken'
import * as dotenv from 'dotenv'
import Config from './config'
import { handleStripeWebhook } from './utils/stripe'
dotenv.config()
const config = new Config(process.env)
const express = require("express")
const bodyParser = require("body-parser")
const cors = require('cors')
// register 3rd party IOC container
TypeORM.useContainer(Container)
const entities: any = [
Organisation,
OrganisationUser,
User,
Project,
Notification,
BankAccount,
StripeTransaction,
Category,
]
const resolvers: any = [
UserResolver,
ProjectResolver,
OrganisationResolver,
NotificationResolver,
LoginResolver,
RegisterResolver,
MeResolver,
BankAccountResolver
]
if (process.env.REGISTER_USERNAME_PASSWORD === 'true') {
resolvers.push.apply(resolvers, [RegisterResolver, ConfirmUserResolver])
}
async function bootstrap () {
try {
// create TypeORM connection
const dropSeed = config.get('DB_DROP_SEED') == 'true'
await TypeORM.createConnection({
type: 'postgres',
database: process.env.TYPEORM_DATABASE_NAME,
username: process.env.TYPEORM_DATABASE_USER,
password: process.env.TYPEORM_DATABASE_PASSWORD,
port: Number(process.env.PORT),
host: process.env.TYPEORM_DATABASE_HOST,
entities,
synchronize: true,
logger: 'advanced-console',
logging: 'all',
dropSchema: dropSeed,
cache: true
})
if (dropSeed) {
// seed database with some data
const { defaultUser } = await seedDatabase()
}
// build TypeGraphQL executable schema
const schema = await TypeGraphQL.buildSchema({
resolvers,
container: Container,
authChecker: userCheck
})
// Create GraphQL server
const apolloServer = new ApolloServer({
schema,
context: ({ req, res }: any) => {
try {
if (!req) {
return null
}
if (req.headers.authorization) {
const token = req.headers.authorization.split(' ')[1].toString()
const secret = config.get('JWT_SECRET')
const decodedJwt: any = jwt.verify(token, secret)
let user
if (decodedJwt.nextAuth) {
user = {
email: decodedJwt?.nextauth?.user?.email,
name: decodedJwt?.nextauth?.user?.name
}
} else {
user = {
email: decodedJwt?.email,
name: decodedJwt?.firstName,
userId: decodedJwt?.userId
}
}
req.user = user
}
} catch (error) {
console.error(
`Apollo Server error : ${JSON.stringify(error, null, 2)}`
)
}
return {
req,
res
}
},
engine: {
reportSchema: true
},
playground: true
})
// Express Server
const app = express();
app.use(cors())
apolloServer.applyMiddleware({ app });
app.post('/stripe-webhook', bodyParser.raw({ type: "application/json" }), handleStripeWebhook);
// Start the server
app.listen({ port: 4000 })
console.log(`🚀 Server is running, GraphQL Playground available at http://127.0.0.1:${4000}`)
} catch (err) {
console.error(err)
}
}
bootstrap()