-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (51 loc) · 1.52 KB
/
index.js
File metadata and controls
59 lines (51 loc) · 1.52 KB
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
require('dotenv').config();
const fs = require('fs');
const https = require('https');
const http = require('http');
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { schema } = require('./schemas/');
const { authors, books } = require('./db');
const configurations = {
// Note: You may need sudo to run on port 443
production: { ssl: true, port: 443, hostname: 'example.com' },
development: { ssl: false, port: 4000, hostname: 'localhost' }
};
const environment = process.env.NODE_ENV || 'development';
const config = configurations[environment];
const apollo = new ApolloServer({
schema,
// here we can attach things like dbs so that resolvers can use them
context: ({ req }) => ({
authors,
books
})
// mockEntireSchema: true
});
const app = express();
apollo.applyMiddleware({ app });
// Create the HTTPS or HTTP server, per configuration
let server;
if (config.ssl) {
// Assumes certificates are in .ssl folder from package root. Make sure
// the files are secured.
server = https.createServer(
{
key: fs.readFileSync(`./ssl/${environment}/server.key`),
cert: fs.readFileSync(`./ssl/${environment}/server.crt`)
},
app
);
} else {
server = http.createServer(app);
}
// Add subscription support
apollo.installSubscriptionHandlers(server);
server.listen({ port: config.port }, () =>
console.log(
'🚀 Server ready at',
`http${config.ssl ? 's' : ''}://${config.hostname}:${config.port}${
apollo.graphqlPath
}`
)
);