-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathserver.js
37 lines (30 loc) · 936 Bytes
/
server.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
import fs from 'fs';
import express from 'express';
import Schema from './data/schema';
import GraphQLHTTP from 'express-graphql';
import {graphql} from 'graphql';
import {introspectionQuery} from 'graphql/utilities';
import {MongoClient} from 'mongodb';
let app = express();
app.use(express.static('public'));
(async () => {
try {
let db = await MongoClient.connect(process.env.MONGO_URL);
let schema = Schema(db);
app.use('/graphql', GraphQLHTTP({
schema,
graphiql: true
}));
let server = app.listen(process.env.PORT || 3000, () => {
console.log(`Listening on port ${server.address().port}`);
});
// Generate schema.json
let json = await graphql(schema, introspectionQuery);
fs.writeFile('./data/schema.json', JSON.stringify(json, null, 2), err => {
if (err) throw err;
console.log("JSON schema created");
});
} catch(e) {
console.log(e);
}
})();