forked from freiksenet/graphql-finland-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserModel.js
40 lines (34 loc) · 911 Bytes
/
UserModel.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
const DataLoader = require("dataloader");
class UserModel {
constructor(resource, userId) {
this.resource = resource;
this.userLoader = new DataLoader(ids => this.getByIds(ids));
this.userId = userId;
}
async getAll() {
return this.resource.query(`/users`);
}
async getById(id) {
console.log(id);
return this.userLoader.load(id);
}
async getByIds(ids) {
const queryString = ids.map(id => `id=${id}`).join("&");
const result = await this.resource.query(`/users?${queryString}`);
return ids.map(id => result.find(user => user.id.toString() === id));
}
async getPrivate(user) {
if (this.userId != user.id) {
return null;
} else {
return {
email: user.email,
address: user.address,
phone: user.phone,
website: user.website,
company: user.company
};
}
}
}
module.exports = UserModel;