Skip to content

Dataloader #4

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

Merged
merged 6 commits into from
Nov 7, 2016
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
composer.phar
composer.lock
vendor
3 changes: 2 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
'types' => 'App\\Http\\GraphQL\\Types',
'fields' => 'App\\Http\\GraphQL\\Fields',
'connections' => 'App\\Http\\GraphQL\\Connections',
'dataloaders' => 'App\\Http\\GraphQL\\DataLoaders',
],

'cache' => storage_path('lighthouse/cache'),
'controller' => 'Nuwave\Lighthouse\Support\Http\Controllers\LaravelController@query',
'pagination_macro' => 'paginate',
'pagination_macro' => 'toConnection',
'route' => [],
'model_path' => 'App\\Models',
'camel_case' => false,
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<php>
<!-- Strong Password Generator (https://strongpasswordgenerator.com/) -->
<env name="APP_KEY" value="O4JYm19Y7qXbllmw5zng88a4MlUmW0uq" />
<env name="DB_CONNECTION" value="sqlite" />
<!-- <env name="DB_CONNECTION" value="sqlite" /> -->
</php>
</phpunit>
22 changes: 22 additions & 0 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nuwave\Lighthouse\Support\Cache\FileStore;
use Nuwave\Lighthouse\Schema\Field;
use Nuwave\Lighthouse\Schema\QueryParser;
use Nuwave\Lighthouse\Schema\FieldParser;
use Nuwave\Lighthouse\Schema\SchemaBuilder;

class GraphQL
Expand Down Expand Up @@ -158,6 +159,17 @@ public function edge($name, ObjectType $type = null, $fresh = false)
return $this->schema()->edgeInstance($name, $type, $fresh);
}

/**
* Extract Data Loader from IoC container.
*
* @param string $name
* @return \Nuwave\Lighthouse\Support\DataLoader\GraphQLDataLoader
*/
public function dataLoader($name)
{
return $this->schema()->dataLoaderInstance($name);
}

/**
* Get cursor encoder for connection edge.
*
Expand Down Expand Up @@ -293,4 +305,14 @@ public function parser()
{
return new QueryParser($this->schema(), $this->query);
}

/**
* Resolve instance of field parser.
*
* @return \Nuwave\Lighthouse\Schema\FieldParser
*/
public function fieldParser()
{
return app(FieldParser::class);
}
}
15 changes: 14 additions & 1 deletion src/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\LengthAwarePaginator;
use Nuwave\Lighthouse\Support\Traits\GlobalIdTrait;
use Nuwave\Lighthouse\Support\DataLoader\QueryBuilder;

class LaravelServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -90,7 +91,7 @@ protected function registerSchema()
*/
protected function registerMacro()
{
$name = $this->app['config']->get('lighthouse.pagination_macro') ?: 'paginate';
$name = $this->app['config']->get('lighthouse.pagination_macro') ?: 'toConnection';

$decodeCursor = function (array $args) {
return $this->decodeCursor($args);
Expand All @@ -108,5 +109,17 @@ protected function registerMacro()
$currentPage
);
});

Collection::macro('fetch', function ($relations) {
if (count($this->items) > 0) {
if (is_string($relations)) {
$relations = [$relations];
}

$query = $this->first()->newQuery()->with($relations);
$this->items = app(QueryBuilder::class)->eagerLoadRelations($query, $this->items);
}
return $this;
});
}
}
81 changes: 81 additions & 0 deletions src/Schema/FieldParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Nuwave\Lighthouse\Schema;

use GraphQL\Language\AST\SelectionSet;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Language\AST\Field as GraphQLField;
use GraphQL\Language\AST\FragmentDefinition;
use GraphQL\Language\AST\FragmentSpread;

