text: const { AuthenticationError } = require('apollo-server-express');
  const { User, Book } = require('../models');
  const { signToken } = require('../utils/auth');
  
  const resolvers = {
    Query: {
      me: async (parent, args, context) => {
        if (context.user) {
          return User.findOne({ _id: context.user._id }).populate('savedBooks');
        }
        throw new AuthenticationError('You need to be logged in!');
      }
    },
  
    Mutation: {
      addUser: async (parent, args) => {
        const user = await User.create(args);
        const token = signToken(user);
        return { token, user };
      },
      login: async (parent, { email, password }) => {
        const user = await User.findOne({ email });
  
        if (!user) {
          throw new AuthenticationError('No user found with this email address');
        }
  
        const correctPw = await user.isCorrectPassword(password);
  
        if (!correctPw) {
          throw new AuthenticationError('Incorrect credentials');
        }
  
        const token = signToken(user);
  
        return { token, user };
      },
      addBook: async (parent, args, context) => {
        if (context.user) {
          // const book = await Book.create({ bookId, title });
  
          const updatedUser = await User.findOneAndUpdate(
            { _id: context.user._id },
            { $addToSet: { savedBooks: args.input } },
            { new: true }
          );
  
          return updatedUser;
        }
        throw new AuthenticationError('You need to be logged in!');
      },
      removeBook: async (parent, args, context) => {
        if (context.user) {
          // const book = await Book.findOneAndDelete({ _id: bookId });
  
          const updatedUser = await User.findOneAndUpdate(
            { _id: context.user._id },
            { $pull: { savedBooks: { bookId: args.bookId }} },
            { new: true }
          );
  
          return updatedUser;
  
        }
        throw new AuthenticationError('You need to be logged in!'); 
      },
      // removeBook: async (parent, { bookId }, context) => {
      //   if (context.user) {
      //     const book = await Book.findOneAndDelete({
      //       {_id: bookId},
      //     })
      //   }
      //   return Thought.findOneAndUpdate(
      //     { _id: thoughtId },
      //     { $pull: { comments: { _id: commentId } } },
      //     { new: true }
      //   );
      // },
    },
  };
  
  module.exports = resolvers;
  