File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ) => {
Original file line number Diff line number Diff 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`
You can’t perform that action at this time.
0 commit comments