From f977a625e3756ed5a4690a72aff14e7c8bbcbe79 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 1 Mar 2024 21:12:27 +0100 Subject: [PATCH 1/4] feat: allow constraining `ToMany` query --- src/Laravel/EloquentBuffer.php | 9 ++++++++- src/Schema/Field/ToMany.php | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Laravel/EloquentBuffer.php b/src/Laravel/EloquentBuffer.php index 08df728..d7e534c 100644 --- a/src/Laravel/EloquentBuffer.php +++ b/src/Laravel/EloquentBuffer.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo; use Tobyz\JsonApiServer\Context; use Tobyz\JsonApiServer\Schema\Field\Relationship; +use Tobyz\JsonApiServer\Schema\Field\ToMany; abstract class EloquentBuffer { @@ -56,7 +57,13 @@ public static function load( $modelClass = get_class($resource->newModel($context)); if ($resource instanceof EloquentResource && !isset($constrain[$modelClass])) { - $constrain[$modelClass] = fn($query) => $resource->scope($query, $context); + $constrain[$modelClass] = function ($query) use ($resource, $context, $relationship) { + $resource->scope($query, $context); + + if ($relationship instanceof ToMany && $relationship->constrain) { + ($relationship->constrain)($query, $context); + } + }; } } diff --git a/src/Schema/Field/ToMany.php b/src/Schema/Field/ToMany.php index 04d3405..7015288 100644 --- a/src/Schema/Field/ToMany.php +++ b/src/Schema/Field/ToMany.php @@ -2,12 +2,15 @@ namespace Tobyz\JsonApiServer\Schema\Field; +use Closure; use Tobyz\JsonApiServer\Context; use Tobyz\JsonApiServer\Exception\BadRequestException; use Tobyz\JsonApiServer\Exception\Sourceable; class ToMany extends Relationship { + public ?Closure $constrain = null; + public function __construct(string $name) { parent::__construct($name); @@ -15,6 +18,13 @@ public function __construct(string $name) $this->type($name); } + public function constrain(?Closure $constrain): static + { + $this->constrain = $constrain; + + return $this; + } + public function serializeValue($value, Context $context): mixed { $meta = $this->serializeMeta($context); From 05a800e5f98514c2fc6545c621965b10394aae62 Mon Sep 17 00:00:00 2001 From: SychO9 Date: Fri, 1 Mar 2024 20:13:09 +0000 Subject: [PATCH 2/4] Run Prettier --- src/Laravel/EloquentBuffer.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Laravel/EloquentBuffer.php b/src/Laravel/EloquentBuffer.php index d7e534c..3c407bb 100644 --- a/src/Laravel/EloquentBuffer.php +++ b/src/Laravel/EloquentBuffer.php @@ -57,7 +57,11 @@ public static function load( $modelClass = get_class($resource->newModel($context)); if ($resource instanceof EloquentResource && !isset($constrain[$modelClass])) { - $constrain[$modelClass] = function ($query) use ($resource, $context, $relationship) { + $constrain[$modelClass] = function ($query) use ( + $resource, + $context, + $relationship, + ) { $resource->scope($query, $context); if ($relationship instanceof ToMany && $relationship->constrain) { From bcc879bf784cc50e6f1ff00bf849293858bf36fe Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 30 Mar 2024 12:28:28 +0100 Subject: [PATCH 3/4] chore: eloquent-specific relationships --- src/Laravel/EloquentBuffer.php | 7 ++++--- .../Field/Concerns/ScopesRelationship.php | 17 +++++++++++++++++ src/Laravel/Field/ToMany.php | 10 ++++++++++ src/Laravel/Field/ToOne.php | 10 ++++++++++ src/Schema/Field/ToMany.php | 9 --------- 5 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 src/Laravel/Field/Concerns/ScopesRelationship.php create mode 100644 src/Laravel/Field/ToMany.php create mode 100644 src/Laravel/Field/ToOne.php diff --git a/src/Laravel/EloquentBuffer.php b/src/Laravel/EloquentBuffer.php index 3c407bb..a9feab9 100644 --- a/src/Laravel/EloquentBuffer.php +++ b/src/Laravel/EloquentBuffer.php @@ -6,8 +6,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; use Tobyz\JsonApiServer\Context; +use Tobyz\JsonApiServer\Laravel\Field\ToMany; +use Tobyz\JsonApiServer\Laravel\Field\ToOne; use Tobyz\JsonApiServer\Schema\Field\Relationship; -use Tobyz\JsonApiServer\Schema\Field\ToMany; abstract class EloquentBuffer { @@ -64,8 +65,8 @@ public static function load( ) { $resource->scope($query, $context); - if ($relationship instanceof ToMany && $relationship->constrain) { - ($relationship->constrain)($query, $context); + if (($relationship instanceof ToMany || $relationship instanceof ToOne) && $relationship->scope) { + ($relationship->scope)($query, $context); } }; } diff --git a/src/Laravel/Field/Concerns/ScopesRelationship.php b/src/Laravel/Field/Concerns/ScopesRelationship.php new file mode 100644 index 0000000..5117917 --- /dev/null +++ b/src/Laravel/Field/Concerns/ScopesRelationship.php @@ -0,0 +1,17 @@ +scope = $scope; + + return $this; + } +} diff --git a/src/Laravel/Field/ToMany.php b/src/Laravel/Field/ToMany.php new file mode 100644 index 0000000..6f7d11a --- /dev/null +++ b/src/Laravel/Field/ToMany.php @@ -0,0 +1,10 @@ +type($name); } - public function constrain(?Closure $constrain): static - { - $this->constrain = $constrain; - - return $this; - } - public function serializeValue($value, Context $context): mixed { $meta = $this->serializeMeta($context); From 5dae9964f7cef6f4a1cde7e2c4c3c38a8787c692 Mon Sep 17 00:00:00 2001 From: SychO9 Date: Sat, 30 Mar 2024 11:29:35 +0000 Subject: [PATCH 4/4] Run Prettier --- src/Laravel/EloquentBuffer.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Laravel/EloquentBuffer.php b/src/Laravel/EloquentBuffer.php index a9feab9..4e914fa 100644 --- a/src/Laravel/EloquentBuffer.php +++ b/src/Laravel/EloquentBuffer.php @@ -65,7 +65,11 @@ public static function load( ) { $resource->scope($query, $context); - if (($relationship instanceof ToMany || $relationship instanceof ToOne) && $relationship->scope) { + if ( + ($relationship instanceof ToMany || + $relationship instanceof ToOne) && + $relationship->scope + ) { ($relationship->scope)($query, $context); } };