forked from graphql-compose/graphql-compose-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (44 loc) · 1.35 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
/* @flow */
import express from 'express';
import cors from 'cors';
import graphqlHTTP from 'express-graphql';
import expressPlayground from 'graphql-playground-middleware-express';
import { mainPage, addToMainPage } from './mainPage';
import { expressPort, getExampleNames, resolveExamplePath } from './config';
import './mongooseConnection';
const server = express();
server.use(cors());
// scan `examples` directory and add
// - graphql endpoint by uri /exampleDirName
// - links and example queries to index page
const exampleNames = getExampleNames();
for (const name of exampleNames) {
addExample(
// $FlowFixMe
require(resolveExamplePath(name)).default,
name
);
}
// $FlowFixMe
server.get('/', (req, res) => {
res.send(mainPage());
});
server.listen(expressPort, () => {
console.log(`The server is running at http://localhost:${expressPort}/`);
});
function addExample(example, uri) {
example.uri = `/${uri}`; // eslint-disable-line
server.use(
example.uri,
graphqlHTTP(() => ({
schema: example.schema,
graphiql: true,
customFormatErrorFn: error => ({
message: error.message,
stack: !error.message.match(/for security reason/i) ? error.stack.split('\n') : null,
}),
}))
);
server.get(`${example.uri}-playground`, expressPlayground({ endpoint: example.uri }));
addToMainPage(example);
}