-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (65 loc) · 1.75 KB
/
index.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
const express = require('express');
const express_graphql = require('express-graphql');
const {buildSchema} = require('graphql');
let users = [{id:0, name: "Pepe", email:"asd@gmail.com"}, {id:1, name: "Juan", email:"asd@gmail.com"}, {id:2, name: "Pepe", email:"asd@gmail.com"}];
let myId = 3;
const app = express();
const schema = buildSchema(`
type Query{
hola:String,
users:[User],
user(id:Int):User
},
type Mutation{
createUser(name: String, email: String):User,
updateUser(id:ID!, name: String, email: String):User,
deleteUser(id: ID!):String,
},
type User{
id:ID!,
name:String!,
email:String!
},
type UserInput{
name: String!,
email: String!
}
`);
const resolver = {
hola: ()=>'Hola',
users: ()=> users,
user(id){
return users.find(user => user.id == id.id);
},
createUser(input){
const newUser = {
id:myId,
name:input.name,
email:input.email
};
users.push(newUser);
myId += 1;
return newUser;
},
deleteUser(input){
users.pop(user => user.id == input.id);
return 'So bye bye, miss American Pie'
},
updateUser(input){
const lastUser = this.user({id:input.id});
const newOldUser = {
id:lastUser.id,
name:input.name,
email:input.email
};
users.pop(user => user.id == lastUser.id);
users.push(newOldUser);
return newOldUser;
}
};
app.use('/graphql', express_graphql({
schema: schema,
rootValue: resolver,
graphiql: true
}));
app.listen(4000,()=> console.log('asd'));