-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (93 loc) · 2.69 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require('dotenv').config()
const { ApolloServer } = require('apollo-server');
const {
ApolloServerPluginLandingPageLocalDefault,
ApolloServerPluginLandingPageProductionDefault
} = require ('apollo-server-core');
const { connectionPool, DataSource } = require('./connection-pool');
const { typeDefs } = require('./schema');
const { resolvers } = require('./resolvers');
const bunyan = require('bunyan');
// Create a logger instance
const logger = bunyan.createLogger({
name: 'example-app',
level: 'info'
});
const PORT = process.env.PORT || 4000;
let knexConfig = {
client: "pg",
version: "13.3"
};
if (process.env.DATABASE_URL) {
knexConfig.connection = {
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
};
} else {
knexConfig.connection = {
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
port: process.env.PGPORT,
password: ''
}
}
const pg = new DataSource(knexConfig);
const loggingPlugin = {
async requestDidStart(requestContext) {
logger.info(`Processing started for operation ${requestContext.request.operationName}`);
return {
async parsingDidStart(requestContext) {
return async (err) => {
if (err) {
logger.error(err);
}
}
},
async validationDidStart(requestContext) {
// This end hook is unique in that it can receive an array of errors,
// which will contain every validation error that occurred.
return async (errs) => {
if (errs) {
errs.forEach(err => logger.error(err));
}
}
},
async didEncounterErrors(requestContext) {
logger.error(`Error while executing operation ${requestContext.request.operationName}`);
logger.error(`Msg: ${requestContext.errors[0].message}`);
logger.error(`Query String: ${requestContext.request.query}`);
},
async executionDidStart(requestContext) {
return {
async executionDidEnd(err) {
logger.info(`Execution completed for operation ${requestContext.request.operationName}`);
if (err) {
logger.error(err);
}
}
};
},
};
},
}
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({ pg }),
plugins: [
loggingPlugin,
process.env.NODE_ENV === "production"
? ApolloServerPluginLandingPageProductionDefault({
footer: false,
})
: ApolloServerPluginLandingPageLocalDefault({ embed: true })
],
cache: "bounded",
introspection: true
});
server.listen({
port: PORT
}).then(({ url }) => {
logger.info(`Server ready at ${url}`);
});