From 2087c1a4d310babd06b98d5e6ce4ad042005e470 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Wed, 18 Oct 2023 10:00:41 +0400 Subject: [PATCH] `StreamNavigator` => `StreamNavigation`. --- packages/graphql/docs/Directives/@stream.md | 2 +- .../src/Stream/Directives/DirectiveTest.php | 34 +++++++++---------- .../Directives/DirectiveTest~expected.graphql | 16 +++++++-- packages/graphql/src/Stream/StreamValue.php | 28 +++++++-------- .../Types/{Navigator.php => Navigation.php} | 4 +-- packages/graphql/src/Stream/Types/Stream.php | 18 ++++++++-- 6 files changed, 63 insertions(+), 39 deletions(-) rename packages/graphql/src/Stream/Types/{Navigator.php => Navigation.php} (92%) diff --git a/packages/graphql/docs/Directives/@stream.md b/packages/graphql/docs/Directives/@stream.md index f4a51e2f8..304465bea 100644 --- a/packages/graphql/docs/Directives/@stream.md +++ b/packages/graphql/docs/Directives/@stream.md @@ -117,7 +117,7 @@ query example( value } length - navigator { + navigation { previous current next diff --git a/packages/graphql/src/Stream/Directives/DirectiveTest.php b/packages/graphql/src/Stream/Directives/DirectiveTest.php index b9b3539c5..673093dc7 100644 --- a/packages/graphql/src/Stream/Directives/DirectiveTest.php +++ b/packages/graphql/src/Stream/Directives/DirectiveTest.php @@ -159,7 +159,7 @@ public function decryptString(mixed $payload): string { value } length - navigator { + navigation { previous current next @@ -256,7 +256,7 @@ public function decryptString(mixed $payload): string { value } length - navigator { + navigation { previous current next @@ -1274,7 +1274,7 @@ static function (): void { ], 'first page' => [ [ - 'items' => [ + 'items' => [ [ 'id' => '2dd0bb15-6df9-4490-8b95-4af55f6e0c7a', 'value' => 'b', @@ -1284,8 +1284,8 @@ static function (): void { 'value' => 'c', ], ], - 'length' => 3, - 'navigator' => [ + 'length' => 3, + 'navigation' => [ 'previous' => null, 'current' => '{"path":"test","cursor":null,"offset":0}', 'next' => '{"path":"test","cursor":null,"offset":2}', @@ -1312,14 +1312,14 @@ static function (): void { ], 'second page: cursor' => [ [ - 'items' => [ + 'items' => [ [ 'id' => '99187829-9c6c-4f4f-a206-54dc8a552165', 'value' => 'a', ], ], - 'length' => 3, - 'navigator' => [ + 'length' => 3, + 'navigation' => [ 'previous' => '{"path":"test","cursor":null,"offset":0}', 'current' => '{"path":"test","cursor":null,"offset":2}', 'next' => null, @@ -1346,14 +1346,14 @@ static function (): void { ], 'second page: offset' => [ [ - 'items' => [ + 'items' => [ [ 'id' => '99187829-9c6c-4f4f-a206-54dc8a552165', 'value' => 'a', ], ], - 'length' => 3, - 'navigator' => [ + 'length' => 3, + 'navigation' => [ 'previous' => '{"path":"test","cursor":null,"offset":0}', 'current' => '{"path":"test","cursor":null,"offset":2}', 'next' => null, @@ -1380,14 +1380,14 @@ static function (): void { ], 'search' => [ [ - 'items' => [ + 'items' => [ [ 'id' => '6aea881f-2b50-4295-ac4f-afed3430e6cd', 'value' => 'b', ], ], - 'length' => 1, - 'navigator' => [ + 'length' => 1, + 'navigation' => [ 'previous' => null, 'current' => '{"path":"test","cursor":null,"offset":0}', 'next' => null, @@ -1412,7 +1412,7 @@ static function (): void { ], 'sort' => [ [ - 'items' => [ + 'items' => [ [ 'id' => '8f1a92ce-2da3-4119-8a87-1395d86fe4eb', 'value' => 'a', @@ -1426,8 +1426,8 @@ static function (): void { 'value' => 'b', ], ], - 'length' => 3, - 'navigator' => [ + 'length' => 3, + 'navigation' => [ 'previous' => null, 'current' => '{"path":"test","cursor":null,"offset":0}', 'next' => null, diff --git a/packages/graphql/src/Stream/Directives/DirectiveTest~expected.graphql b/packages/graphql/src/Stream/Directives/DirectiveTest~expected.graphql index a82edca0a..a15454da7 100644 --- a/packages/graphql/src/Stream/Directives/DirectiveTest~expected.graphql +++ b/packages/graphql/src/Stream/Directives/DirectiveTest~expected.graphql @@ -310,9 +310,21 @@ type Car { } type CarsStream { + """ + Requested items. + """ items: [Car!]! + + """ + Total number of items. Not recommended querying it in each query + due to performance. + """ length: Int - navigator: StreamNavigator! + + """ + Cursors to navigate within the stream. + """ + navigation: StreamNavigation! } type Query @@ -563,7 +575,7 @@ implements @validate } -type StreamNavigator { +type StreamNavigation { current: StreamCursor! next: StreamCursor previous: StreamCursor diff --git a/packages/graphql/src/Stream/StreamValue.php b/packages/graphql/src/Stream/StreamValue.php index ce2d2c8df..beb76618e 100644 --- a/packages/graphql/src/Stream/StreamValue.php +++ b/packages/graphql/src/Stream/StreamValue.php @@ -13,25 +13,25 @@ public function __construct( public function __isset(string $name): bool { return match ($name) { - 'items' => true, - 'length' => true, - 'navigator' => true, - 'previous' => true, - 'current' => true, - 'next' => true, - default => false, + 'items' => true, + 'length' => true, + 'navigation' => true, + 'previous' => true, + 'current' => true, + 'next' => true, + default => false, }; } public function __get(string $name): mixed { return match ($name) { - 'items' => $this->stream->getItems(), - 'length' => $this->stream->getLength(), - 'navigator' => $this, - 'previous' => $this->stream->getPreviousCursor(), - 'current' => $this->stream->getCurrentCursor(), - 'next' => $this->stream->getNextCursor(), - default => null, + 'items' => $this->stream->getItems(), + 'length' => $this->stream->getLength(), + 'navigation' => $this, + 'previous' => $this->stream->getPreviousCursor(), + 'current' => $this->stream->getCurrentCursor(), + 'next' => $this->stream->getNextCursor(), + default => null, }; } } diff --git a/packages/graphql/src/Stream/Types/Navigator.php b/packages/graphql/src/Stream/Types/Navigation.php similarity index 92% rename from packages/graphql/src/Stream/Types/Navigator.php rename to packages/graphql/src/Stream/Types/Navigation.php index 9cf2267f5..4e99ec6f8 100644 --- a/packages/graphql/src/Stream/Types/Navigator.php +++ b/packages/graphql/src/Stream/Types/Navigation.php @@ -11,13 +11,13 @@ use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator; use LastDragon_ru\LaraASP\GraphQL\Stream\Directives\Directive; -class Navigator implements TypeDefinition { +class Navigation implements TypeDefinition { public function __construct() { // empty } public function getTypeName(Manipulator $manipulator, BuilderInfo $builder, TypeSource $source): string { - return Directive::Name.'Navigator'; + return Directive::Name.'Navigation'; } public function getTypeDefinition( diff --git a/packages/graphql/src/Stream/Types/Stream.php b/packages/graphql/src/Stream/Types/Stream.php index 32d140e75..ee390db20 100644 --- a/packages/graphql/src/Stream/Types/Stream.php +++ b/packages/graphql/src/Stream/Types/Stream.php @@ -38,15 +38,27 @@ public function getTypeDefinition( string $name, TypeSource $source, ): TypeDefinitionNode|Type|null { - $type = $source->getTypeName(); - $navigator = $manipulator->getType(Navigator::class, $source); + $type = $source->getTypeName(); + $navigation = $manipulator->getType(Navigation::class, $source); return Parser::objectTypeDefinition( <<