-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (108 loc) · 2.3 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
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
class User {
id;
name;
email;
constructor(args) {
Object.assign(this, args);
}
}
const users = [1,2,3].map(n=> new User({id:n, name:'steve'+n, email:'info@konfer.be'+n}))
const fakeDatabase = users;
const schema = buildSchema(`
type Query {
hello: String
}
`);
const root = {
hello: () => {
return 'Hello graphQL world';
}
};
const schema2 = buildSchema(`
type Query {
quoteOfTheDay: String
random: Float!,
rollThreeDice: [Int],
rollDice(idx:Int): Int
}
`);
const root2 = {
quoteOfTheDay: () => {
return 'Quote of the day'
},
random: () => {
return 3.14
},
rollThreeDice: () => {
return [1,2,3]
},
rollDice: (idx) => {
return [1,2,3].find(v => v === idx.idx)
}
};
const schema3 = buildSchema(`
type User {
id: Int,
name: String,
email: String
},
type Query {
single(args: Int): User,
multiple: [User]
}
`);
const root3 = {
single: (args) => {
return users.find(u => u.id === args.args);
},
multiple: () => {
return users;
}
};
const schema4 = buildSchema(`
input UserInput {
name: String,
email: String
},
type User {
id: Int,
name: String,
email: String
},
type Query {
getUser(id: ID!): User
},
type Mutation {
createUser(input: UserInput): User,
updateUser(id: ID!, input: UserInput): User
}
`);
const root4 = {
single: (args) => {
return fakeDatabase.find(u => u.id === args.args);
},
getUser: ({ id }) => {
console.log('id', id);
return fakeDatabase.find(u => u.id == id);
},
createUser: ({ input }) => {
fakeDatabase.push(input);
return fakeDatabase.slice().shift();
},
updateUser: ({ id, input }) => {
fakeDatabase[id].name = name;
return fakeDatabase[id];
}
};
const app = express();
app.post('/graphql', graphqlHTTP({
schema: schema4,
rootValue: root4,
graphiql: true
}));
app.listen(8201, () => {
console.log('Express is running on port 8201');
});