forked from graphql-compose/graphql-compose-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1.04 KB
/
Copy pathindex.js
File metadata and controls
43 lines (36 loc) · 1.04 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
import express from 'express';
import cors from 'cors';
import graphqlHTTP from 'express-graphql';
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 (let name of exampleNames) {
addExample(
require(resolveExamplePath(name)).default,
name
);
}
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}`;
server.use(example.uri, graphqlHTTP(req => ({
schema: example.schema,
graphiql: true,
formatError: (error) => ({
message: error.message,
stack: error.stack.split('\n'),
}),
})));
addToMainPage(example);
}