-
-
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.
Merge branch 'v2-beta-dataloader' into v2-beta
- Loading branch information
Showing
6 changed files
with
192 additions
and
23 deletions.
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
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
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,62 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Support\DataLoader; | ||
|
||
use GraphQL\Deferred; | ||
|
||
abstract class BatchLoader | ||
{ | ||
/** | ||
* Keys to resolve. | ||
* | ||
* @var \Illuminate\Support\Collection | ||
*/ | ||
protected $keys = []; | ||
|
||
/** | ||
* Check if data has been loaded. | ||
* | ||
* @var bool | ||
*/ | ||
protected $hasLoaded = false; | ||
|
||
/** | ||
* Load object by key. | ||
* | ||
* @param mixed $key | ||
* @param array $data | ||
* | ||
* @return Deferred | ||
*/ | ||
public function load($key, array $data = []) | ||
{ | ||
$this->keys[$key] = $data; | ||
|
||
return new Deferred(function () use ($key) { | ||
if (! $this->hasLoaded) { | ||
$this->resolve(); | ||
$this->hasLoaded = true; | ||
} | ||
|
||
return array_get($this->keys, "$key.value"); | ||
}); | ||
} | ||
|
||
/** | ||
* Set key value. | ||
* | ||
* @param mixed $key | ||
* @param mixed $value | ||
*/ | ||
protected function set($key, $value) | ||
{ | ||
if ($field = array_get($this->keys, $key)) { | ||
$this->keys[$key] = array_merge($field, compact('value')); | ||
} | ||
} | ||
|
||
/** | ||
* Resolve keys. | ||
*/ | ||
abstract public function resolve(); | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Support\DataLoader\Loaders; | ||
|
||
use Nuwave\Lighthouse\Support\DataLoader\BatchLoader; | ||
|
||
class BelongsToLoader extends BatchLoader | ||
{ | ||
/** | ||
* Resolve keys. | ||
*/ | ||
public function resolve() | ||
{ | ||
collect($this->keys)->map(function ($item) { | ||
return array_merge($item, ['json' => json_encode($item['args'])]); | ||
})->groupBy('json')->each(function ($items) { | ||
$relation = array_get($items->first(), 'relation'); | ||
$models = $items->pluck('root'); | ||
|
||
$models->fetch([$relation]); | ||
$models->each(function ($model) use ($relation) { | ||
$this->set($model->id, $model->getRelation($relation)); | ||
}); | ||
}); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Support\DataLoader\Loaders; | ||
|
||
use Nuwave\Lighthouse\Support\DataLoader\BatchLoader; | ||
use Nuwave\Lighthouse\Support\Traits\HandlesGlobalId; | ||
|
||
class HasManyLoader extends BatchLoader | ||
{ | ||
use HandlesGlobalId; | ||
|
||
/** | ||
* Resolve keys. | ||
*/ | ||
public function resolve() | ||
{ | ||
collect($this->keys)->map(function ($item) { | ||
return array_merge($item, ['json' => json_encode($item['args'])]); | ||
})->groupBy('json')->each(function ($items) { | ||
$first = $items->first(); | ||
$parents = $items->pluck('parent'); | ||
$scopes = array_get($first, 'scopes', []); | ||
$relation = $first['relation']; | ||
$type = $first['type']; | ||
$args = $first['args']; | ||
|
||
$constraints = [$relation => function ($q) use ($scopes, $args) { | ||
foreach ($scopes as $scope) { | ||
call_user_func_array([$q, $scope], [$args]); | ||
} | ||
}]; | ||
|
||
switch ($type) { | ||
case 'relay': | ||
$first = data_get($args, 'first', 15); | ||
$after = $this->decodeCursor($args); | ||
$currentPage = $first && $after ? floor(($first + $after) / $first) : 1; | ||
$parents->fetchForPage($first, $currentPage, $constraints); | ||
break; | ||
case 'paginator': | ||
$first = data_get($args, 'count', 15); | ||
$page = data_get($args, 'page', 1); | ||
$parents->fetchForPage($first, $page, $constraints); | ||
break; | ||
default: | ||
$parents->fetch($constraints); | ||
break; | ||
} | ||
|
||
$parents->each(function ($model) use ($relation) { | ||
$this->set($model->id, $model->getRelation($relation)); | ||
}); | ||
}); | ||
} | ||
} |