-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
238 lines (204 loc) · 8.76 KB
/
resolvers.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"use strict";
const UserModel = {
load: async (context, userId) => {
if (context.userCache[userId]) {
return context.userCache[userId];
}
const [rows, fields] = await context.db.query('SELECT * FROM user WHERE user_id = ?', [userId]);
context.userCache[userId] = rows;
return rows;
},
getName: async (context, { userId }) => {
const rows = await UserModel.load(context, userId);
return (rows.length === 0 ? null : rows[0].userName);
},
getAccount: async (context, { userId }) => {
const rows = await UserModel.load(context, userId);
return (rows.length === 0 ? null : rows[0].account);
},
getPassword: async (context, { userId }) => {
const rows = await UserModel.load(context, userId);
return (rows.length === 0 ? null : rows[0].pass);
},
getEmail: async (context, { userId }) => {
const rows = await UserModel.load(context, userId);
return (rows.length === 0 ? null : rows[0].email);
}
}
const ProductModel = {
load: async (context, productId) => {
if (context.userCache[productId]) {
return context.productCache[productId];
}
const [rows, fields] = await context.db.query('SELECT * FROM product WHERE product_id = ?', [productId]);
context.productCache[productId] = rows;
return rows;
},
getName: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].productName);
},
getPrice: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].price);
},
getSeller: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : { userId: rows[0].seller });
},
getCategory: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].category);
},
getBoughtDate: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].boughtDate);
},
getProductPhoto: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].product_photo);
},
getLookLike: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].look_like);
},
getNumberOfProduct: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].numberOfProduct);
},
getDescript: async (context, { productId }) => {
const rows = await ProductModel.load(context, productId);
return (rows.length === 0 ? null : rows[0].descript);
}
}
const resolvers = {
Product: {
productId: ({ productId }, _, context) => {
return productId;
},
productName: async({ productId }, _, context) => {
return ProductModel.getName(context, { productId });
},
price: async({ productId }, _, context) => {
return ProductModel.getPrice(context, { productId });
},
seller: async({ productId }, _, context) => {
return ProductModel.getSeller(context, { productId });
},
category: async({ productId }, _, context) => {
return ProductModel.getCategory(context, { productId });
},
boughtDate: async({ productId }, _, context) => {
return ProductModel.getBoughtDate(context, { productId });
},
product_photo: async({ productId }, _, context) => {
return ProductModel.getProductPhoto(context, { productId });
},
look_like: async({ productId }, _, context) => {
return ProductModel.getLookLike(context, { productId });
},
numberOfProduct: async({ productId }, _, context) => {
return ProductModel.getNumberOfProduct(context, { productId });
},
descript: async({ productId }, _, context) => {
return ProductModel.getDescript(context, { productId });
}
},
Query: {
user: async (_, { userId }, context) => {
const [rows, fields] = await context.db.query('SELECT user_id AS userId FROM user WHERE user_id = ?', [userId]);
// console.log("Query user", userId)
return (rows.length > 0 ? { userId: rows[0].userId } : null);
},
users: async (_, { limit = 20, offset = 0, sort = 'ASC' }, context) => {
const [rows, fields] = await context.db.query('SELECT user_id AS userId FROM user LIMIT ? OFFSET ?', [limit, offset]);
// console.log("Query users:")
return rows;
},
product: async (_, { productId }, context) => {
const [rows, fields] = await context.db.query('SELECT product_id AS productId FROM product WHERE product_id = ?', [productId]);
return (rows.length > 0 ? { productId: rows[0].productId } : null);
},
products: async (_, { limit = 20, offset = 0, sort = 'ASC' }, context) => {
const [rows, fields] = await context.db.query('SELECT product_id AS productId FROM product LIMIT ? OFFSET ?', [limit, offset]);
return rows;
},
history: async (_, { historyId }, context) => {
// console.log("history", historyId);
const [rows, fields] = await context.db.query('SELECT history_id AS historyId FROM history WHERE history_id = ?', [historyId]);
return (rows.length > 0 ? { historyId: rows[0].historyId } : null);
},
historys: async (_, { limit = 20, offset = 0, sort = 'ASC' }, context) => {
const [rows, fields] = await context.db.query('SELECT history_id AS historyId FROM history LIMIT ? OFFSET ?', [limit, offset]);
return rows;
},
},
Mutation: {
updateUser: async(_, { userId, account, email }, context) => {
let q = '';
let d = [];
if(account && !email ) {
q = 'UPDATE user SET account = ? WHERE user_id = ?';
d = [account, userId];
}
else if(!account && email ) {
q = 'UPDATE user SET email = ? WHERE user_id = ?';
d = [email, userId];
}
else {
q = 'UPDATE user SET account = ?, email = ? WHERE user_id = ?';
d = [account, email, userId];
}
const [rows, fields] = await context.db.query(q, d);
return rows
}
},
User: {
userName: async ({ userId }, _, context) => {
return UserModel.getName(context, { userId });
},
userId: ({ userId }, _, context) => {
return userId;
},
account: async ({ userId }, _, context) => {
return UserModel.getAccount(context, { userId });
},
pass: async ({ userId }, _, context) => {
return UserModel.getPassword(context, { userId });
},
email: async ({ userId }, _, context) => {
return UserModel.getEmail(context, { userId });
},
products: async ({ userId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT product_id AS productId FROM product WHERE seller = ?', [userId]);
return rows.map(({ productId }) => ({ productId: productId }));
},
buyProducts: async ({ userId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT history_id AS HistoryId FROM history WHERE buyer = ?', [userId]);
return rows.map(({ HistoryId }) => ({ HistoryId: HistoryId }));
}
},
History: {
buyer: async ({ HistoryId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT buyer FROM history WHERE history_id = ?', [HistoryId]);
return (rows.length === 0 ? null : rows[0].buyer);
},
buy_at: async ({ HistoryId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT buy_at FROM history WHERE history_id = ?', [HistoryId]);
return (rows.length === 0 ? null : rows[0].buy_at.toISOString().substring(0, 10) );
},
num: async ({ HistoryId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT num FROM history WHERE history_id = ?', [HistoryId]);
return (rows.length === 0 ? null : rows[0].num );
},
product_name: async ({ HistoryId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT product_name FROM history WHERE history_id = ?', [HistoryId]);
return (rows.length === 0 ? null : rows[0].product_name );
},
price: async ({ HistoryId }, _, context) => {
const [rows, fields] = await context.db.query('SELECT price FROM history WHERE history_id = ?', [HistoryId]);
return (rows.length === 0 ? null : rows[0].price );
}
}
};
module.exports = resolvers;