I'm trying to create a mutation in GraphQL that charges the user with Stripe but I can't get the user back client side. Everything works well in my database, the payment is done but on my client side the .then() is firing too early with a result of null while it should return me the edited user. It's actually firing before the stripe.charges has finished on my server.
Server side :
chargeUser(args) {
const { userId, membershipId, stripeToken } = args;
return User.findById(userId)
.then((user) => {
return Membership.findById(membershipId)
.then((membership) => {
return stripe.charges.create({
amount: membership.price * 100,
currency: 'chf',
source: stripeToken,
description: `${user.firstname} ${user.lastname}`
}, function(err) {
if (!err) {
user.membership = membershipId;
return user.save();
} else {
console.log(err)
}
});
})
});
}
client side :
this.props.mutate({
variables: {
membershipId: this.props.membershipId,
userId: global.userId,
stripeToken: token.tokenId
}
})
.then((res) => {
// Returning null before the stripe.charges has finished
console.log(res)
})