feat(server): author -> books

This commit is contained in:
Joshua Seigler 2020-01-28 23:56:19 -05:00
parent c3b89e918f
commit 4dd04ff725

View file

@ -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 });
}
}
}