Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
DDynamic committed Jan 9, 2021
1 parent 326b6ab commit f8b749e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 53 deletions.
10 changes: 7 additions & 3 deletions src/Pagination/PaginateDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
);

if (config('lighthouse.optimized_selects')) {
$fieldSelection = array_keys($resolveInfo->getFieldSelection(2)['data']);
$selectColumns = SelectHelper::getSelectColumns($this->definitionNode, $fieldSelection, $this->getModelClass());
$query = $query->select($selectColumns);
$fieldSelection = $resolveInfo->getFieldSelection(4);

if (in_array('data', $fieldSelection) || in_array('edges', $fieldSelection)) {
$fieldSelection = array_keys(in_array($fieldSelection, 'data') ? $fieldSelection['data'] : $fieldSelection['edges']['node']);
$selectColumns = SelectHelper::getSelectColumns($this->definitionNode, $fieldSelection, $this->getModelClass());
$query = $query->select($selectColumns);
}
}

return PaginationArgs
Expand Down
9 changes: 6 additions & 3 deletions src/Schema/Directives/RelationDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Nuwave\Lighthouse\Select\SelectHelper;
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Illuminate\Support\Facades\DB;

abstract class RelationDirective extends BaseDirective implements FieldResolver
{
Expand Down Expand Up @@ -81,17 +82,19 @@ protected function makeBuilderDecorator(ResolveInfo $resolveInfo, Model $parent,
$this->directiveArgValue('scopes', [])
);

if (config('lighthouse.optimized_selects')) {
$builderDecorator->select(['*', DB::raw('"abc"')]);

/*if (config('lighthouse.optimized_selects')) {
$fieldSelection = array_keys($resolveInfo->getFieldSelection(1));
$selectColumns = SelectHelper::getSelectColumns($this->definitionNode, $fieldSelection, $this->getModelClass());
$selectColumns = SelectHelper::getSelectColumns($this->definitionNode, $fieldSelection, get_class($builderDecorator->getRelated()));
$foreignKeyName = $parent->{$relationName}()->getForeignKeyName();
if (! in_array($foreignKeyName, $selectColumns)) {
array_push($selectColumns, $foreignKeyName);
}
$builderDecorator->select($selectColumns);
}
}*/
};
}

Expand Down
76 changes: 31 additions & 45 deletions src/Select/SelectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nuwave\Lighthouse\Select;

use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use Illuminate\Database\Eloquent\Model;
use Nuwave\Lighthouse\Schema\AST\ASTBuilder;
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
Expand All @@ -25,6 +26,10 @@ public static function getSelectColumns(Node $definitionNode, array $fieldSelect

$type = $documentAST->types[$returnTypeName];

/*if ($type instanceof UnionTypeDefinitionNode) {
$type = $documentAST->types[ASTHelper::getUnderlyingTypeName($type->types[0])];
}*/

/** @var iterable<\GraphQL\Language\AST\FieldDefinitionNode> $fieldDefinitions */
$fieldDefinitions = $type->fields;

Expand All @@ -36,7 +41,31 @@ public static function getSelectColumns(Node $definitionNode, array $fieldSelect
$fieldDefinition = ASTHelper::firstByName($fieldDefinitions, $field);

if ($fieldDefinition) {
$name = $fieldDefinition->name->value;
$directivesRequiringLocalKey = ['hasOne', 'hasMany', 'count', 'morphOne', 'morphMany'];
$directivesRequiringForeignKey = ['belongsTo', 'belongsToMany', 'morphOne', 'morphMany', 'morphTo'];
$directivesRequiringKeys = array_merge($directivesRequiringLocalKey, $directivesRequiringForeignKey);

foreach ($directivesRequiringKeys as $directiveType) {
if (ASTHelper::hasDirective($fieldDefinition, $directiveType)) {
$directive = ASTHelper::directiveDefinition($fieldDefinition, $directiveType);

if (in_array($directiveType, $directivesRequiringLocalKey)) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $field);
array_push($selectColumns, $model->{$relationName}()->getLocalKeyName());
}

if (in_array($directiveType, $directivesRequiringForeignKey)) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $field);
array_push($selectColumns, $model->{$relationName}()->getForeignKeyName());
}

if ($directiveType === 'morphTo') {
array_push($selectColumns, $model->{$relationName}()->getMorphType());
}

continue 2;
}
}

if (ASTHelper::hasDirective($fieldDefinition, 'select')) {
// append selected columns in select directive to seletion
Expand All @@ -54,52 +83,9 @@ public static function getSelectColumns(Node $definitionNode, array $fieldSelect
$renamedAttribute = ASTHelper::directiveArgValue($directive, 'attribute');
array_push($selectColumns, $renamedAttribute);
}
} elseif (ASTHelper::hasDirective($fieldDefinition, 'count')) {
// append relationship local key
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'count');

if ($directive) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $name);

if ($relationName) {
array_push($selectColumns, $model->{$relationName}()->getLocalKeyName());
}
}
} elseif (ASTHelper::hasDirective($fieldDefinition, 'hasOne')) {
// append relationship local key
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'hasOne');

if ($directive) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $name);
array_push($selectColumns, $model->{$relationName}()->getLocalKeyName());
}
} elseif (ASTHelper::hasDirective($fieldDefinition, 'hasMany')) {
// append relationship local key
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'hasMany');

if ($directive) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $name);
array_push($selectColumns, $model->{$relationName}()->getLocalKeyName());
}
} elseif (ASTHelper::hasDirective($fieldDefinition, 'belongsTo')) {
// append relationship foreign key
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'belongsTo');

if ($directive) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $name);
array_push($selectColumns, $model->{$relationName}()->getForeignKeyName());
}
} elseif (ASTHelper::hasDirective($fieldDefinition, 'belongsToMany')) {
// append relationship foreign key
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'belongsToMany');

if ($directive) {
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $name);
array_push($selectColumns, $model->{$relationName}()->getForeignKeyName());
}
} else {
// fallback to selecting the field name
array_push($selectColumns, $name);
array_push($selectColumns, $field);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions tests/DBTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;

abstract class DBTestCase extends TestCase
{
Expand Down Expand Up @@ -39,13 +40,17 @@ protected function getEnvironmentSetUp($app): void
{
parent::getEnvironmentSetUp($app);

Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
error_log($query->sql . ' - ' . serialize($query->bindings));
});

$app['config']->set('database.default', 'mysql');
$app['config']->set('database.connections.mysql', [
'driver' => 'mysql',
'database' => env('LIGHTHOUSE_TEST_DB_DATABASE', 'test'),
'host' => env('LIGHTHOUSE_TEST_DB_HOST', 'mysql'),
'host' => env('LIGHTHOUSE_TEST_DB_HOST', 'localhost'),
'username' => env('LIGHTHOUSE_TEST_DB_USERNAME', 'root'),
'password' => env('LIGHTHOUSE_TEST_DB_PASSWORD', ''),
'password' => env('LIGHTHOUSE_TEST_DB_PASSWORD', 'U1eky8aXNdI68DoB'),
]);
}
}

0 comments on commit f8b749e

Please sign in to comment.