// import the required GraphQL Types
const {
    GraphQLObjectType,
    GraphQLInt,
    GraphQLString,
    GraphQLList,
    GraphQLID,
    GraphQLInputObjectType
  } = require('graphql');

  var GraphQLDate = require('graphql-date')    

const  db  = require('../../models');
const bcrypt = require('bcrypt')
const User = db.users;
const UserType = require('./../types/userType')  

exports.createUserMutation = {
    type : UserType,
    description: 'Create a new user',
    args:{
        username: {
            type: GraphQLString
        },
        email: {
          type: GraphQLString
        },
        password: {
            type: GraphQLString
        }
    },
    resolve: async (parent, args ) =>{
    const hashedPassword = await bcrypt.hash(args.password, 10)
    args.password= hashedPassword    
    const user = new User(args)   

    const newUser = await user.save()
    if(!newUser){
        throw new Error('User is empty');
    }else{
        return newUser
    }

    } 
}; 