-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.js
39 lines (38 loc) · 1.12 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
const resolvers = {
Book: {
author: (book, args, { db }) => db.getAuthorById(book.authorId),
cover: parent => ({
path: parent.coverPath
})
},
Author: {
books: (author, args, { db }) => parent.bookIds.map(db.getBookById),
photo: parent => ({
path: parent.photoPath
})
},
Avatar: {
image: avatar => ({
path: avatar.imagePath
})
},
Image: {
url: (image, args, context) => context.baseAssetsUrl + image.path
},
Query: {
books: (rootValue, args, { db }) => db.getAllBooks(),
authors: (rootValue, args, { db }) => db.getAllAuthors(),
users: (rootValue, args, { db }) => db.getAllUsers(),
randomBook: (rootValue, args, { db }) => {
const numberOfBooks = db.getAllBooks().length;
const randomId = Math.floor(Math.random() * numberOfBooks + 1);
return db.getBookById(randomId);
},
randomAuthor: (rootValue, args, { db }) => {
const numberOfAuthors = db.getAllAuthors().length;
const randomId = Math.floor(Math.random() * numberOfAuthors + 1);
return db.getAuthorById(randomId);
}
}
};
module.exports = resolvers;