-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (124 loc) · 3.62 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
let express = require('express');
let graphQLHTTP = require('express-graphql');
let { buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
let schema = buildSchema(`
input MessageInput {
content: String
author: String
}
type Message {
id: ID!
content: String
author: String
}
type RandomDice {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!): [Int]
}
type Query {
hello: String
quoteOfTheDay: String
random: Float!
rollThreeDices: [Int]
rollDice(numDice: Int!, numSides: Int): [Int]
getDice(numSides: Int): RandomDice
catchMessage: String
getMessage(id: ID!): Message
ip: String
}
type Mutation {
setMessage(message: String): String
createMessage(input: MessageInput): Message
updateMessage(id: ID!, input: MessageInput): Message
}
`);
// If Message had any complex fields, we'd put them on this object.
class Message {
constructor(id, { content, author }) {
this.id = id;
this.content = content;
this.author = author;
}
}
// This class implements the RandomDice GraphQL type.
class RandomDice {
constructor(numSides) {
this.numSides = numSides;
}
rollOnce() {
return 1 + Math.floor(Math.random() * this.numSides);
}
roll({ numRolls }) {
return new Array(numRolls).fill().map((_, i) => i + 1).map(_ => this.rollOnce());
}
}
// Maps username to content.
let fakeMessageDatabase = {};
let fakeDatabase = {};
function loggingMiddleware(req, res, next) {
console.log('ip:', req.ip);
next();
}
// The root provides a resolver function for each API endpoints.
let root = {
hello: () => {
return 'Hello world!';
},
quoteOfTheDay: () => {
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation comes from Jesus';
},
random: () => {
return Math.random();
},
rollThreeDices: () => {
return [1, 2, 3].map(_ => 1 + Math.floor(Math.random() * 6));
},
rollDice: ({ numDice, numSides }) => {
return new Array(numDice).fill().map((_, i) => i + 1).map(_ => 1 + Math.floor(Math.random() * (numSides || 6)));
},
getDice: ({ numSides }) => {
return new RandomDice(numSides || 6);
},
catchMessage: () => {
return fakeMessageDatabase.message
},
setMessage: ({ message }) => {
fakeMessageDatabase.message = message;
return message;
},
getMessage: function ({ id }) {
if (!fakeDatabase[id]) {
throw new Error('no message exists with id ' + id);
}
return new Message(id, fakeDatabase[id]);
},
createMessage: function ({ input }) {
// Create a random id for our "database"
let id = require('crypto').randomBytes(10).toString('hex');
fakeDatabase[id] = input;
return new Message(id, input);
},
updateMessage: function ({ id, input }) {
if (!fakeDatabase[id]) {
throw new Error('no message exists with id ' + id);
}
// This replaces all old data, but some apps might want partial update.
fakeDatabase[id] = input;
return new Message(id, input);
},
ip: (args, request) => {
return request.ip;
}
};
let app = express();
let httpSettings = {
schema: schema,
rootValue: root,
graphiql: true
};
app.use(loggingMiddleware);
app.use('/graphql', graphQLHTTP(httpSettings));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');