-
-
Notifications
You must be signed in to change notification settings - Fork 439
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
6 changed files
with
180 additions
and
7 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,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; | ||
} | ||
} |
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,109 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Select; | ||
|
||
use GraphQL\Language\AST\Node; | ||
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]; | ||
|
||
/** @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) { | ||
$name = $fieldDefinition->name->value; | ||
|
||
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); | ||
} | ||
} 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); | ||
} | ||
} | ||
} | ||
|
||
return array_unique($selectColumns); | ||
} | ||
} |
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