forked from jscomplete/learning-graphql-and-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (33 loc) · 1.04 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
const fs = require('fs');
const path = require('path');
const { MongoClient } = require('mongodb');
const assert = require('assert');
const { graphql } = require('graphql');
const { introspectionQuery } = require('graphql/utilities');
const graphqlHTTP = require('express-graphql');
const express = require('express');
const app = express();
app.use(express.static('public'));
const mySchema = require('./schema/main');
const MONGO_URL = 'mongodb://localhost:27017/test';
MongoClient.connect(MONGO_URL, (err, db) => {
assert.equal(null, err);
console.log('Connected to MongoDB server');
app.use('/graphql', graphqlHTTP({
schema: mySchema,
context: { db },
graphiql: true
}));
graphql(mySchema, introspectionQuery)
.then(result => {
fs.writeFileSync(
path.join(__dirname, 'cache/schema.json'),
JSON.stringify(result, null, 2)
);
console.log('Generated cached schema.json file');
})
.catch(console.error);
app.listen(3000, () =>
console.log('Running Express.js on port 3000')
);
});