im trying to return the book count of each author and i cant seem to pass in any arguments in when when using find in mongoose because i get the error above. my code below.
const typeDefs = gql`
type Author {
name: String!
born: Int
bookCount: Int
id: String!
}
type Book {
title: String!
published: Int!
author: Author!
genres: [String!]!
id: ID!
}
type Query {
bookCount: Int!
authorCount: Int!
allBooks(author: String, genre: String): [Book!]!
allAuthors: [Author!]!
}
type Mutation {
addBook(
title: String!
author: String!
published: Int!
genres: [String!]!
): Book
editAuthor(name: String!, setBornTo: Int!): Author
}
`;
const resolvers = {
Query: {
bookCount: async () => Book.collection.countDocuments(),
authorCount: async () => Author.collection.countDocuments(),
allBooks: async (root, args) => {
// if (args.genre && args.author) {
// return await Book.find({ author: [args.author] });
// }
// if (args.author) {
// return await Book.find({
// author: args.author,
// });
// }
// if (args.genre) {
// return await Book.find({
// genres: { $in: [args.genre] },
// });
// }
return await Book.find({});
},
allAuthors: async () => await Author.find({}),
},
Author: {
bookCount: async (root) => {
const books = await Book.find({ author: root.name });
return books.length;
},
},
Mutation: {
addBook: async (root, args) => {
const author = await Author.findOne({ author: args.name }).exec();
// const author = authors.find((author) => author.name === args.author);
console.log(author);
if (!author) {
author = { name: args.author, id: uuid() };
authors = authors.concat(author);
}
const newBook = { ...args, id: uuid() };
books = books.concat(newBook);
return newBook;
},
editAuthor: (root, args) => {
const authorToUpdate = authors.find(
(author) => author.name === args.name
);
if (!authorToUpdate) {
return null;
}
const updatedAuthor = { ...authorToUpdate, born: args.setBornTo };
authors = authors.map((author) =>
author.name === args.name ? updatedAuthor : author
);
return updatedAuthor;
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
this part is suppose to return the book count but the error comes and anywhere else where i try to pass in any arguments in find
Author: {
bookCount: async (root) => {
const books = await Book.find({ author: root.name });
return books.length;
},
},