Suppose I have graphA and graphB both subgraph, and let query be teacher in graphA and students in graphB and they have common field teacherId, so I want to return list of students by teacher, One to many relation through entities. Please help
Hey @ankit9787 !
We have a course on Odyssey - Voyage I: Federation from Day One that is similar to what you’re describing.
Subgraph A - Locations (Teachers)
Subgraph B - Reviews (Students)
And in the course, we cover how to set it up so that a location has a list of reviews. In your case, how a teacher has a list of students.
Have you taken this course? If not, the particular lesson that delves into entities is Lesson 9: Connecting data using entities. The lessons after that dig deeper into the details of defining the entity, writing reference resolvers, etc.
Hope this helps, let us know if you need anything specific clarified!
Hi thank you for prompt reply, but let me explain my case -
In odyssey course, Reviews has location field added and referred using locationId field and now in locations we have use refResolver to return values I get that. But how to resolve condition which I mentioned below-
Suppose there is a type student-
so eg -
type student @key fields(“Id”){
name: String
teacherID: String
}
now writing refResolver in assesment-api class for above case will work, because it returns single entity (in bussiness logic it will be student.find()… etc)
now if it is
type student @key fields(“teacherID”){
name: String
teacherID: String
}
now if i write refResolved in student it is not working (bussines logic - Student.filter(Student.teacherId === teacherId)), basically if API returns list so result not coming, how to make this working ? when return type is list
Hey @ankit9787, I’m finding it a bit difficult to follow your example, so please let me know if I misunderstood something.
First, let me clarify. In the course, yes you can query for Review.location
. But you can also query for Location.reviewsForLocation
, so you get a list of reviews for a location. The Location.reviewsForLocation
field belongs in the reviews subgraph. From my understanding, that’s what you’re looking for, you want Teacher.students
, a list of students for a teacher, and you want that field to live in the students subgraph. So you should be able to use the course to implement your use case.
From your schema code snippets, it seems you’re misunderstanding what the @key
field directive is for. It’s an entity’s primary key, the field that can uniquely identify an instance of that entity within a subgraph. In your case, that should be the student’s id. The snippet is also missing the teachers
field. There are also a few typos, so please make sure to fix those in your code.
So your students subgraph schema should look something like this:
type Student @key(fields:"id"){
id: ID
# ...other fields
}
The reference resolver for the Student
type, as you said, would be something like student.find()
.
You shouldn’t use the teacherId
as the primary key for the Student entity (like you did in the second code snippet) because the teacherId
value uniquely identifies a teacher, not a student.
Next, we want to contribute a field to the Teacher
entity in the students subgraph: specifically the Teacher.students
field.
type Teacher @key(fields: "id") {
id: ID
students: [Student]
}
You’ll need to:
- make sure the
Teacher
type is defined as an entity in the teachers subgraph - write a reference resolver for the
Teacher
entity in the teachers subgraph - write a resolver for
Teacher.students
in the students subgraph
Again, all covered in this lesson.
Let me know if that helps, or if you can share your code, that might help explain your usecase a bit clearer. Thank you!