-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
244 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Schema\Generators; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use Nuwave\Lighthouse\Support\Traits\GlobalIdTrait; | ||
|
||
class ConnectionResolveGenerator | ||
{ | ||
use GlobalIdTrait; | ||
|
||
/** | ||
* Attempt to auto-resolve connection. | ||
* | ||
* @param mixed $root | ||
* @param array $args | ||
* @param ResolveInfo $info | ||
* @param string $name | ||
* @return LengthAwarePaginator | ||
*/ | ||
public function resolve($root, array $args, ResolveInfo $info, $name) | ||
{ | ||
$items = $this->getItems($root, $info, $name); | ||
|
||
if (is_array($items)) { | ||
$items = collect($items); | ||
} else if (! $items instanceof Collection) { | ||
return $items; | ||
} | ||
|
||
return $items->paginate($args); | ||
} | ||
|
||
/** | ||
* @param $collection | ||
* @param ResolveInfo $info | ||
* @param $name | ||
* @return Collection|array | ||
*/ | ||
protected function getItems($collection, ResolveInfo $info, $name) | ||
{ | ||
if ($collection instanceof Model) { | ||
if (in_array($name, array_keys($collection->getRelations()))) { | ||
return $collection->{$name}; | ||
} | ||
|
||
return method_exists($collection, $name) | ||
? $collection->{$name}()->select($this->getSelectFields($info))->get() | ||
: $collection->getAttribute($name); | ||
} elseif (is_object($collection) && method_exists($collection, 'get')) { | ||
return $collection->get($name); | ||
} elseif (is_array($collection) && isset($collection[$name])) { | ||
return collect($collection[$name]); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Select only certain fields on queries instead of all fields. | ||
* | ||
* @param ResolveInfo $info | ||
* @return array | ||
*/ | ||
protected function getSelectFields(ResolveInfo $info) | ||
{ | ||
$camel = config('relay.camel_case'); | ||
$fields = array_get($info->getFieldSelection(2), 'edges.node'); | ||
|
||
return collect($fields)->reject(function ($value) { | ||
is_array($value); | ||
})->keys()->transform(function ($value) use ($camel) { | ||
return $camel ? snake_case($value) : $value; | ||
})->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace Nuwave\Tests\Queries; | ||
|
||
use GraphQL; | ||
use GraphQL\Type\Definition\Type; | ||
use Nuwave\Lighthouse\Tests\DBTestCase; | ||
use Nuwave\Lighthouse\Tests\Support\GraphQL\Types\TaskType; | ||
use Nuwave\Lighthouse\Tests\Support\Models\User; | ||
use Nuwave\Lighthouse\Tests\Support\Models\Task; | ||
use Nuwave\Lighthouse\Support\Definition\GraphQLType; | ||
use Nuwave\Lighthouse\Support\Interfaces\RelayType; | ||
use Nuwave\Lighthouse\Support\Traits\GlobalIdTrait; | ||
|
||
class AutoResolveTest extends DBTestCase | ||
{ | ||
use GlobalIdTrait; | ||
|
||
/** | ||
* User model. | ||
* | ||
* @var User | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* User assigned tasks. | ||
* | ||
* @var \Illuminate\Support\Collection | ||
*/ | ||
protected $tasks; | ||
|
||
/** | ||
* Set up test environment. | ||
*/ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = factory(User::class)->create(); | ||
|
||
$this->tasks = factory(Task::class, 6)->create([ | ||
'user_id' => $this->user->id | ||
]); | ||
|
||
$graphql = app('graphql'); | ||
$graphql->schema()->type('user', UserStubType::class); | ||
$graphql->schema()->type('task', TaskType::class); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itCanAutoResolveConnection() | ||
{ | ||
$data = $this->executeQuery($this->getQuery()); | ||
$edges = array_get($data, 'data.node.tasks.edges', []); | ||
|
||
$this->assertCount(2, $edges); | ||
} | ||
|
||
/** | ||
* Get query. | ||
* | ||
* @return string | ||
*/ | ||
protected function getQuery() | ||
{ | ||
$id = $this->encodeGlobalId(UserStubType::class, $this->user->id); | ||
|
||
return '{ | ||
node(id:"'.$id.'") { | ||
... on User { | ||
name | ||
tasks(first:2) { | ||
edges { | ||
node { | ||
title | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}'; | ||
} | ||
} | ||
|
||
class UserStubType extends GraphQLType implements RelayType | ||
{ | ||
protected $attributes = [ | ||
'name' => 'User', | ||
'description' => 'A user.' | ||
]; | ||
|
||
public function resolveById($id) | ||
{ | ||
return User::find($id); | ||
} | ||
|
||
public function fields() | ||
{ | ||
return [ | ||
'name' => [ | ||
'type' => Type::string(), | ||
'description' => 'Name of the user.' | ||
], | ||
'email' => [ | ||
'type' => Type::string(), | ||
'description' => 'Email of the user.' | ||
], | ||
'tasks' => GraphQL::connection('task')->field('tasks') | ||
]; | ||
} | ||
} |