-
-
Notifications
You must be signed in to change notification settings - Fork 438
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
Add optimized selects #1626
base: master
Are you sure you want to change the base?
Add optimized selects #1626
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Select; | ||
|
||
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; | ||
|
||
class SelectDirective extends BaseDirective | ||
{ | ||
public static function definition(): string | ||
{ | ||
return /** @lang GraphQL */ <<<'GRAPHQL' | ||
""" | ||
Specify the SQL column dependencies of this field. | ||
""" | ||
directive @select( | ||
""" | ||
SQL columns names to pass to the Eloquent query builder | ||
""" | ||
columns: [String!] | ||
) on FIELD_DEFINITION | ||
GRAPHQL; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,99 @@ | ||||||||
<?php | ||||||||
|
||||||||
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; | ||||||||
|
||||||||
class SelectHelper | ||||||||
{ | ||||||||
/** | ||||||||
* Given a field definition node, resolve info, and a model name, return the SQL columns that should be selected. | ||||||||
* Accounts for relationships and the rename and select directives. | ||||||||
* | ||||||||
* @param mixed[] $fieldSelection | ||||||||
* @return string[] | ||||||||
*/ | ||||||||
public static function getSelectColumns(Node $definitionNode, array $fieldSelection, string $modelName): array | ||||||||
{ | ||||||||
$returnTypeName = ASTHelper::getUnderlyingTypeName($definitionNode); | ||||||||
|
||||||||
/** @var \Nuwave\Lighthouse\Schema\AST\DocumentAST $documentAST */ | ||||||||
$documentAST = app(ASTBuilder::class)->documentAST(); | ||||||||
|
||||||||
$type = $documentAST->types[$returnTypeName]; | ||||||||
// error_log(print_r($type, true)); | ||||||||
|
||||||||
if ($type instanceof UnionTypeDefinitionNode) { | ||||||||
$type = $documentAST->types[ASTHelper::getUnderlyingTypeName($type->types[0])]; | ||||||||
} | ||||||||
|
||||||||
|
||||||||
/** @var iterable<\GraphQL\Language\AST\FieldDefinitionNode> $fieldDefinitions */ | ||||||||
$fieldDefinitions = $type->fields; | ||||||||
|
||||||||
$model = new $modelName; | ||||||||
|
||||||||
$selectColumns = []; | ||||||||
|
||||||||
foreach ($fieldSelection as $field) { | ||||||||
$fieldDefinition = ASTHelper::firstByName($fieldDefinitions, $field); | ||||||||
|
||||||||
if ($fieldDefinition) { | ||||||||
$directivesRequiringLocalKey = ['hasOne', 'hasMany', 'count']; | ||||||||
$directivesRequiringForeignKey = ['belongsTo', 'belongsToMany', 'morphTo']; | ||||||||
$directivesRequiringKeys = array_merge($directivesRequiringLocalKey, $directivesRequiringForeignKey); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are trying to optimize performance here: Those should all be |
||||||||
|
||||||||
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); | ||||||||
|
||||||||
if (method_exists($model, $relationName)) { | ||||||||
array_push($selectColumns, $model->{$relationName}()->getLocalKeyName()); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
if (in_array($directiveType, $directivesRequiringForeignKey)) { | ||||||||
$relationName = ASTHelper::directiveArgValue($directive, 'relation', $field); | ||||||||
|
||||||||
if (method_exists($model, $relationName)) { | ||||||||
array_push($selectColumns, $model->{$relationName}()->getForeignKeyName()); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
continue 2; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
if (ASTHelper::hasDirective($fieldDefinition, 'select')) { | ||||||||
// append selected columns in select directive to seletion | ||||||||
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'select'); | ||||||||
|
||||||||
if ($directive) { | ||||||||
$selectFields = ASTHelper::directiveArgValue($directive, 'columns') ?? []; | ||||||||
$selectColumns = array_merge($selectColumns, $selectFields); | ||||||||
} | ||||||||
} elseif (ASTHelper::hasDirective($fieldDefinition, 'rename')) { | ||||||||
// append renamed attribute to selection | ||||||||
$directive = ASTHelper::directiveDefinition($fieldDefinition, 'rename'); | ||||||||
|
||||||||
if ($directive) { | ||||||||
$renamedAttribute = ASTHelper::directiveArgValue($directive, 'attribute'); | ||||||||
array_push($selectColumns, $renamedAttribute); | ||||||||
} | ||||||||
} else { | ||||||||
// fallback to selecting the field name | ||||||||
array_push($selectColumns, $field); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return array_unique($selectColumns); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, but if I need an accessor that is not in the table columns, we will get a "Column not found" SQL error.
Suggested change
|
||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This directive has no use without this argument.