Skip to content

Commit

Permalink
Manipulator::getPlaceholderTypeDefinitionNode() will support Lighth…
Browse files Browse the repository at this point in the history
…ouse `RelationDirective`.
  • Loading branch information
LastDragon-ru committed Jan 26, 2024
1 parent e65075d commit 59a6624
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
47 changes: 25 additions & 22 deletions packages/graphql/src/Builder/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\Source;
use LastDragon_ru\LaraASP\GraphQL\Stream\Directives\Directive as StreamDirective;
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;
use LastDragon_ru\LaraASP\GraphQL\Utils\PaginateDirectiveHelper;
use LastDragon_ru\LaraASP\GraphQL\Utils\RelationDirectiveHelper;
use Nuwave\Lighthouse\Pagination\PaginateDirective;
use Nuwave\Lighthouse\Pagination\PaginationType;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Nuwave\Lighthouse\Schema\Directives\RelationDirective;
use Nuwave\Lighthouse\Support\Contracts\Directive;
use Override;

use function array_map;
Expand Down Expand Up @@ -314,35 +317,35 @@ public function getPlaceholderTypeDefinitionNode(
): TypeDefinitionNode|Type|null {
$node = $this->getTypeDefinition($field);
$name = $this->getTypeName($node);
$directives = [
StreamDirective::class,
PaginateDirective::class,
];
$directives = $this->getDirectives($field, null, static function (Directive $directive): bool {
return $directive instanceof StreamDirective
|| $directive instanceof PaginateDirective
|| $directive instanceof RelationDirective;
});

foreach ($directives as $directive) {
$directive = $this->getDirective($field, $directive);
$type = null;
$type = null;

if ($directive instanceof StreamDirective) {
$type = Str::singular(mb_substr($name, 0, -mb_strlen(StreamDirective::Name)));
} elseif ($directive instanceof PaginateDirective) {
$pagination = (new class() extends PaginateDirective {
public function getPaginationType(PaginateDirective $directive): PaginationType {
return $directive->paginationType();
} elseif ($directive instanceof PaginateDirective || $directive instanceof RelationDirective) {
$pagination = $directive instanceof PaginateDirective
? PaginateDirectiveHelper::getPaginationType($directive)
: RelationDirectiveHelper::getPaginationType($directive);

if ($pagination) {
if ($pagination->isPaginator()) {
$type = mb_substr($name, 0, -mb_strlen('Paginator'));
} elseif ($pagination->isSimple()) {
$type = mb_substr($name, 0, -mb_strlen('SimplePaginator'));
} elseif ($pagination->isConnection()) {
$type = mb_substr($name, 0, -mb_strlen('Connection'));
} else {
// empty
}
})->getPaginationType($directive);

if ($pagination->isPaginator()) {
$type = mb_substr($name, 0, -mb_strlen('Paginator'));
} elseif ($pagination->isSimple()) {
$type = mb_substr($name, 0, -mb_strlen('SimplePaginator'));
} elseif ($pagination->isConnection()) {
$type = mb_substr($name, 0, -mb_strlen('Connection'));
} else {
// empty
}
} else {
// empty
// empty
}

if ($type) {
Expand Down
31 changes: 31 additions & 0 deletions packages/graphql/src/Builder/ManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,37 @@ public static function dataProviderGetPlaceholderTypeDefinitionNode(): array {
}
GRAPHQL,
],
'@hasOne' => [
'Test',
<<<'GRAPHQL'
type Query {
field: [Test!]
@hasOne(
model: "\\LastDragon_ru\\LaraASP\\GraphQL\\Testing\\Package\\Data\\Models\\TestObject"
)
}
type Test {
field: Int
}
GRAPHQL,
],
'@hasMany(type: PAGINATOR)' => [
'Test',
<<<'GRAPHQL'
type Query {
field: [Test!]
@hasMany(
model: "\\LastDragon_ru\\LaraASP\\GraphQL\\Testing\\Package\\Data\\Models\\TestObject"
type: PAGINATOR
)
}
type Test {
field: Int
}
GRAPHQL,
],
'@stream' => [
'Test',
<<<'GRAPHQL'
Expand Down
15 changes: 15 additions & 0 deletions packages/graphql/src/Utils/PaginateDirectiveHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\GraphQL\Utils;

use Nuwave\Lighthouse\Pagination\PaginateDirective;
use Nuwave\Lighthouse\Pagination\PaginationType;

/**
* @internal
*/
abstract class PaginateDirectiveHelper extends PaginateDirective {
public static function getPaginationType(PaginateDirective $directive): PaginationType {
return $directive->paginationType();
}
}

0 comments on commit 59a6624

Please sign in to comment.