-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (45 loc) · 1008 Bytes
/
Copy pathindex.js
File metadata and controls
50 lines (45 loc) · 1008 Bytes
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
const express = require("express");
const { graphqlHTTP } = require("express-graphql");
const { buildSchema } = require("graphql");
const schema = buildSchema(`
type RandomDie {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!):[Int]
}
type Mutation {
setMessage(message:String): String
}
type Query {
getDie(numSides:Int):RandomDie
getMessage: String
}
`);
class RandomDie {
constructor(numSides) {
this.numSides = numSides;
}
rollOnce() {
return 1;
}
roll({ numRolls }) {
return [numRolls];
}
}
let fakeDatabase = {};
const root = {
getDie: ({ numSides }) => {
return new RandomDie(numSides || 6);
},
getMessage: () => {
return fakeDatabase.message;
},
setMessage: ({ message }) => {
fakeDatabase.message = message;
return message;
},
};
const app = express();
app.use("/graphql", graphqlHTTP({ schema, rootValue: root, graphiql: true }));
app.listen(4000);
console.log("Running a GraphQL API server at http://localhost:4000/graphql");