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

Limit Query Select to Model Table #1253

Draft
wants to merge 10 commits 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
21 changes: 19 additions & 2 deletions src/Execution/Arguments/ArgumentSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Nuwave\Lighthouse\Execution\Arguments;

use Closure;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use Nuwave\Lighthouse\Schema\Directives\RenameDirective;
use Nuwave\Lighthouse\Schema\Directives\SpreadDirective;
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
Expand Down Expand Up @@ -137,16 +140,30 @@ function ($value) {
/**
* Apply ArgBuilderDirectives and scopes to the builder.
*
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $builder
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
* @param string[] $scopes
* @param \Closure $directiveFilter
*
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Relations\Relation
*/
public function enhanceBuilder($builder, array $scopes, Closure $directiveFilter = null)
{
self::applyArgBuilderDirectives($this, $builder, $directiveFilter);

$baseBuilder = null;
if ($builder instanceof Builder) {
$baseBuilder = $builder;
} elseif ($builder instanceof EloquentBuilder) {
$baseBuilder = $builder->getQuery();
} elseif ($builder instanceof Relation) {
$baseBuilder = $builder->getBaseQuery();
}

if ($baseBuilder && is_null($baseBuilder->columns)) {
// Fix a long standing issue within Eloquent https://github.com/laravel/framework/issues/4962
$builder->select($baseBuilder->from.'.*');
}

foreach ($scopes as $scope) {
call_user_func([$builder, $scope], $this->toArray());
}
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Database/ColumnSelectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Unit\Database;

use Tests\DBTestCase;
use Tests\Utils\Models\Team;
use Tests\Utils\Models\User;

class ColumnSelectTest extends DBTestCase
{
protected $schema = /** @lang GraphQL */ '
type Query {
usersWithTeam(
team: ID! @scope(name: "withTeam")
): [User!] @all
}

type User {
id: ID!
team: Team @belongsTo
}

type Team {
id: ID!
}
';

public function testSelectsSameColumn(): void
{
$team = factory(Team::class)->create();
factory(User::class, 2)->create([
'team_id' => $team->id,
]);

$this->graphQL(/** @lang GraphQL */ '
query {
usersWithTeam(team: 1) {
id
}
}
')->assertJson([
'data' => [
'usersWithTeam' => [
[
'id' => '1',
],
[
'id' => '2',
],
],
],
]);
}
}
7 changes: 7 additions & 0 deletions tests/Utils/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public function scopeNamed(Builder $query): Builder
return $query->whereNotNull('name');
}

public function scopeWithTeam(Builder $builder, $teamId): Builder
{
return $builder
->join('teams', 'teams.id', '=', 'users.team_id')
->where('teams.id', '=', $teamId);
}

public function tasksLoaded(): bool
{
return $this->relationLoaded('tasks');
Expand Down