scrnhq /
laravel-bakery
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bakery\Queries; |
||
| 4 | |||
| 5 | use Bakery\Utils\Utils; |
||
| 6 | use Illuminate\Support\Arr; |
||
| 7 | use Bakery\Support\Arguments; |
||
| 8 | use Bakery\Types\Definitions\RootType; |
||
| 9 | use Illuminate\Database\Eloquent\Model; |
||
| 10 | use GraphQL\Type\Definition\ResolveInfo; |
||
| 11 | use Illuminate\Database\Eloquent\Builder; |
||
| 12 | use Bakery\Exceptions\TooManyResultsException; |
||
| 13 | |||
| 14 | class SingleEntityQuery extends EloquentQuery |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Get the name of the query. |
||
| 18 | */ |
||
| 19 | public function name(): string |
||
| 20 | { |
||
| 21 | if (isset($this->name)) { |
||
| 22 | return $this->name; |
||
| 23 | } |
||
| 24 | |||
| 25 | return Utils::single($this->modelSchema->getTypename()); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The return type of the query. |
||
| 30 | */ |
||
| 31 | public function type(): RootType |
||
| 32 | { |
||
| 33 | return $this->registry->type($this->modelSchema->typename())->nullable(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The arguments for the Query. |
||
| 38 | */ |
||
| 39 | public function args(): array |
||
| 40 | { |
||
| 41 | return $this->modelSchema->getLookupTypes()->toArray(); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Resolve the EloquentQuery. |
||
| 46 | */ |
||
| 47 | public function resolve(Arguments $args, $root, $context, ResolveInfo $info): ?Model |
||
| 48 | { |
||
| 49 | $query = $this->scopeQuery($this->modelSchema->getQuery()); |
||
| 50 | |||
| 51 | $fields = $info->getFieldSelection(config('bakery.security.eagerLoadingMaxDepth')); |
||
| 52 | $this->eagerLoadRelations($query, $fields, $this->modelSchema); |
||
| 53 | |||
| 54 | if ($args->primaryKey) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 55 | return $query->find($args->primaryKey); |
||
| 56 | } |
||
| 57 | |||
| 58 | $results = $this->queryByArgs($query, $args)->get(); |
||
| 59 | |||
| 60 | if ($results->count() < 1) { |
||
| 61 | return null; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($results->count() > 1) { |
||
| 65 | throw (new TooManyResultsException)->setModel(get_class($this->model), |
||
| 66 | Arr::pluck($results, $this->model->getKeyName())); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $results->first(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Query by the arguments supplied to the query. |
||
| 74 | */ |
||
| 75 | protected function queryByArgs(Builder $query, Arguments $args): Builder |
||
| 76 | { |
||
| 77 | foreach ($args as $key => $value) { |
||
| 78 | if ($value instanceof Arguments) { |
||
| 79 | $query->whereHas($key, function (Builder $query) use ($value) { |
||
| 80 | $this->queryByArgs($query, $value); |
||
| 81 | }); |
||
| 82 | } else { |
||
| 83 | $query->where($key, $value); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return $query; |
||
| 88 | } |
||
| 89 | } |
||
| 90 |