diff --git a/src/Schema/Directives/RelationDirective.php b/src/Schema/Directives/RelationDirective.php index 0368ca6de..66f812525 100644 --- a/src/Schema/Directives/RelationDirective.php +++ b/src/Schema/Directives/RelationDirective.php @@ -72,10 +72,10 @@ public function resolveField(FieldValue $fieldValue): callable ) { $relationBatchLoader = BatchLoaderRegistry::instance( $this->qualifyPath($args, $resolveInfo), - static fn (): RelationBatchLoader => new RelationBatchLoader( + static fn(): RelationBatchLoader => new RelationBatchLoader( $paginationArgs === null - ? new SimpleModelsLoader($relationName, $decorateBuilder) - : new PaginatedModelsLoader($relationName, $decorateBuilder, $paginationArgs), + ? new SimpleModelsLoader($relationName, $decorateBuilder) + : new PaginatedModelsLoader($relationName, $decorateBuilder, $paginationArgs), ), ); @@ -129,7 +129,7 @@ protected function edgeType(DocumentAST $documentAST): ?ObjectTypeDefinitionNode { if ($edgeTypeName = $this->directiveArgValue('edgeType')) { $edgeType = $documentAST->types[$edgeTypeName] ?? null; - if (! $edgeType instanceof ObjectTypeDefinitionNode) { + if (!$edgeType instanceof ObjectTypeDefinitionNode) { throw new DefinitionException("The `edgeType` argument of @{$this->name()} on {$this->nodeName()} must reference an existing object type definition."); } @@ -171,6 +171,10 @@ protected function paginationDefaultCount(): ?int /** @param \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $relation */ protected function isSameConnection(Relation $relation): bool { + if ($this->lighthouseConfig['batchload_relations_only_on_same_connections'] === false) { + return true; + } + $default = $this->database->getDefaultConnection(); $parent = $relation->getParent()->getConnectionName() ?? $default; diff --git a/src/lighthouse.php b/src/lighthouse.php index fdf19b54b..be63d66bb 100644 --- a/src/lighthouse.php +++ b/src/lighthouse.php @@ -367,11 +367,14 @@ |-------------------------------------------------------------------------- | | If set to true, relations marked with directives like @hasMany or @belongsTo - | will be optimized by combining the queries through the BatchLoader. + | will be optimized by combining the queries through the BatchLoader. By default + | this optimization is limited to models on same connections but this can be + | changed. | */ 'batchload_relations' => true, + 'batchload_relations_only_on_same_connections' => true, /* |--------------------------------------------------------------------------