-
Notifications
You must be signed in to change notification settings - Fork 52
/
user.ts
128 lines (126 loc) · 3.46 KB
/
user.ts
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
import { ApolloContext } from './../app';
import { gql, IResolvers, AuthenticationError, ApolloError } from 'apollo-server-koa';
import User from '../entity/User';
import { getRepository, getManager } from 'typeorm';
import VelogConfig from '../entity/VelogConfig';
import Series from '../entity/Series';
import UserProfile from '../entity/UserProfile';
export const typeDef = gql`
type User {
id: ID!
username: String
email: String
created_at: Date
updated_at: Date
is_certified: Boolean
profile: UserProfile
velog_config: VelogConfig
series_list: [Series]
}
type UserProfile {
id: ID!
display_name: String
short_bio: String
thumbnail: String
created_at: Date
updated_at: Date
about: String
profile_links: JSON
}
type VelogConfig {
id: ID!
title: String
logo_image: String
}
extend type Query {
user(id: ID, username: String): User
velog_config(username: String): VelogConfig
auth: User
}
extend type Mutation {
update_about(about: String!): UserProfile
}
`;
export const resolvers: IResolvers<any, ApolloContext> = {
User: {
profile: async (parent: User, _: any, { loaders }: ApolloContext) => {
return loaders.userProfile.load(parent.id);
},
velog_config: async (parent: User, _: any, context: ApolloContext) => {
const { loaders }: ApolloContext = context;
return loaders.velogConfig.load(parent.id);
},
email: (parent: User, _: any, context: any) => {
if (context.user_id !== parent.id) {
throw new AuthenticationError('No permission to read email address');
}
return parent.email;
},
series_list: async (parent: User, _: any, { loaders }) => {
const seriesRepo = getRepository(Series);
const seriesList = await seriesRepo.find({
where: {
fk_user_id: parent.id
},
order: {
updated_at: 'DESC'
}
});
return seriesList;
}
},
Query: {
user: async (parent: any, { id, username }: any) => {
const repo = getRepository(User);
try {
if (username) {
const user = await repo.findOne({
where: {
username
}
});
return user;
}
const user = await repo.findOne({
id
});
return user;
} catch (e) {
console.log(e);
}
},
velog_config: async (parent: any, { username }: any) => {
const repo = getRepository(VelogConfig);
const velogConfig = repo
.createQueryBuilder('velog_config')
.leftJoinAndSelect('velog_config.user', 'user')
.where('user.username = :username', { username })
.getOne();
return velogConfig;
},
auth: async (parent: any, params: any, ctx) => {
if (!ctx.user_id) return null;
return ctx.loaders.user.load(ctx.user_id);
}
},
Mutation: {
update_about: async (parent: any, args: any, ctx) => {
if (!ctx.user_id) {
throw new AuthenticationError('Not Logged In');
}
const userProfileRepo = getRepository(UserProfile);
const profile = await userProfileRepo.findOne({
where: {
fk_user_id: ctx.user_id
}
});
const { about } = args as { about: string };
if (!profile) {
throw new ApolloError('Failed to retrieve user profile');
}
profile.about = about || '';
await userProfileRepo.save(profile);
return profile;
}
}
};