Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide override for eager loading models on different connections #2654

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Schema/Directives/RelationDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Comment on lines +75 to +78
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert reformatting.

),
);

Expand Down Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/lighthouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

/*
|--------------------------------------------------------------------------
Expand Down