Description
We open this task in order to get feedback for the generated-code API of eager loading.
The 3 requirements (maybe there are more) we thought about are:
- Allow filter edges/relations (using
.Where
) - Get nested edges/relations
- Limit/offset capabilities
A few examples here for what we thought about:
Add a With<EdgeName>
method to the builder:
client.User.
Query().
Where(...).
WithPets(). // populate all user pets.
WithGroups(group.HasAdmin()). // filter out groups without admin.
All(ctx)
This example gets all users under a condition + their pets + their groups (with an admin).
What about getting all users + their groups (limit to X) + their admins (eager-loading with depth 2)?
I'm not sure how common this scenario, so I think either disable this, or add a With<Edge>Fn
option for more flexible loading:
client.User.
Query().
Where(...).
WithGroupsFn(func(q *ent.GroupQuery) {
q.Where(...).Limit(X).WithAdmin()
}).
All(ctx)
Maybe we can change With<Edge>
to behave like With<Edge>Fn
and make the function to be an optional parameter.
The return type for eager loading:
We were thinking about the 2 options:
-
Add the schema edges as public fields in the generated struct (e.g.
ent.User
), and then populate the result into these fields. -
Add another struct
type
(struct) for holding the schema edges. For example:type UserEdges struct { Pets []*Pet // ... }
I'm not sure how this is supposed to handle nested eager-loading (User->Pets->Friends)?