Skip to content

Commit 0bdaff3

Browse files
committed
Add query booksByAuthor
1 parent ea6566d commit 0bdaff3

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

graphQLApollo/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,25 @@ const resolvers = {
3434
booksAndAuthors() {
3535
let booksAndAuthorsArray = []
3636
for (let i = 0; i < db.books.length; i++) {
37-
for(let j = 0; j < db.authors.length; j++) {
38-
if(db.books[i].author === db.authors[j].id) {
39-
booksAndAuthorsArray.push({title: db.books[i].title, authorName: db.authors[j].name })
37+
for (let j = 0; j < db.authors.length; j++) {
38+
if (db.books[i].author === db.authors[j].id) {
39+
booksAndAuthorsArray.push({ title: db.books[i].title, authorName: db.authors[j].name })
4040
}
4141
}
4242
}
4343
return booksAndAuthorsArray
4444
},
45+
booksByAuthor(parent, args, context) {
46+
const authorsID = db.authors.find(a => {
47+
if (a.name === args.name) {
48+
return a
49+
}
50+
})
51+
const booksByAuthor = db.books.filter(b => {
52+
return b.author === authorsID.id
53+
})
54+
return booksByAuthor
55+
},
4556
book(parent, args, context) {
4657
//console.log(parent, "\n", context)
4758
let result = db.books.find((b) => {

graphQLApollo/schema/schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ export const typeDefs = `#graphql
2525
title: String!
2626
authorName: String!
2727
}
28-
28+
2929
type Query {
3030
books: [Book]
3131
book(id: Int!): Book
3232
authors: [Author]
3333
addressAuthor(name: String!): Address
3434
booksAndAuthors: [BookAndAuthor]
35+
booksByAuthor(name: String!): [Book]
3536
}
3637
`

0 commit comments

Comments
 (0)