Skip to content

Commit c780deb

Browse files
committed
[graphql] @sortBy: MorphOne support.
1 parent 0d53a8c commit c780deb

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ The main feature - the ability to sort results by relation properties, at the mo
351351

352352
- `HasOne` (https://laravel.com/docs/8.x/eloquent-relationships#one-to-one)
353353
- `BelongsTo` (https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse)
354+
- `MorphOne` (https://laravel.com/docs/8.x/eloquent-relationships#one-to-one-polymorphic-relations)
354355

355356

356357
How to use:

src/SortBy/SortBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
66
use Illuminate\Database\Eloquent\Relations\BelongsTo;
77
use Illuminate\Database\Eloquent\Relations\HasOne;
8+
use Illuminate\Database\Eloquent\Relations\MorphOne;
89
use Illuminate\Database\Eloquent\Relations\Relation;
910
use Illuminate\Database\Query\Builder as QueryBuilder;
1011
use LastDragon_ru\LaraASP\GraphQL\Helpers\ModelHelper;
@@ -27,6 +28,7 @@ class SortBuilder {
2728
protected array $relations = [
2829
BelongsTo::class,
2930
HasOne::class,
31+
MorphOne::class,
3032
];
3133

3234
public function __construct(
@@ -155,7 +157,7 @@ protected function processRelation(
155157
? "{$parentAlias}.{$relation->getForeignKeyName()}"
156158
: $relation->getQualifiedForeignKeyName(),
157159
);
158-
} elseif ($relation instanceof HasOne) {
160+
} elseif ($relation instanceof HasOne || $relation instanceof MorphOne) {
159161
$builder = $builder->leftJoinSub(
160162
$relation->getQuery(),
161163
$alias,

src/SortBy/SortBuilderTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
1111
use Illuminate\Database\Eloquent\Relations\HasOne;
12+
use Illuminate\Database\Eloquent\Relations\MorphOne;
1213
use Illuminate\Database\Query\Builder as QueryBuilder;
1314
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\BuilderDataProvider;
1415
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase;
@@ -150,6 +151,7 @@ static function (): EloquentBuilder {
150151
implode('`, `', [
151152
BelongsTo::class,
152153
HasOne::class,
154+
MorphOne::class,
153155
]),
154156
)),
155157
static function (): EloquentBuilder {
@@ -282,6 +284,63 @@ static function (): EloquentBuilder {
282284
],
283285
],
284286
],
287+
'eloquent: '.MorphOne::class => [
288+
[
289+
'sql' => ''.
290+
'select'.
291+
' "table_a".*,'.
292+
' "table_alias_0"."name" as "table_alias_0_name",'.
293+
' "table_alias_0"."created_at" as "table_alias_0_created_at",'.
294+
' "table_alias_1"."name" as "table_alias_1_name",'.
295+
' "table_alias_1"."created_at" as "table_alias_1_created_at" '.
296+
'from "table_a" '.
297+
'left join (select * from "table_b" where'.
298+
' "table_b"."morphable_a_id" = ? and "table_b"."morphable_a_id" is not null and'.
299+
' "table_b"."morphable_a_type" = ? and "c" = ?'.
300+
') as "table_alias_0" on "table_alias_0"."morphable_a_id" = "table_a"."id" '.
301+
'left join (select * from "table_c" where'.
302+
' "table_c"."morphable_b_id" = ? and "table_c"."morphable_b_id" is not null and'.
303+
' "table_c"."morphable_b_type" = ?'.
304+
') as "table_alias_1" on "table_alias_1"."morphable_b_id" = "table_alias_0"."id" '.
305+
'order by'.
306+
' "table_alias_0_name" asc,'.
307+
' "table_alias_0_created_at" desc,'.
308+
' "table_alias_1_name" desc,'.
309+
' "table_alias_1_created_at" desc,'.
310+
' "table_a"."name" asc',
311+
'bindings' => [
312+
12,
313+
SortBuilderTest__ModelA::class,
314+
'c',
315+
56,
316+
SortBuilderTest__ModelB::class,
317+
],
318+
],
319+
static function (): EloquentBuilder {
320+
return SortBuilderTest__ModelA::query();
321+
},
322+
[
323+
[
324+
'morphOneB' => ['name' => 'asc'],
325+
],
326+
[
327+
'morphOneB' => ['created_at' => 'desc'],
328+
],
329+
[
330+
'morphOneB' => [
331+
'morphOneC' => ['name' => 'desc'],
332+
],
333+
],
334+
[
335+
'morphOneB' => [
336+
'morphOneC' => ['created_at' => 'desc'],
337+
],
338+
],
339+
[
340+
'name' => 'asc',
341+
],
342+
],
343+
],
285344
];
286345
}
287346
// </editor-fold>
@@ -322,6 +381,12 @@ public function hasOneB(): HasOne {
322381
->where('b', '=', 'b');
323382
}
324383

384+
public function morphOneB(): MorphOne {
385+
return $this
386+
->morphOne(SortBuilderTest__ModelB::class, 'morphable_a')
387+
->where('c', '=', 'c');
388+
}
389+
325390
public function unsupported(): HasMany {
326391
return $this->hasMany(SortBuilderTest__ModelB::class);
327392
}
@@ -354,6 +419,10 @@ public function belongsToC(): BelongsTo {
354419
public function hasOneC(): HasOne {
355420
return $this->hasOne(SortBuilderTest__ModelC::class, 'model_b_id');
356421
}
422+
423+
public function morphOneC(): MorphOne {
424+
return $this->morphOne(SortBuilderTest__ModelC::class, 'morphable_b');
425+
}
357426
}
358427

359428
/**

0 commit comments

Comments
 (0)