I see that it's possible to use Data Loaders for root queries, but is it also possible to use Data Loaders for nested connections? In the example below, I want use a Data Loader for the rooms
property. In the example request at the bottom, there will be three database queries made. One by the data loader to fetch both buildings, one to fetch the rooms for building 1, and another to fetch the rooms for building 2. Instead, I'm trying to use a data loader for the rooms, so only two database queries are made.
// Building DB table
ID | Name
1 | Main Campus
2 | Satellite Campus
// Rooms DB table
ID | BuildingId | Name
1 | 1 | Lab
2 | 1 | Dorm
3 | 2 | Theatre
4 | 2 | Gym
// Schema
type Building {
id: Int!
name: String!
rooms(after: String before: String first: PaginationAmount last: PaginationAmount): RoomsConnection
}
type Room {
id: Int!
name: String!
building: Building!
}
// Hot Chocolate
public class BuildingType: ObjectType<Building> {
protected override void Configure(IObjectTypeDescriptor<Building> descriptor)
{
// ... omitted other fields for brevity
// Instead of using a resolver, can a data loader be used instead?
descriptor.Field(b => b.rooms).UsePaging<RoomType>().Resolver(ctx => {
var building = ctx.Parent<Building>();
var roomsRepository = ctx.Service<IRoomsRepository>();
return roomsRepository.GetRoomsByBuildingId(building.Id);
});
}
}
// Example request
query {
a: building(id: 1){
id,
name,
rooms {
nodes {
id,
name
}
}
},
b: building(id: 2){
id,
name,
rooms {
nodes {
id,
name
}
}
}
}