-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
85 lines (77 loc) · 2.01 KB
/
index.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
import Bugsnag from '@bugsnag/js'
import { ApolloServer } from 'apollo-server'
import dotenv from 'dotenv'
import jwt from 'jsonwebtoken'
import nodemailer from 'nodemailer'
import { schema, models } from './data/schema.js'
import { connectDB } from './lib/mongoose.js'
dotenv.config({ path: `.env.${process.env.NODE_ENV}` })
// Configure BugSnag monitoring.
Bugsnag.start({
appVersion: '1.2.2',
apiKey: process.env.BUGSNAG_SERVER_API_KEY,
enabledReleaseStages: ['production'],
environment: process.env.NODE_ENV,
})
const bugsnagPlugin = () => ({
requestDidStart: async () => ({
async didEncounterErrors(requestContext) {
requestContext.errors.forEach((error) => {
if (
!['BAD_USER_INPUT', 'UNAUTHENTICATED'].includes(
error.extensions?.code
)
) {
Bugsnag.notify(error, (event) => {
event.addMetadata('GraphQLMetadata', {
path: error.path,
})
})
}
})
},
}),
})
// Initialise SMTP transport.
const smtpTransport = nodemailer.createTransport({
host: process.env.SMTP_SERVER,
port: 587,
secure: false,
ignoreTLS: true,
auth: {
user: process.env.SMTP_SERVER_USER,
pass: process.env.SMTP_SERVER_PASSWORD,
},
})
// Connect to MongoDB.
await connectDB(process.env.MONGODB_URI)
// Extract the user from the token for authentication.
const getUser = function (token) {
if (token) {
try {
token = token.split(' ')[1]
return jwt.verify(token, process.env.JWT_ACCESS_TOKEN_SECRET)
} catch (err) {
return null
}
}
}
// Create and configure the Apollo server.
const server = new ApolloServer({
schema,
context: ({ req }) => {
const token = req?.headers?.authorization || ''
const user = getUser(token)
return {
env: process.env,
models,
smtpTransport,
user,
}
},
plugins: [bugsnagPlugin],
})
// Launch the apollo server.
server.listen({ port: 4001 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})