-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (97 loc) · 2.25 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// https://graphql.org/graphql-js/running-an-express-graphql-server/
const express = require('express');
const { createHandler } = require('graphql-http/lib/use/express');
const { buildSchema } = require('graphql');
const { ruruHTML } = require('ruru/server');
const schema = buildSchema(`
type RandomDie {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!): [Int]
}
type Mutation {
setMessage(message: String): String
}
type Query {
hello: String
rollDice(numDice: Int!, numSides: Int): [Int]
quoteOfTheDay: String
random: Float!
rollThreeDice: [Int]
getDie(numSides: Int): RandomDie
getMessage: String
ip: String
}
`);
class RandomDie {
constructor(numSides) {
this.numSides = numSides;
}
rollOnce() {
return 1 + Math.floor(Math.random() * this.numSides);
}
roll({ numRolls }) {
const output = [];
for (let i = 0; i < numRolls; i++) {
output.push(this.rollOnce());
}
return output;
}
}
const fakeDatabase = {};
const loggingMiddleware = (req, _res, next) => {
console.log('ip:', req.ip);
next();
}
const rootValue = {
hello: () => {
return "Hello world!";
},
rollDice: ({ numDice, numSides }) => {
const output = [];
for (let i = 0; i < numDice; i++) {
output.push(1 + Math.floor(Math.random() * (numSides || 6)));
}
return output;
},
quoteOfTheDay: () => {
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';
},
random: () => {
return Math.random();
},
rollThreeDice: () => {
return [1, 2, 3].map(_ => 1 + Math.floor(Math.random() * 6));
},
getDie: ({ numSides }) => {
return new RandomDie(numSides || 6);
},
setMessage: ({ message }) => {
fakeDatabase.message = message;
return message;
},
getMessage: () => {
return fakeDatabase.message;
},
ip: function (_, context) {
return context.ip;
},
};
const app = express();
app.use(loggingMiddleware);
app.all(
'/graphql',
createHandler({
schema,
rootValue,
context: (req) => ({
ip: req.raw.ip,
}),
})
);
app.get('/', (_req, res) => {
res.type('html');
res.end(ruruHTML({ endpoint: '/graphql' }));
});
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');