-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
33 lines (26 loc) · 912 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
import express from 'express';
import { graphql } from 'graphql';
import bodyParser from 'body-parser';
import schema from './schema';
const app = express();
app.use(bodyParser.text({ type: 'application/graphql' }));
app.get('/api', (req, res) => {
// Imagine different client side components
// requesting different pieces of data
let querys = [
"{ user { name }, books { author } }"
"{ books { id, title } }",
"{ books { id, title, author } }"
]
// Pick a random query to use
let query = querys[Math.floor(Math.random() * querys.length)];
// Pass the query to GraphQL and return the response
graphql(schema, query).then((result) => {
res.send(JSON.stringify(result));
});
});
const server = app.listen(4000, function () {
const host = server.address().address;
const port = server.address().port;
console.log('GraphQL listening at http://%s:%s', host, port);
});