We are using Sequelize with a AWS Aurora Postgresql database with a read and write database. Sequelize is configured to use the read replica and write databases.
We have a users
table and follows
table. The User
object type has fields that are subqueries, for instance the field followingCount on the user:
...
followingCount: {
type: new GraphQLNonNull(GraphQLInt),
resolve(user) {
return db.Follower.count({
where: { followerId: user.id, status: "Approved" }
});
}
},
When we add a record to the follows
table for a user, then return that user instance, the followingCount
is not updated. I'm 99% confident this is because it's reading from the replica/read database which does not have the data. I've verified this by disabling the replica and seeing the issue disappear.
I'm hesitant to add the useMaster
option to these fields, as it negates our concurrency strategy.
Here is the exact code I am calling:
const followingUser = await db.User.findOne({
where: {
id
}
});
await db.Follower.create({
followerId: getCurrentUserId(context),
followingId: id,
status: followingUser.privateProfile ? "Pending" : "Approved"
});
return followingUser.reload();
The followingCount
field on the user instance(followingUser
) isn't returning the newest count even though it exists on the master/write db.
What options are there? Is there someway I can wrap my mutations in a block so that anything executed within uses the Master/Write database? Would wrapping it in a transaction help, and if so how do I pass the transaction down to my
ANY help appreciated
psql 9.6.8
sequelize 4.38.0