import { IResolvers } from "apollo-server-express" ; import CategoryThread from "../repo/CategoryThread" ; import { getTopCategoryThread } from "../repo/CategoryThreadRepo" ; import { QueryArrayResult , QueryOneResult } from "../repo/QueryArrayResult" ; import { Thread } from "../repo/Thread" ; import { ThreadCategory } from "../repo/ThreadCategory" ; import { getAllCategories } from "../repo/ThreadCategoryRepo" ; import { ThreadItem } from "../repo/ThreadItem" ; import { updateThreadItemPoint } from "../repo/ThreadItemPointRepo" ; import { createThreadItem , getThreadItemsByThreadId , } from "../repo/ThreadItemRepo" ; import { updateThreadPoint } from "../repo/ThreadPointRepo" ; import { createThread , getThreadById , getThreadsByCategoryId , getThreadsLatest , } from "../repo/ThreadRepo" ; import { User } from "../repo/User" ; import { changePassword , login , logout , me , register , UserResult , } from "../repo/UserRepo" ; import { GqlContext } from "./GqlContext" ; const STANDARD_ERROR = "An error has occurred" ; interface EntityResult { messages : Array < string > ; } const resolvers = { UserResult : { __resolveType ( obj , context , info ) { if ( obj . messages ) { return "EntityResult" ; } return "User" ; } , } , ThreadResult : { __resolveType ( obj , context , info ) { if ( obj . messages ) { return "EntityResult" ; } return "Thread" ; } , } , ThreadItemResult : { __resolveType ( obj , context , info ) { if ( obj . messages ) { return "EntityResult" ; } return "ThreadItem" ; } , } , ThreadArrayResult : { __resolveType ( obj , context , info ) { if ( obj . messages ) { return "EntityResult" ; } return "ThreadArray" ; } , } , ThreadItemArrayResult : { __resolveType ( obj , context , info ) { if ( obj . messages ) { return "EntityResult" ; } return "ThreadItemArray" ; } , } , Query : { getThreadById : async ( obj , args , ctx , info ) : Promise < Thread | EntityResult > => { let thread ; try { thread = await getThreadById ( args . id ) ; if ( thread . entity ) { return thread . entity ; } return { messages : thread . messages ? thread . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { console . log ( ex . message ) ; throw ex ; } } , getThreadsByCategoryId : async ( obj , args , ctx , info ) : Promise < { threads : Array < Thread > ; } | EntityResult > => { let threads ; try { threads = await getThreadsByCategoryId ( args . categoryId ) ; if ( threads . entities ) { return { threads : threads . entities , } ; } return { messages : threads . messages ? threads . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { throw ex ; } } , getThreadsLatest : async ( obj , args , ctx , info ) : Promise < { threads : Array < Thread > ; } | EntityResult > => { let threads ; try { threads = await getThreadsLatest ( ) ; if ( threads . entities ) { return { threads : threads . entities , } ; } return { messages : threads . messages ? threads . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { throw ex ; } } , getThreadItemByThreadId : async ( obj , args , ctx , info ) : Promise < { threadItems : Array < ThreadItem > ; } | EntityResult > => { let threadItems ; try { threadItems = await getThreadItemsByThreadId ( args . threadId ) ; if ( threadItems . entities ) { return { threadItems : threadItems . entities , } ; } return { messages : threadItems . messages ? threadItems . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { throw ex ; } } , getAllCategories : async ( obj , args , ctx , info ) : Promise < Array < ThreadCategory > | EntityResult > => { let categories ; try { categories = await getAllCategories ( ) ; if ( categories . entities ) { return categories . entities ; } return { messages : categories . messages ? categories . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { throw ex ; } } , me : async ( obj , args , ctx , info ) : Promise < User | EntityResult > => { let user ; try { if ( ! ctx . req . session ?. userId ) { return { messages : [ "User not logged in." ] , } ; } user = await me ( ctx . req . session . userId ) ; if ( user && user . user ) { return user . user ; } return { messages : user . messages ? user . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { throw ex ; } } , getTopCategoryThread : async ( obj , args , ctx , info ) : Promise < Array < CategoryThread > > => { try { return await getTopCategoryThread ( ) ; } catch ( ex ) { console . log ( ex . message ) ; throw ex ; } } , } , Mutation : { createThread : async ( obj , args , ctx , info ) : Promise < EntityResult > => { let result ; try { result = await createThread ( args . userId , args . categoryId , args . title , args . body ) ; return { messages : result . messages ? result . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { console . log ( ex ) ; throw ex ; } } , createThreadItem : async ( obj , args , ctx , info ) : Promise < EntityResult > => { let result ; try { result = await createThreadItem ( args . userId , args . threadId , args . body ) ; return { messages : result . messages ? result . messages : [ STANDARD_ERROR ] , } ; } catch ( ex ) { console . log ( ex ) ; throw ex ; } } , updateThreadPoint : async ( obj , args , ctx , info ) : Promise < string > => { let result = "" ; try { if ( ! ctx . req . session || ! ctx . req . session ?. userId ) { return "You must be logged in to set likes." ; } result = await updateThreadPoint ( ctx . req . session ! . userId , args . threadId , args . increment ) ; return result ; } catch ( ex ) { throw ex ; } } , updateThreadItemPoint : async ( obj , args , ctx , info ) : Promise < string > => { let result = "" ; try { if ( ! ctx . req . session || ! ctx . req . session ?. userId ) { return "You must be logged in to set likes." ; } result = await updateThreadItemPoint ( ctx . req . session ! . userId , args . threadItemId , args . increment ) ; return result ; } catch ( ex ) { throw ex ; } } , register : async ( obj , args , ctx , info ) : Promise < string > => { let user ; try { user = await register ( args . email , args . userName , args . password ) ; if ( user && user . user ) { return "Registration successful." ; } return user && user . messages ? user . messages [ 0 ] : STANDARD_ERROR ; } catch ( ex ) { throw ex ; } } , login : async ( obj , args , ctx , info ) : Promise < string > => { let user ; try { user = await login ( args . userName , args . password ) ; if ( user && user . user ) { ctx . req . session ! . userId = user . user . id ; return `Login successful for userId ${ ctx . req . session ! . userId }.` ; } return user && user . messages ? user . messages [ 0 ] : STANDARD_ERROR ; } catch ( ex ) { console . log ( ex . message ) ; throw ex ; } } , logout : async ( obj , args , ctx , info ) : Promise < string > => { try { let result = await logout ( args . userName ) ; ctx . req . session ?. destroy ( ( err ) => { if ( err ) { console . log ( "destroy session failed" ) ; return ; } console . log ( "session destroyed" , ctx . req . session ?. userId ) ; } ) ; return result ; } catch ( ex ) { throw ex ; } } , changePassword : async ( obj , args , ctx , info ) : Promise < string > => { try { if ( ! ctx . req . session || ! ctx . req . session ! . userId ) { return "You must be logged in before you can change your password." ; } let result = await changePassword ( ctx . req . session ! . userId , args . newPassword ) ; return result ; } catch ( ex ) { throw ex ; } } , } , } ; export default resolvers ;