-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
142 lines (125 loc) · 3.71 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { UserInputError, AuthenticationError } = require('apollo-server')
const Book = require('./models/book')
const Author = require('./models/author')
const User = require('./models/user')
const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
const { PubSub } = require('graphql-subscriptions')
const pubsub = new PubSub()
require('dotenv').config()
const SECRET = process.env.SECRET
const resolvers = {
Query: {
bookCount: async () => Book.collection.countDocuments(),
authorCount: async () => Author.collection.countDocuments(),
allBooks: async (root, args) => {
// fix the author queries after fixing authors data
if (args.author && args.genre) {
return books.filter(
(b) => b.author === args.author && b.genres.includes(args.genre)
)
}
if (args.genre) {
return Book.find({ genres: { $in: [args.genre] } })
}
if (args.author) {
return books.filter((b) => b.author === args.author)
}
return Book.find({})
},
allAuthors: async () => Author.find({}),
me: (root, args, context) => {
return context.currentUser
},
},
Book: {
author: async (root) => {
return Author.findById(root.author)
},
},
Author: {
bookCount: async (message, args, { loaders }) => {
return loaders.books.load(message._id)
},
},
Mutation: {
addBook: async (root, args, { currentUser }) => {
if (!currentUser) {
throw new AuthenticationError('Not authenticated')
}
let author = await Author.findOne({ name: args.author })
if (!author) {
author = new Author({ name: args.author, born: args.born })
try {
await author.save()
} catch (error) {
throw new UserInputError(error.message, {
invalidArgs: args,
})
}
}
const book = new Book({
title: args.title,
published: args.published,
genres: args.genres,
author: author._id,
})
// send notification to subscribers
pubsub.publish('BOOK_ADDED', { bookAdded: book })
try {
return book.save()
} catch (error) {
throw new UserInputError(error.message, {
invalidArgs: args,
})
}
},
editAuthor: async (root, args, { currentUser }) => {
if (!currentUser) {
throw new AuthenticationError('Not authenticated')
}
const author = await Author.findOne({ name: args.name })
if (!author)
throw new UserInputError('That author does not exist in the App')
author.born = args.setBornTo
return author.save()
},
createUser: async (root, args) => {
const { username, password, favouriteGenre } = args
const saltRounds = 10
const passwordHash = await bcrypt.hash(password, saltRounds)
const user = new User({
username,
passwordHash,
favouriteGenre,
})
return user.save().catch((error) => {
throw new UserInputError(error.message, {
invalidArgs: args,
})
})
},
login: async (root, args) => {
const { username, password } = args
const user = await User.findOne({ username })
const passwordCorrect =
user === null
? false
: await bcrypt.compare(password, user.passwordHash)
if (!(user && passwordCorrect)) {
throw new UserInputError('wrong credentials')
}
const userForToken = {
username: user.username,
id: user._id,
}
return { value: jwt.sign(userForToken, SECRET) }
},
},
Subscription: {
bookAdded: {
subscribe: () => pubsub.asyncIterator(['BOOK_ADDED']),
},
},
}
module.exports = resolvers