Deduplicate nested relations using dot notation with equivalent fields
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.5k
- Forks
- 468
- Avg merge
- 3h 9m
- Merged PRs (30d)
- 2
Description
What problem does this feature proposal attempt to solve?
See https://github.com/nuwave/lighthouse/pull/1871. Database queries are unnecessarily duplicated.
Which possible solutions should be considered?
When using dot notation to batch load a relation, e.g. @with(relation: "foo.bar.baz"), we would have to pick apart the individual relation segments and resolve a batch loader for each one separately.
Rough draft of how that might look:
/** @var array<int, \Nuwave\Lighthouse\Execution\BatchLoader\RelationBatchLoader> $relationBatchLoader */
$loaders = [];
// Dot notation may be used to eager load nested relations
$parts = explode('.', $this->relation());
// Includes the field we are loading the relation for
$path = $resolveInfo->path;
// In case we have no args, we can combine eager loads that are the same
if ($args === []) {
array_pop($path);
}
$lastRelation = array_pop($parts);
foreach ($parts as $intermediaryRelation) {
$path []= $intermediaryRelation;
$loaders []= BatchLoaderRegistry::instance(
$intermediaryRelation,
function () use ($resolveInfo, $intermediaryRelation): RelationBatchLoader {
return new RelationBatchLoader($this->relationLoader($resolveInfo, $intermediaryRelation));
}
);
}
$path []= $lastRelation;
$path = array_merge($path, $this->scopes());
$loaders []= BatchLoaderRegistry::instance(
$path,
function () use ($resolveInfo, $lastRelation): RelationBatchLoader {
return new RelationBatchLoader($this->relationLoader($resolveInfo, $lastRelation));
}
);
return new Deferred(static function () use ($loaders, $parent) {
/** @var RelationBatchLoader $loader */
foreach ($loaders as $loader) {
$loader->load($parent);
}
});
Scopes and additional constraints only apply to the last relation query, so the intermediary queries can safely be batched together with other unconstrained queries on the same relation:

Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the @with relation batch-loading path and inspect relation(), BatchLoaderRegistry, and RelationBatchLoader. Trace how dot-notated segments and resolveInfo paths form loaders, then verify that intermediary unconstrained relations are deduplicated while scopes and constraints remain on the final relation query.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, laravel, php
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100