class FieldParser
{
/**
* Prefetch data.
*
* @param ResolveInfo $info
* @param int $depth
* @return void
*/
public function fetch(ResolveInfo $info, $depth = 999999)
{
$fields = [];

/** @var GraphQLField $fieldAST */
foreach ($info->fieldASTs as $fieldAST) {
$fields = array_merge_recursive($fields, $this->foldSelectionSet($fieldAST->selectionSet, $depth));
}

return $fields;
}

/**
* Fold field selection set.
*
* @param SelectionSet $selectionSet
* @param int $descend
* @return array
*/
protected function foldSelectionSet(SelectionSet $selectionSet, $descend)
{
$fields = [];

foreach ($selectionSet->selections as $selectionAST) {
if ($selectionAST instanceof GraphQLField) {
$fields[$selectionAST->name->value] = $descend > 0 && !empty($selectionAST->selectionSet)
? $this->buildField($selectionAST, ['children' => $this->foldSelectionSet($selectionAST->selectionSet, $descend - 1)])
: $this->buildField($selectionAST);
} else if ($selectionAST instanceof FragmentSpread) {
$spreadName = $selectionAST->name->value;
if (isset($this->fragments[$spreadName])) {
/** @var FragmentDefinition $fragment */
$fragment = $this->fragments[$spreadName];
$fields += $this->foldSelectionSet($fragment->selectionSet, $descend);
}
}
}

return $fields;
}

/**
* Build field output.
*
* @param GraphQLField $field
* @param array $data
* @return array
*/
protected function buildField(GraphQLField $field, $data = [])
{
$args = [];

foreach ($field->arguments as $argument) {
$args[$argument->name->value] = $argument->value->value;
}

return array_merge([
'parent' => isset($data['children']),
'args' => $args,
], $data);
}
}
2 changes: 1 addition & 1 deletion src/Schema/Generators/ConnectionResolveGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function resolve($root, array $args, $context, ResolveInfo $info, $name)
return $items;
}

return $items->paginate($args);
return $items->toConnection($args);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/Schema/Registrars/DataLoaderRegistrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Nuwave\Lighthouse\Schema\Registrars;

class DataLoaderRegistrar
{
/**
* Add Data Loader to registrar.
*
* @param string $name
* @param string $loader
* @return bool
*/
public function register($name, $loader)
{
app()->singleton($loader);
app()->alias($loader, $this->alias($name));

$instance = app($this->alias($name));

if (empty($instance->getName())) {
$instance->setName($name);
}

return true;
}

/**
* Extract Data Loader from IoC Container.
*
* @param string $name
* @return \Nuwave\Lighthouse\Support\DataLoader\GraphQLDataLoader
*/
public function instance($name)
{
return app($this->alias($name));
}

/**
* Get alias of Data Loader.
*
* @param string $name
* @return string
*/
protected function alias($name)
{
return 'graphql.dataloader.'.$name;
}
}
23 changes: 23 additions & 0 deletions src/Schema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ public function cursor($name, Closure $encoder, Closure $decoder = null)
return $this->getCursorRegistrar()->register($name, $encoder, $decoder);
}

/**
* Add Data Loader to registrar.
*
* @param string $name
* @param string $loader
* @return bool
*/
public function dataLoader($name, $loader)
{
return $this->getDataLoaderRegistrar()->register($name, $loader);
}

/**
* Get type field from registrar.
*
Expand Down Expand Up @@ -205,6 +217,17 @@ public function edgeInstance($name, $type = null, $fresh = false)
return $this->getEdgeRegistrar()->instance($name, $fresh, $type);
}

/**
* Add Data Loader to registrar.
*
* @param string $name
* @return \Nuwave\Lighthouse\Support\DataLoader\GraphQLDataLoader
*/
public function dataLoaderInstance($name)
{
return $this->getDataLoaderRegistrar()->instance($name);
}

/**
* Get encoder for connection edge.
*
Expand Down
75 changes: 75 additions & 0 deletions src/Support/Console/Commands/DataLoaderMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Nuwave\Lighthouse\Support\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class MutationMakeCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'lighthouse:dataloader';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a GraphQL DataLoader.';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'DataLoader';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/dataloader.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return config('lighthouse.namespaces.dataloaders');
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [];
}

/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceClass($stub, $name)
{
$class = str_replace($this->getNamespace($name).'\\', '', $name);

return str_replace('DummyClass', $class, $stub);
}
}
25 changes: 25 additions & 0 deletions src/Support/Console/Commands/stubs/dataloader.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DummyNamespace;

use Nuwave\Lighthouse\Support\DataLoader\GraphQLDataLoader;

class DummyClass extends GraphQLDataLoader
{
/**
* Available fields and associated DataLoaders.
*
* @var array
*/
protected $children = [];

/**
* Create new instance of DataLoader.
*
* @return void
*/
public function __construct()
{
// ...
}
}
Loading