I am writing a resolver (using sequelize) for my apollo-server and am struggling to understand how the resolve of this backwards association works... I am super confused with how the include
in my sequelize query is working.
my model associations:
User.hasOne(models.Profile) <- so the foreign key 'userId' is on the profile table
Profile.belongsTo(models.User) <- foreign key 'userId' still on profile table
my graphql schema:
type Profile {
id: ID!
user: User!
}
my resolver: (I can't query the User
model using where: {profileId: profile.id}
as the profileId
foreign key doesn't exist on User
) so... I use include..
Profile: {
user: async (profile, _args, { models }) => {
return await models.User.findOne({
include: [{
model: models.Profile,
where: { id: profile.id } <- this makes zero sense to me.. id is the id of the Profile row? how does this work??
}]
})