Skip to content

Commit

Permalink
refactor(graphql)!: Added TypeSource argument to `BuilderInfoProvid…
Browse files Browse the repository at this point in the history
…er::getBuilderInfo()`.
  • Loading branch information
LastDragon-ru committed Sep 15, 2023
1 parent b2196b0 commit f29215a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
21 changes: 10 additions & 11 deletions packages/graphql/src/Builder/BuilderInfoDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Collection;
use Laravel\Scout\Builder as ScoutBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderInfoProvider;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\BuilderUnknown;
use LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithManipulator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithSource;
Expand Down Expand Up @@ -52,13 +53,12 @@ public function getFieldBuilderInfo(
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode $type,
FieldDefinitionNode $field,
): BuilderInfo {
$builder = $this->getBuilderInfo($field);
$manipulator = $this->getAstManipulator($document);
$fieldSource = $this->getFieldSource($manipulator, $type, $field);
$builder = $this->getBuilderInfo($field, $fieldSource);

if (!$builder) {
$manipulator = $this->getAstManipulator($document);
$argSource = $this->getFieldSource($manipulator, $type, $field);

throw new BuilderUnknown($argSource);
throw new BuilderUnknown($fieldSource);
}

return $builder;
Expand All @@ -70,26 +70,25 @@ public function getFieldArgumentBuilderInfo(
FieldDefinitionNode $field,
InputValueDefinitionNode $argument,
): BuilderInfo {
$builder = $this->getBuilderInfo($field);
$manipulator = $this->getAstManipulator($document);
$argSource = $this->getFieldArgumentSource($manipulator, $type, $field, $argument);
$builder = $this->getBuilderInfo($field, $argSource);

if (!$builder) {
$manipulator = $this->getAstManipulator($document);
$argSource = $this->getFieldArgumentSource($manipulator, $type, $field, $argument);

throw new BuilderUnknown($argSource);
}

return $builder;
}

protected function getBuilderInfo(Node $node): ?BuilderInfo {
protected function getBuilderInfo(Node $node, TypeSource $source): ?BuilderInfo {
// Provider?
$provider = $this->locator->associated($node)->first(static function (Directive $directive): bool {
return $directive instanceof BuilderInfoProvider;
});

if ($provider instanceof BuilderInfoProvider) {
$builder = $provider->getBuilderInfo();
$builder = $provider->getBuilderInfo($source);
$instance = $builder
? $this->getBuilderInfoInstance($builder)
: null;
Expand Down
15 changes: 9 additions & 6 deletions packages/graphql/src/Builder/BuilderInfoDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Illuminate\Database\Query\Builder as QueryBuilder;
use Laravel\Scout\Builder as ScoutBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderInfoProvider;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\TestCase;
use Mockery;
use Nuwave\Lighthouse\Pagination\PaginateDirective;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Nuwave\Lighthouse\Schema\Directives\AggregateDirective;
Expand Down Expand Up @@ -44,14 +46,15 @@ class BuilderInfoDetectorTest extends TestCase {
*/
public function testGetNodeBuilderInfo(array $expected, Closure $nodeFactory): void {
$locator = $this->app->make(DirectiveLocator::class);
$source = Mockery::mock(TypeSource::class);
$node = $nodeFactory($locator);
$directive = new class($locator) extends BuilderInfoDetector {
public function getBuilderInfo(Node $node): ?BuilderInfo {
return parent::getBuilderInfo($node);
public function getBuilderInfo(Node $node, TypeSource $source): ?BuilderInfo {
return parent::getBuilderInfo($node, $source);
}
};

$actual = $directive->getBuilderInfo($node);
$actual = $directive->getBuilderInfo($node, $source);

self::assertEquals(
$expected,
Expand Down Expand Up @@ -294,7 +297,7 @@ public static function definition(): string {
throw new Exception('should not be called.');
}

public function getBuilderInfo(): BuilderInfo|string|null {
public function getBuilderInfo(TypeSource $source): BuilderInfo|string|null {
return new BuilderInfo('Custom', BuilderInfoProvider::class);
}
})::class,
Expand Down Expand Up @@ -323,7 +326,7 @@ public static function definition(): string {
throw new Exception('should not be called.');
}

public function getBuilderInfo(): BuilderInfo|string|null {
public function getBuilderInfo(TypeSource $source): BuilderInfo|string|null {
return EloquentBuilder::class;
}
})::class,
Expand Down Expand Up @@ -352,7 +355,7 @@ public static function definition(): string {
throw new Exception('should not be called.');
}

public function getBuilderInfo(): BuilderInfo|string|null {
public function getBuilderInfo(TypeSource $source): BuilderInfo|string|null {
return EloquentModel::class;
}
})::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ interface BuilderInfoProvider {
/**
* @return BuilderInfo|class-string<EloquentBuilder<EloquentModel>|EloquentModel|QueryBuilder|ScoutBuilder|Collection<array-key, mixed>>|null
*/
public function getBuilderInfo(): BuilderInfo|string|null;
public function getBuilderInfo(TypeSource $source): BuilderInfo|string|null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laravel\Scout\Builder as ScoutBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\BuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderInfoProvider;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static function getName(): string {
return '@exposeBuilder'.hash('sha256', static::class);
}

public function getBuilderInfo(): BuilderInfo|string|null {
public function getBuilderInfo(TypeSource $source): BuilderInfo|string|null {
return static::$builder::class;
}

Expand Down

0 comments on commit f29215a

Please sign in to comment.