Skip to content

n+1 problem with eager loading models in different databases (connections) #2653

@wojo1206

Description

@wojo1206

What problem does this feature proposal attempt to solve?

I found n+1 problem with eager loading models in different databases (connections). Currently, the code will not eager load entitles that live on two different connections. I debug and tracked the problem to this specific line at src/Schema/Directives/RelationDirective.php:

if (
    $this->lighthouseConfig['batchload_relations']
    // Batch loading joins across both models, thus only works if they are on the same connection
    && $this->isSameConnection($relation) //  <-- Right here.
) {

Which possible solutions should be considered?

I don't know what is the rationale for this limitation but that should be OK by default to eager load models on different connections. To solve this problem we can either eliminate this limitation or introduce new config variable that allows to override this check at $this->isSameConnection($relation), something like:

    /*
    |--------------------------------------------------------------------------
    | Batchload Relations
    |--------------------------------------------------------------------------
    |
    | If set to true, relations marked with directives like @hasMany or @belongsTo
    | will be optimized by combining the queries through the BatchLoader.
    |
    */

    'batchload_relations' => true,
    'batchload_relations_only_on_same_connections' => true,

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionRequest for support or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions