Skip to content

Commit 575f6ee

Browse files
committed
refactor(resolvers): redesign the directory pattern
1 parent 9c7c880 commit 575f6ee

File tree

17 files changed

+164
-136
lines changed

17 files changed

+164
-136
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint": "tslint -p ."
1111
},
1212
"dependencies": {
13-
"@vichurch/model": "1.0.2",
13+
"@vichurch/model": "1.1.1",
1414
"bara": "2.4.0",
1515
"bcryptjs": "2.4.3",
1616
"graphql-yoga": "1.17.4",

src/resolvers/ActivitySchedule.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import {getUserId, Context} from '../../utils';
1+
import { getUserId, Context } from '../../utils'
22

3-
export const activitySchedule = {
4-
async addActivitySchedule(
5-
parent,
6-
{activityTypeId, start, end},
7-
ctx: Context,
8-
info,
9-
) {
10-
const userId = getUserId(ctx);
11-
if (userId) {
12-
return ctx.prisma.createActivitySchedule({
13-
activity: {connect: {id: activityTypeId}},
14-
start,
15-
end,
16-
});
17-
} else {
18-
throw new Error('Not authorized user!');
19-
}
20-
},
21-
};
3+
export async function addActivitySchedule(
4+
parent,
5+
{ activityTypeId, start, end },
6+
ctx: Context,
7+
info,
8+
) {
9+
const userId = getUserId(ctx)
10+
if (userId) {
11+
return ctx.prisma.createActivitySchedule({
12+
activity: { connect: { id: activityTypeId } },
13+
start,
14+
end,
15+
})
16+
} else {
17+
throw new Error('Not authorized user!')
18+
}
19+
}

src/resolvers/Mutation/auth.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ import * as bcrypt from 'bcryptjs'
22
import * as jwt from 'jsonwebtoken'
33
import { Context } from '../../utils'
44

5-
export const auth = {
6-
async signup(parent, args, ctx: Context) {
7-
const password = await bcrypt.hash(args.password, 10)
8-
const user = await ctx.prisma.createUser({ ...args, password })
5+
export async function signup(parent, args, ctx: Context) {
6+
const password = await bcrypt.hash(args.password, 10)
7+
const user = await ctx.prisma.createUser({ ...args, password })
98

10-
return {
11-
token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
12-
user,
13-
}
14-
},
9+
return {
10+
token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
11+
user,
12+
}
13+
}
1514

16-
async login(parent, { username, password }, ctx: Context) {
17-
const user = await ctx.prisma.user({ username })
18-
if (!user) {
19-
throw new Error(`No such user found as: ${username}`)
20-
}
15+
export async function login(parent, { username, password }, ctx: Context) {
16+
const user = await ctx.prisma.user({ username })
17+
if (!user) {
18+
throw new Error(`No such user found as: ${username}`)
19+
}
2120

22-
const valid = await bcrypt.compare(password, user.password)
23-
if (!valid) {
24-
throw new Error('Invalid password')
25-
}
21+
const valid = await bcrypt.compare(password, user.password)
22+
if (!valid) {
23+
throw new Error('Invalid password')
24+
}
2625

27-
return {
28-
token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
29-
user,
30-
}
31-
},
26+
return {
27+
token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
28+
user,
29+
}
3230
}

src/resolvers/Mutation/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//export * from './profile'
2+
export * from './auth'
3+
//export * from './activity-schedule'

src/resolvers/Mutation/profile.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
11
import { getUserId, Context } from '../../utils'
22

33
export const profile = {
4-
async createProfile(parent, { title, content }, ctx: Context, info) {
5-
const userId = getUserId(ctx)
6-
return ctx.prisma.profiles()
7-
//return ctx.prisma.createProfile({
8-
//title,
9-
//content,
10-
//author: {
11-
//connect: { id: userId },
12-
//},
13-
//})
14-
},
15-
4+
//async createProfile(parent, { title, content }, ctx: Context, info) {
5+
//const userId = getUserId(ctx)
6+
//return ctx.prisma.profiles()
7+
//return ctx.prisma.createProfile({
8+
//title,
9+
//content,
10+
//author: {
11+
//connect: { id: userId },
12+
//},
13+
//})
14+
//},
1615
//async publish(parent, { id }, ctx: Context, info) {
17-
//const userId = getUserId(ctx)
18-
//const postExists = await ctx.prisma.$exists.post({
19-
//id,
20-
//author: { id: userId },
21-
//})
22-
//if (!postExists) {
23-
//throw new Error(`Post not found or you're not the author`)
24-
//}
25-
26-
//return ctx.prisma.updatePost({
27-
//where: { id },
28-
//data: { published: true },
29-
//})
16+
//const userId = getUserId(ctx)
17+
//const postExists = await ctx.prisma.$exists.post({
18+
//id,
19+
//author: { id: userId },
20+
//})
21+
//if (!postExists) {
22+
//throw new Error(`Post not found or you're not the author`)
23+
//}
24+
//return ctx.prisma.updatePost({
25+
//where: { id },
26+
//data: { published: true },
27+
//})
3028
//},
31-
3229
//async deletePost(parent, { id }, ctx: Context, info) {
33-
//const userId = getUserId(ctx)
34-
//const postExists = await ctx.prisma.$exists.post({
35-
//id,
36-
//author: { id: userId },
37-
//})
38-
//if (!postExists) {
39-
//throw new Error(`Post not found or you're not the author`)
40-
//}
41-
42-
//return ctx.prisma.deletePost({ id })
30+
//const userId = getUserId(ctx)
31+
//const postExists = await ctx.prisma.$exists.post({
32+
//id,
33+
//author: { id: userId },
34+
//})
35+
//if (!postExists) {
36+
//throw new Error(`Post not found or you're not the author`)
37+
//}
38+
//return ctx.prisma.deletePost({ id })
4339
//},
4440
}

src/resolvers/Query/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { getUserId, Context } from '../../utils'
22

33
export const Query = {
4+
translation(parent, { id }, ctx: Context) {
5+
return ctx.prisma.translation({ id })
6+
},
7+
localization(parent, { id }, ctx: Context) {
8+
return ctx.prisma.localization({ id })
9+
},
410
profile(parent, { id }, ctx: Context) {
511
return ctx.prisma.profile({ id })
612
},
@@ -11,4 +17,9 @@ export const Query = {
1117
const id = getUserId(ctx)
1218
return ctx.prisma.user({ id })
1319
},
20+
async getLocalizationsByLang(parent, { lang }, ctx: Context, info) {
21+
console.log('Fetch localization for lang', lang)
22+
const localizations = await ctx.prisma.localizations()
23+
return localizations
24+
},
1425
}

src/resolvers/Subscription.ts renamed to src/resolvers/Subscription/Profile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Context } from '../utils'
1+
import { Context } from '../../utils'
22

3-
export const Subscription = {
3+
export const Profile = {
44
profileSubscription: {
55
subscribe: async (parent, args, ctx: Context) => {
66
return ctx.prisma.$subscribe
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Profile'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Context } from '../../utils'
2+
3+
export const ActivitySchedule = {
4+
activity: ({ id }, args, ctx: Context) => {
5+
return ctx.prisma.activitySchedule({ id }).activity()
6+
},
7+
}

0 commit comments

Comments
 (0)