Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Oitzinger <[email protected]>
  • Loading branch information
toitzi committed Nov 12, 2024
1 parent 1cf0a13 commit c2e66d3
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions src/Laravel/Eloquent/State/LinksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

namespace ApiPlatform\Laravel\Eloquent\State;

use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -37,22 +40,72 @@ public function handleLinks(Builder $builder, array $uriVariables, array $contex
$identifier = $uriVariables[$uriVariable];

if ($to = $link->getToProperty()) {
$builder = $builder->where($builder->getModel()->getTable().'.'.$builder->getModel()->{$to}()->getForeignKeyName(), $identifier);
$builder = $builder->where($builder->getModel()->{$to}()->getQualifiedForeignKeyName(), $identifier);

continue;
}

if ($from = $link->getFromProperty()) {
$relation = $this->application->make($link->getFromClass());
$builder = $builder->getModel()->where($builder->getModel()->getTable().'.'.$relation->{$from}()->getForeignKeyName(), $identifier);
$builder = $builder->getModel()->where($relation->{$from}()->getQualifiedForeignKeyName(), $identifier);

continue;
}

$builder->where($builder->getModel()->getTable().'.'.$link->getIdentifiers()[0], $identifier);
$builder->where($builder->getModel()->qualifyColumn($link->getIdentifiers()[0]), $identifier);
}

return $builder;
} elseif ($operation instanceof GraphQlOperation) {
if (!($linkClass = $context['linkClass'] ?? false)) {
return $builder;
}

$newLink = null;
$linkProperty = $context['linkProperty'] ?? null;

try {
$resourceMetadataCollection = $this->application->make(ResourceMetadataCollectionFactoryInterface::class)->create($linkClass);
$linkedOperation = $resourceMetadataCollection->getOperation($operation->getName());
} catch (OperationNotFoundException $e) {
// Instead, we'll look for the first Query available.
foreach ($resourceMetadataCollection as $resourceMetadata) {
foreach ($resourceMetadata->getGraphQlOperations() as $op) {
if ($op instanceof Query) {
$linkedOperation = $op;
}
}
}
}

if (!$linkedOperation) {
return $builder;
}

$resourceClass = $builder->getModel()::class;
foreach ($linkedOperation->getLinks() ?? [] as $link) {
if ($resourceClass === $link->getToClass() && $linkProperty === $link->getFromProperty()) {
$newLink = $link;
break;
}
}

if (!$newLink) {
return $builder;
}

$identifier = $uriVariables[$newLink->getIdentifiers()[0]];

if ($to = $newLink->getToProperty()) {
return $builder->where($builder->getModel()->{$to}()->getQualifiedForeignKeyName(), $identifier);
}

if ($from = $newLink->getFromProperty()) {
$relation = $this->application->make($newLink->getFromClass());
return $builder->getModel()->where($relation->{$from}()->getQualifiedForeignKeyName(), $identifier);
}

$builder->where($builder->getModel()->qualifyColumn($newLink->getIdentifiers()[0]), $identifier);
}

return $builder;
Expand Down

0 comments on commit c2e66d3

Please sign in to comment.