Skip to content

Commit

Permalink
refactor(graphql)!: WithBuilderInfo::getBuilderInfo() will accept a…
Browse files Browse the repository at this point in the history
…ny `Node`.
  • Loading branch information
LastDragon-ru committed Aug 15, 2023
1 parent aacd7b1 commit a4dcdbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
17 changes: 10 additions & 7 deletions packages/graphql/src/Builder/Traits/WithBuilderInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
Expand Down Expand Up @@ -58,10 +59,10 @@ protected function getFieldArgumentBuilderInfo(
return $builder;
}

protected function getBuilderInfo(FieldDefinitionNode $field): ?BuilderInfo {
protected function getBuilderInfo(Node $node): ?BuilderInfo {
// Provider?
$directives = Container::getInstance()->make(DirectiveLocator::class);
$provider = $directives->associated($field)->first(static function (Directive $directive): bool {
$provider = $directives->associated($node)->first(static function (Directive $directive): bool {
return $directive instanceof BuilderInfoProvider;
});

Expand All @@ -72,10 +73,12 @@ protected function getBuilderInfo(FieldDefinitionNode $field): ?BuilderInfo {
// Scout?
$scout = false;

foreach ($field->arguments as $argument) {
if ($directives->associatedOfType($argument, SearchDirective::class)->isNotEmpty()) {
$scout = true;
break;
if ($node instanceof FieldDefinitionNode) {
foreach ($node->arguments as $argument) {
if ($directives->associatedOfType($argument, SearchDirective::class)->isNotEmpty()) {
$scout = true;
break;
}
}
}

Expand All @@ -84,7 +87,7 @@ protected function getBuilderInfo(FieldDefinitionNode $field): ?BuilderInfo {
}

// Builder?
$directive = $directives->associated($field)->first(static function (Directive $directive): bool {
$directive = $directives->associated($node)->first(static function (Directive $directive): bool {
return $directive instanceof AllDirective
|| $directive instanceof PaginateDirective
|| $directive instanceof BuilderDirective
Expand Down
15 changes: 8 additions & 7 deletions packages/graphql/src/Builder/Traits/WithBuilderInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Exception;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\Parser;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
Expand Down Expand Up @@ -37,14 +38,14 @@ class WithBuilderInfoTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
/**
* @dataProvider dataProviderGetBuilderInfo
* @dataProvider dataProviderGetNodeBuilderInfo
*
* @param array{name: string, builder: string} $expected
* @param Closure(DirectiveLocator): FieldDefinitionNode $fieldFactory
* @param array{name: string, builder: string} $expected
* @param Closure(DirectiveLocator): Node $nodeFactory
*/
public function testGetBuilderInfo(array $expected, Closure $fieldFactory): void {
public function testGetNodeBuilderInfo(array $expected, Closure $nodeFactory): void {
$directives = $this->app->make(DirectiveLocator::class);
$field = $fieldFactory($directives);
$node = $nodeFactory($directives);
$directive = new class() extends BaseDirective {
use WithBuilderInfo {
getBuilderInfo as public;
Expand All @@ -55,7 +56,7 @@ public static function definition(): string {
}
};

$actual = $directive->getBuilderInfo($field);
$actual = $directive->getBuilderInfo($node);

self::assertEquals(
$expected,
Expand All @@ -75,7 +76,7 @@ public static function definition(): string {
* Closure(DirectiveLocator): FieldDefinitionNode,
* }>
*/
public static function dataProviderGetBuilderInfo(): array {
public static function dataProviderGetNodeBuilderInfo(): array {
return [
'unknown' => [
[
Expand Down

0 comments on commit a4dcdbe

Please sign in to comment.