import { GraphQLString , GraphQLNonNull } from 'graphql' ; import { mutationWithClientMutationId } from 'graphql-relay' ; import { GraphQLContext } from '../../../TypeDefinition' ; import UserType from '../UserType' ; import * as UserLoader from '../UserLoader' ; export default mutationWithClientMutationId ( { name : 'UserChangePassword' , inputFields : { oldPassword : { type : new GraphQLNonNull ( GraphQLString ) , } , password : { type : new GraphQLNonNull ( GraphQLString ) , description : 'user new password' , } , } , mutateAndGetPayload : async ( { oldPassword , password } , { user } ) => { if ( ! user ) { return { error : 'User not authenticated' , } ; } const correctPassword = user . authenticate ( oldPassword ) ; if ( ! correctPassword ) { return { error : 'INVALID_PASSWORD' , } ; } user . password = password ; await user . save ( ) ; return { error : null , } ; } , outputFields : { error : { type : GraphQLString , resolve : ( { error } ) => error , } , me : { type : UserType , resolve : ( obj , args , context ) => UserLoader . load ( context , context . user . id ) , } , } , } ) ;