-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
131 lines (127 loc) · 3.79 KB
/
schema.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
const graphql = require ('graphql');
const User = require('./models/user');
// const TODOs = [
// {
// "id": 1446412739542,
// "title": "Read emails",
// "completed": false
// },
// {
// "id": 1446412740883,
// "title": "Buy orange",
// "completed": true
// }
// ];
const userType = new graphql.GraphQLObjectType({
name: 'user',
fields: () => ({
_id: {type: graphql.GraphQLString},
name: {type: graphql.GraphQLString},
email: {type: graphql.GraphQLString},
username: {type: graphql.GraphQLString},
password: {type: graphql.GraphQLString},
msg: {type: graphql.GraphQLString}
})
});
const queryType = new graphql.GraphQLObjectType({
name: 'Query',
fields:{
users: {
type: new graphql.GraphQLList(userType),
resolve(){
return new Promise((resolve, reject) => {
User.getAllUser({}, (err, docs) => {
if (err) reject({msg: "Invalid request"})
else resolve(docs)
})
});
}
},
user:{
type: userType,
args: {
_id: {type: graphql.GraphQLString}
},
resolve: (root, args, context) => {
return new Promise((resolve, reject) => {
User.getUser({_id: args._id}, (err, user) => {
if (err) return reject({msg: "Invalid request"});
if (!user) resolve({msg: 'User not found !'})
else resolve(user);
})
})
}
}
}
})
const mutation = new graphql.GraphQLObjectType({
name: 'Mutation',
fields:{
add:{
type: userType,
description: 'Add user',
args: {
username: {type: new graphql.GraphQLNonNull(graphql.GraphQLString)},
name: {type: new graphql.GraphQLNonNull(graphql.GraphQLString)},
email: {type: new graphql.GraphQLNonNull(graphql.GraphQLString)},
password: {type: new graphql.GraphQLNonNull(graphql.GraphQLString)}
},
resolve(root,args){
var newUser = new User(args);
return new Promise((resolve, reject) => {
User.createUser(newUser, (err) => {
if (err) return reject({msg: "Invalid request"})
else resolve(newUser);
})
})
}
},
delete: {
type: userType,
description: 'Delete user',
args: {
_id: {type: new graphql.GraphQLNonNull(graphql.GraphQLString)}
},
resolve(root,args){
return new Promise((resolve, reject) => {
User.deleteUser(args._id, (err) => {
if (err) return reject({msg : "Invalid request"})
else resolve({msg: 'Delete user successfully !'})
})
})
}
},
edit: {
type: userType,
description: 'Update user',
args: {
_id: {type: new graphql.GraphQLNonNull(graphql.GraphQLString)},
name: {type: graphql.GraphQLString},
username: {type: graphql.GraphQLString},
email: {type: graphql.GraphQLString},
},
resolve(root,args){
return new Promise((resolve,reject) => {
User.getUser({_id: args._id}, (err,user) => {
if (err) return reject({msg: "Invalid request"});
if (!user) reject({msg: 'User not found !'});
let correctUser = {
name: args.name || user.name,
email: args.email || user.email,
username: args.username || user.username,
password: user.password
}
User.correctUser({_id: args._id}, correctUser, (err,docs) => {
if (err) return resolve({msg: "Invalid request"})
else resolve(docs);
})
})
})
}
}
}
})
module.exports = new graphql.GraphQLSchema({
query: queryType,
mutation
})