import { AuthenticationError , ForbiddenError } from 'apollo-server' ; import { MyContext } from './MyContext' ; const resolvers = { Query : { launch ( _ , __ , { isAuthenticated } ) { if ( ! isAuthenticated ( ) ) { throw new ForbiddenError ( 'You need to be logged in to access launch queries' ) ; } return { id : 'LAUNCH_QUERIES' , } ; } , me : ( _ , __ , { getUser } ) => getUser ( ) , version : ( ) => '1.0' , } , LaunchQueries : { find ( parent , { id } , { dataSources : { launchAPI } } ) { return launchAPI . find ( parseInt ( id , 10 ) ) ; } , } , Mutation : { async login ( _ , { name , password } , { authenticate , login } ) { const { user , info } = await authenticate ( 'graphql-local' , { username : name , password } ) ; if ( ! user ) { throw new AuthenticationError ( `Failed to login: ${ info }` ) ; } await login ( user ) ; return user ; } , async logout ( _ , __ , { logout } ) { try { logout ( ) ; } catch { return false ; } return true ; } , launch ( _ , __ , { isAuthenticated , getUser } ) { const user = getUser ( ) ; if ( ! isAuthenticated ( ) || ! user ) { throw new ForbiddenError ( 'You need to be logged in to access launch mutations' ) ; } if ( user . permission !== 'admin' ) { throw new ForbiddenError ( 'You have insufficient privileges for launch mutations' ) ; } return { id : 'LAUNCH_MUTATIONS' , } ; } , } , LaunchMutations : { add ( parent , { name } , { dataSources : { launchAPI } } ) { const newLaunch = launchAPI . add ( name ) ; return newLaunch ; } , } , } ; export default resolvers ;