text: const { AuthenticationError } = require('apollo-server-express');
  const { User } = require('../models');
  const { signToken } = require('../utils/auth');
  
  const resolvers = {
    Query: {
      // users: async () => {
      //   return User.find().populate('thoughts');
      // },
      // user: async (parent, { username }) => {
      //   return User.findOne({ username }).populate('thoughts');
      // },
      // thoughts: async (parent, { username }) => {
      //   const params = username ? { username } : {};
      //   return Thought.find(params).sort({ createdAt: -1 });
      // },
      // thought: async (parent, { thoughtId }) => {
      //   return Thought.findOne({ _id: thoughtId });
      // },
      me: async (parent, args, context) => {
        if (context.user) {
          return User.findOne({ _id: context.user._id });
        }
        throw new AuthenticationError('You need to be logged in!');
      },
    },
  
    Mutation: {
        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 };
      },
  
      addUser: async (parent, { username, email, password }) => {
        const user = await User.create({ username, email, password });
        const token = signToken(user);
        return { token, user };
      },
  
      saveBook: async (parent, args, context) => {
          if (context.user) {
              try {
                  const bookSaved = await User.findOneAndUpdate(
                       {_id: context.user._id},
                       {$addToSet: {savedBooks: args}},
                       {new: true, runValidators: true}
                   );
                  return bookSaved;
              } catch (err) {
                  return err;
              };
          }
           throw new AuthenticationError('You need to be logged in!');
      },
      removeBook: async (parent, {bookId}, context) => {
          console.log(bookId);
          if (context.user) {
               const bookRemoved = await User.findOneAndUpdate(
                  { _id: context.user._id },
                  { $pull: { savedBooks: { bookId: bookId } } },
                  { new: true }
                );
                 if (!bookRemoved) {
                   return "Couldn't find user with this id!";
                }
                return bookRemoved;
          }
              throw new AuthenticationError('You need to be logged in!');
          },
    }
  };    
  
  module.exports = resolvers;
  