Skip to content

Commit

Permalink
refactor(graphql)!: AstManipulator::getOriginTypeDefinition() => (m…
Browse files Browse the repository at this point in the history
…ore useful) `AstManipulator::getOriginType()`.
  • Loading branch information
LastDragon-ru committed Jan 27, 2024
1 parent 342a170 commit 2f29617
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
2 changes: 1 addition & 1 deletion packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ This section is actual only if you are extending the package. Please review and
$context->get(LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextBuilderInfo::class)?->value
```

* [ ] Removed `getPlaceholderTypeDefinitionNode()` => `LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator::getOriginTypeDefinition()`
* [ ] Removed `getPlaceholderTypeDefinitionNode()` => `LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator::getOriginType()`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Directives\HandlerDirective`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected function getArgumentTypeDefinitionNode(
ContextContract $context,
): ListTypeNode|NamedTypeNode|NonNullTypeNode|null {
$definition = $context->get(HandlerContextImplicit::class)?->value
? $manipulator->getOriginTypeDefinition($argument->getField())
? $manipulator->getTypeDefinition($manipulator->getOriginType($argument->getField()))
: $argument->getTypeDefinition();
$operator = $manipulator->getOperator(static::getScope(), $operator);
$node = $manipulator->getTypeSource($definition);
Expand Down
67 changes: 32 additions & 35 deletions packages/graphql/src/Utils/AstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,52 +296,49 @@ public function removeTypeDefinition(string $name): void {
}

/**
* @return (TypeDefinitionNode&Node)|Type
* @return (TypeNode&Node)|Type
*/
public function getOriginTypeDefinition(
public function getOriginType(
FieldDefinitionNode|FieldDefinition|InputValueDefinitionNode|InputObjectField $field,
): TypeDefinitionNode|Type {
$node = $this->getTypeDefinition($field);
$name = $this->getTypeName($node);
$directives = $this->getDirectives($field, null, static function (Directive $directive): bool {
): TypeNode|Type {
$directive = $this->getDirective($field, Directive::class, static function (Directive $directive): bool {
return $directive instanceof StreamDirective
|| $directive instanceof PaginateDirective
|| $directive instanceof RelationDirective;
});

foreach ($directives as $directive) {
$type = null;

if ($directive instanceof StreamDirective) {
$type = Str::singular(mb_substr($name, 0, -mb_strlen(StreamDirective::Name)));
} 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
}
$origin = $field instanceof FieldDefinition || $field instanceof InputObjectField
? $field->getType()
: $field->type;
$name = $this->getTypeName($origin);
$type = null;

if ($directive instanceof StreamDirective) {
$type = Str::singular(mb_substr($name, 0, -mb_strlen(StreamDirective::Name)));
} 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
}
} else {
// empty
}
} else {
// empty
}

if ($type) {
$node = $this->getTypeDefinition($type);

break;
}
if ($type) {
$origin = Parser::typeReference("[{$type}!]!");
}

return $node;
return $origin;
}

/**
Expand Down
28 changes: 15 additions & 13 deletions packages/graphql/src/Utils/AstManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ static function (mixed $field) use ($manipulator): bool {
}

/**
* @dataProvider dataProviderGetOriginTypeDefinition
* @dataProvider dataProviderGetOriginType
*/
public function testGetOriginTypeDefinition(string $expected, string $graphql): void {
public function testGetOriginType(string $expected, string $graphql): void {
$ast = Mockery::mock(DocumentAST::class);
$types = Container::getInstance()->make(TypeRegistry::class);
$directives = Container::getInstance()->make(DirectiveLocator::class);
Expand All @@ -590,9 +590,9 @@ public function testGetOriginTypeDefinition(string $expected, string $graphql):

self::assertNotNull($field);

$type = $manipulator->getOriginTypeDefinition($field);
$type = $manipulator->getOriginType($field);

self::assertEquals($expected, $manipulator->getName($type));
self::assertGraphQLPrintableEquals($expected, $type);
}
//</editor-fold>

Expand Down Expand Up @@ -1210,7 +1210,7 @@ interface InterfaceC {
/**
* @return array<string, array{string,string}>
*/
public static function dataProviderGetOriginTypeDefinition(): array {
public static function dataProviderGetOriginType(): array {
return [
'field nullable' => [
'Test',
Expand All @@ -1225,7 +1225,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'field not null' => [
'Test',
'Test!',
<<<'GRAPHQL'
type Query {
field: Test! @mock
Expand All @@ -1237,7 +1237,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'list' => [
'Test',
'[Test]',
<<<'GRAPHQL'
type Query {
field: [Test] @mock
Expand All @@ -1249,7 +1249,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'@paginate(type: PAGINATOR)' => [
'Test',
'[Test!]!',
<<<'GRAPHQL'
type Query {
field: [Test!]
Expand All @@ -1265,7 +1265,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'@paginate(type: SIMPLE)' => [
'Test',
'[Test!]!',
<<<'GRAPHQL'
type Query {
field: [Test!]
Expand All @@ -1281,7 +1281,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'@paginate(type: CONNECTION)' => [
'Test',
'[Test!]!',
<<<'GRAPHQL'
type Query {
field: [Test!]
Expand All @@ -1297,7 +1297,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'@hasOne' => [
'Test',
'[Test!]',
<<<'GRAPHQL'
type Query {
field: [Test!]
Expand All @@ -1312,7 +1312,7 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'@hasMany(type: PAGINATOR)' => [
'Test',
'[Test!]!',
<<<'GRAPHQL'
type Query {
field: [Test!]
Expand All @@ -1328,12 +1328,14 @@ public static function dataProviderGetOriginTypeDefinition(): array {
GRAPHQL,
],
'@stream' => [
'Test',
'[Test!]!',
<<<'GRAPHQL'
type Query {
field: [Test!]
@stream(
key: "id"
searchable: false
sortable: false
builder: {
model: "\\LastDragon_ru\\LaraASP\\GraphQL\\Testing\\Package\\Data\\Models\\TestObject"
}
Expand Down

0 comments on commit 2f29617

Please sign in to comment.