From 4dd04ff7258e78a6e1f72c89c51f4ea987cb86e1 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Tue, 28 Jan 2020 23:56:19 -0500 Subject: [PATCH] feat(server): author -> books --- server/schema/schema.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/server/schema/schema.js b/server/schema/schema.js index b1b0733..2f5bb7a 100644 --- a/server/schema/schema.js +++ b/server/schema/schema.js @@ -6,6 +6,7 @@ const { GraphQLString, GraphQLID, GraphQLInt, + GraphQLList, GraphQLSchema } = graphql; @@ -13,7 +14,10 @@ const { const books = [ { name: 'Name of the Wind', genre: 'Fantasy', id: '1', authorId: '1' }, { name: 'The Final Empire', genre: 'Fantasy', id: '2', authorId: '2' }, - { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorId: '3' } + { name: 'The Hero of Ages', genre: 'Fantasy', id: '4', authorId: '2' }, + { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorId: '3' }, + { name: 'The Colour of Magic', genre: 'Fantasy', id: '5', authorId: '3' }, + { name: 'The Light Fantastic', genre: 'Fantasy', id: '6', authorId: '3' } ]; const authors = [ { name: 'Patrick Rothfuss', age: 44, id: '1' }, @@ -41,7 +45,13 @@ const AuthorType = new GraphQLObjectType({ fields: () => ({ id: { type: GraphQLID }, name: { type: GraphQLString }, - age: { type: GraphQLInt } + age: { type: GraphQLInt }, + books: { + type: new GraphQLList(BookType), + resolve (parent, args) { + return _.filter(books, { authorId: parent.id }); + } + } }) }); @@ -51,15 +61,15 @@ const RootQuery = new GraphQLObjectType({ book: { type: BookType, args: { id: { type: GraphQLID } }, - resolve (parent, { id }) { - return _.find(books, { id }); + resolve (parent, args) { + return _.find(books, { id: args.id }); } }, author: { type: AuthorType, args: { id: { type: GraphQLID } }, - resolve (parent, { id }) { - return _.find(authors, { id }); + resolve (parent, args) { + return _.find(authors, { id: args.id }); } } }