-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolvers.js
90 lines (81 loc) · 2.5 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
import pc from '@prisma/client'
import bcrypt from 'bcryptjs'
import { AuthenticationError,ForbiddenError } from 'apollo-server-express'
import jwt from 'jsonwebtoken'
import {PubSub} from 'graphql-subscriptions'
const pubsub = new PubSub()
const prisma = new pc.PrismaClient()
const MESSAGE_ADDED = 'MESSAGE_ADDED'
const resolvers = {
Query: {
users:async (_,args,{userId})=>{
if(!userId) throw new ForbiddenError("You must be logged in")
const users = await prisma.user.findMany({
orderBy:{
createdAt:"desc"
},
where:{
id:{
not:userId
}
}
})
return users
},
messagesByUser:async (_,{receiverId},{userId})=>{
if(!userId) throw new ForbiddenError("You must be logged in")
const messages = await prisma.message.findMany({
where:{
OR:[
{senderId:userId, receiverId:receiverId},
{senderId:receiverId,receiverId:userId}
]
},
orderBy:{
createdAt:"asc"
}
})
return messages
}
},
Mutation: {
signupUser: async (_, { userNew }) => {
const user = await prisma.user.findUnique({ where: { email: userNew.email } })
if (user) throw new AuthenticationError("User already exists with that email")
const hashedPassword = await bcrypt.hash(userNew.password, 10)
const newUser = await prisma.user.create({
data: {
...userNew,
password: hashedPassword
}
})
return newUser
},
signinUser: async (_, { userSignin }) => {
const user = await prisma.user.findUnique({ where: { email: userSignin.email } })
if (!user) throw new AuthenticationError("User dosen't exists with that email")
const doMatch = await bcrypt.compare(userSignin.password, user.password)
if (!doMatch) throw new AuthenticationError("email or password is invalid")
const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET)
return { token }
},
createMessage:async (_,{receiverId,text},{userId})=>{
if(!userId) throw new ForbiddenError("You must be logged in")
const message = await prisma.message.create({
data:{
text,
receiverId,
senderId:userId
}
})
pubsub.publish(MESSAGE_ADDED,{messageAdded:message})
return message
}
},
Subscription:{
messageAdded:{
subscribe:()=>pubsub.asyncIterator(MESSAGE_ADDED)
}
}
}
export default resolvers