This is my code of mutation in which i use the type of User which has name,email,password and i make two mutations for the register the user and login the user. I have searched all the docs about the graphql and read all the blogs related to authentication but can't got the answer to return the token from the mutation
const mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
addUser: {
type: UserType,
args: {
name: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
avatar: { type: GraphQLString }
},
resolve(parentValue, args) {
const avatar = gravatar.url(args.email);
return bcrypt
.hash(args.password, 10)
.then(hash => {
args.password = hash;
const newUser = new User({
name: args.name,
email: args.email,
password: args.password,
avatar
});
return newUser
.save()
.then(user => user)
.catch(e => e);
})
.catch(e => e);
}
},
login: {
name: "Login",
type: UserType,
args: {
email: { type: GraphQLString },
password: { type: GraphQLString }
},
resolve(parentValue, args, context) {
return User.findOne({ email: args.email })
.then(user => {
if (user) {
return bcrypt
.compare(args.password, user.password)
.then(isValid => {
if (!isValid) {
throw new Error({ message: "password Incrrect" });
} else {
const token = jwt.sign(
{ name: user.name, id: user.id },
"mySecret"
);
return user;
}
})
.catch(e => e);
} else {
throw new Error({ message: "email Incorrect" });
}
})
.catch(e => e);
}
}
}
});
This is my User Type
const UserType = new GraphQLObjectType({
name: "User",
fields: {
id: { type: GraphQLString },
name: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
avatar: { type: GraphQLString }
}
});