graphql 多重关联
graphql multiple associations
我在sequelize中有以下模型
用户
User.associate = models => {
User.hasMany(models.Schedule, {
onDelete: 'CASCADE',
foreignKey: 'patient_id',
sourceKey: 'id',
as: 'patient'
});
User.hasMany(models.Schedule, {
onDelete: 'CASCADE',
foreignKey: 'professional_id',
sourceKey: 'id',
as: 'professional'
});
};
日程安排
Schedule.associate = models => {
Schedule.belongsTo(models.User, {
foreignKey: 'patient_id',
targetKey: 'id',
as: 'patient'
});
};
Schedule.associate = models => {
Schedule.belongsTo(models.User, {
foreignKey: 'professional_id',
targetKey: 'id',
as: 'professional'
});
};
以及 graphql 中的以下模式
用户
type User {
id: ID!
schedules1: [Schedule] @relation(name: "patient")
schedules2: [Schedule] @relation(name: "professional")
}
日程安排
type Schedule {
id: ID!
date : Date
patient: User! @relation(name: "patient")
professional: User! @relation(name: "professional")
}
但是当尝试使用这样的时间表查询用户时
{
users{
id
name
schedules1{
id
}
}
}
我得到了以下结果
{
"data": {
"users": [
{
"id": "1",
"name": "Gregorio",
"schedules1": null
},
...
我的问题是,我如何在 graphql 中对多个关联进行建模,我尝试了注解 @relation 但没有成功。
菜鸟错误...我忘了将解析器添加到关系中。
User: {
patient: async(user, args, { models }) =>
await models.Schedule.findAll({
where: {
patient_id: user.id,
},
}),
professional: async(user, args, { models }) =>
await models.Schedule.findAll({
where: {
professional_id: user.id,
},
})
},
我在sequelize中有以下模型
用户
User.associate = models => {
User.hasMany(models.Schedule, {
onDelete: 'CASCADE',
foreignKey: 'patient_id',
sourceKey: 'id',
as: 'patient'
});
User.hasMany(models.Schedule, {
onDelete: 'CASCADE',
foreignKey: 'professional_id',
sourceKey: 'id',
as: 'professional'
});
};
日程安排
Schedule.associate = models => {
Schedule.belongsTo(models.User, {
foreignKey: 'patient_id',
targetKey: 'id',
as: 'patient'
});
};
Schedule.associate = models => {
Schedule.belongsTo(models.User, {
foreignKey: 'professional_id',
targetKey: 'id',
as: 'professional'
});
};
以及 graphql 中的以下模式
用户
type User {
id: ID!
schedules1: [Schedule] @relation(name: "patient")
schedules2: [Schedule] @relation(name: "professional")
}
日程安排
type Schedule {
id: ID!
date : Date
patient: User! @relation(name: "patient")
professional: User! @relation(name: "professional")
}
但是当尝试使用这样的时间表查询用户时
{
users{
id
name
schedules1{
id
}
}
}
我得到了以下结果
{
"data": {
"users": [
{
"id": "1",
"name": "Gregorio",
"schedules1": null
},
...
我的问题是,我如何在 graphql 中对多个关联进行建模,我尝试了注解 @relation 但没有成功。
菜鸟错误...我忘了将解析器添加到关系中。
User: {
patient: async(user, args, { models }) =>
await models.Schedule.findAll({
where: {
patient_id: user.id,
},
}),
professional: async(user, args, { models }) =>
await models.Schedule.findAll({
where: {
professional_id: user.id,
},
})
},