text: const { AuthenticationError } = require("apollo-server-express");
  const { User, Post } = require("../models");
  const { signToken } = require("../utils/auth");
  const axios = require("axios");
  require("dotenv").config();
  
  const messages = [];
  
  const resolvers = {
    Query: {
      users: async () => {
        return User.find().populate("posts");
      },
      user: async (parent, { username }) => {
        return User.findOne({ username }).populate("posts");
      },
      posts: async (parent, { username }) => {
        const params = username ? { username } : {};
        return Post.find(params).sort({ createdAt: -1 });
      },
      post: async (parent, { postId }) => {
        return Post.findOne({ _id: postId });
      },
      me: async (parent, args, context) => {
        if (context.user) {
          return User.findOne({ _id: context.user._id }).populate("posts");
        }
        throw new AuthenticationError("You need to be logged in!");
      },
      articles: async () => {
        const result = await axios.get(
          "https://newsapi.org/v2/top-headlines?language=en&category=technology&apiKey=b35c794fa5b748db95bc403305608276"
        );
        console.log(result);
        return result.data.articles;
      },
      messages: () => messages,
    },
  
    Mutation: {
      addUser: async (parent, { username, email, password }) => {
        const user = await User.create({ username, email, password });
        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 };
      },
      addPost: async (parent, { postText }, context) => {
        if (context.user) {
          const post = await Post.create({
            postText,
            postAuthor: context.user.username,
          });
  
          await User.findOneAndUpdate(
            { _id: context.user._id },
            { $addToSet: { posts: post._id } }
          );
  
          return post;
        }
        throw new AuthenticationError("You need to be logged in!");
      },
      addComment: async (parent, { postId, commentText }, context) => {
        if (context.user) {
          return Post.findOneAndUpdate(
            { _id: postId },
            {
              $addToSet: {
                comments: { commentText, commentAuthor: context.user.username },
              },
            },
            {
              new: true,
              runValidators: true,
            }
          );
        }
        throw new AuthenticationError("You need to be logged in!");
      },
      removePost: async (parent, { postId }, context) => {
        if (context.user) {
          const post = await Post.findOneAndDelete({
            _id: postId,
            postAuthor: context.user.username,
          });
  
          await User.findOneAndUpdate(
            { _id: context.user._id },
            { $pull: { posts: post._id } }
          );
  
          return post;
        }
        throw new AuthenticationError("You need to be logged in!");
      },
      removeComment: async (parent, { postId, commentId }, context) => {
        if (context.user) {
          return Post.findOneAndUpdate(
            { _id: postId },
            {
              $pull: {
                comments: {
                  _id: commentId,
                  commentAuthor: context.user.username,
                },
              },
            },
            { new: true }
          );
        }
        throw new AuthenticationError("You need to be logged in!");
      },
      postMessage: (parent, { user, content}) => {
        const _id = messages.length;
        messages.push({
        _id,
        user,
        content
        });
        return _id
      }
    },
  };
  
  module.exports = resolvers;
  