'how to set one to many relation in nestjs graphql
this is my relation user entity
@OneToMany(() => DoctorSchedule, doctorSchedule => doctorSchedule.user)
@Field(type => DoctorSchedule)
@JoinColumn({ name: "doctorId" })
doctorSchedules?: DoctorSchedule[];
this is my resolver
@ResolveField((returns) => DoctorSchedule)
@UseGuards(JwtAuthGuard)
doctorSchedules(@Parent() user: User): Promise<DoctorSchedule[]> {
return this.usersService.getDoctorSchedule(user.id);
}
this is my doctor schedule entity
@ManyToOne(() => User, user => user.doctorSchedules)
@Field(type => User, { nullable: true })
user?: User;
this is my rsollver doctor schedule
@ResolveField(returns => User)
// @UseGuards(JwtAuthGuard)
user(@Parent() doctorSchedule: DoctorSchedule): Promise<User> {
return this.doctorScheduleService.getUser(doctorSchedule.doctorId);
}
many to one relation is working like:
query{
doctorSchedules{
day
}user(id:11){
first_name
last_name
}
}
output is good
but one to many is not working like:
query{
user(id:11){
email
first_name
doctorSchedules{
day
}
}
}
its giver me error "Cannot return null for non-nullable field DoctorSchedule.day." please help me
Solution 1:[1]
Make sure you have the relationship in your service
Example:
method() {
return yourRepository.find({ relations: ['doctorSchedules'] });
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jefferson |
