-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
85 lines (81 loc) · 2.16 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
const { AuthenticationError } = require("./utils/errors");
const resolvers = {
Query: {
user: async (_, { id }, { dataSources }) => {
const user = await dataSources.accountsAPI.getUser(id);
if (!user) {
throw new Error("No user found for this Id");
}
return user;
},
me: async (_, __, { dataSources, userId }) => {
if (!userId) throw AuthenticationError();
const user = await dataSources.db.getUser(userId);
return user;
},
},
Mutation: {
updateProfile: async (
_,
{ updateProfileInput },
{ dataSources, userId }
) => {
if (!userId) throw AuthenticationError();
try {
const updatedUser = await dataSources.accountsAPI.updateUser({
userId,
userInfo: updateProfileInput,
});
return {
code: 200,
success: true,
message: "Profile successfully updated!",
user: updatedUser,
};
} catch (err) {
return {
code: 400,
success: false,
message: err.message,
};
}
},
changeLoggedInStatus: async (_, __, { dataSources, userId }) => {
if (!userId) throw AuthenticationError();
try {
const userLoggedInOrOut = await dataSources.db.changeAuthStatus(userId);
const { lastActiveTime, isLoggedIn } = userLoggedInOrOut;
return {
success: true,
time: lastActiveTime,
message: `User was successfully ${
isLoggedIn ? "logged in" : "logged out"
}`,
};
} catch (err) {
return {
success: false,
time: null,
message: err.message,
};
}
},
},
User: {
__resolveReference: async ({ id }, { dataSources }) => {
const user = await dataSources.db.getUser(id);
return user;
},
id: (parent) => {
// Get the username from the object in db and return it as id
return parent.username;
},
profileDescription: (user) => {
return user.description;
},
lastActiveTime: (user) => {
return user.lastActiveTime.toString();
},
},
};
module.exports = resolvers;