nuwave / nuwave/lighthouse

Deduplicate nested relations using dot notation with equivalent fields

Open
#1,873 5 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement performance
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:

image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.