-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
46 lines (30 loc) · 1.02 KB
/
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
38
39
40
41
import express from 'express';
import Schema from './data/schema';
import GraphQLHTTP from 'express-graphql';
import { graphql } from 'graphql';
import { introspectionQuery , printSchema} from 'graphql/utilities';
import fs from 'fs';
import {MongoClient} from 'mongodb';
let app = express();
app.use(express.static('public'));
// mongo sets up local db by the name you pass to it.
(async () => {
// pass db to the graphql schema
try {
let db = await MongoClient.connect('mongodb://localhost:27017/graphql_tutorial');
let schema = Schema(db);
app.use('/graphql', GraphQLHTTP({
schema,
graphiql: true
}))
app.listen(5000 , () => console.log('You are live on server 5000'));
// 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);
}
})();