Say I have a StudentService
with a method that adds lessons to a student and a LessonService
with a method that adds students to a lesson. In both my Lesson and Student Resolvers I want to be able to update this lesson <---> student relationship. So in my LessonResolver
I have something along the lines of:
async assignStudentsToLesson(
@Args('assignStudentsToLessonInput')
assignStudentsToLesson: AssignStudentsToLessonInput,
) {
const { lessonId, studentIds } = assignStudentsToLesson;
await this.studentService.assignLessonToStudents(lessonId, studentIds); **** A.1 ****
return this.lessonService.assignStudentsToLesson(lessonId, studentIds); **** A.2 ****
}
and essentially the reverse in my StudentResolver
The difference between A.1 and A.2 above is that the StudentService
has access to the StudentRepository
and the LessonService
has access to the LessonRepository
- which I believe adheres to a solid separation of concerns.
However, it seems to be an anti-pattern that the StudentModule
must import the LessonModule
and the LessonModule
must import the StudentModule
. This is fixable using the forwardRef
method, but in the NestJS Documentation it mentions this pattern should be avoided if possible:
While circular dependencies should be avoided where possible, you can't always do so. (is this one of those cases?)
This seems like it should be a common issue when using DI, but I'm struggling to get a definitive answer as to what options are available that can eliminate this situation, or if I've stumbled upon a situation where it's unavoidable.
The ultimate goal is for me to be able to write the two GraphQL queries below:
query {
students {
firstName
lessons {
name
}
}
}
query {
lessons {
name
students {
firstName
}
}
}
Aucun commentaire:
Enregistrer un commentaire