Skip to content

Commit

Permalink
StreamNavigator => StreamNavigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Oct 18, 2023
1 parent 6867406 commit 654c466
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/graphql/docs/Directives/@stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ query example(
value
}
length
navigator {
navigation {
previous
current
next
Expand Down
34 changes: 17 additions & 17 deletions packages/graphql/src/Stream/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function decryptString(mixed $payload): string {
value
}
length
navigator {
navigation {
previous
current
next
Expand Down Expand Up @@ -256,7 +256,7 @@ public function decryptString(mixed $payload): string {
value
}
length
navigator {
navigation {
previous
current
next
Expand Down Expand Up @@ -1274,7 +1274,7 @@ static function (): void {
],
'first page' => [
[
'items' => [
'items' => [
[
'id' => '2dd0bb15-6df9-4490-8b95-4af55f6e0c7a',
'value' => 'b',
Expand All @@ -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}',
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -1412,7 +1412,7 @@ static function (): void {
],
'sort' => [
[
'items' => [
'items' => [
[
'id' => '8f1a92ce-2da3-4119-8a87-1395d86fe4eb',
'value' => 'a',
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -563,7 +575,7 @@ implements
@validate
}

type StreamNavigator {
type StreamNavigation {
current: StreamCursor!
next: StreamCursor
previous: StreamCursor
Expand Down
28 changes: 14 additions & 14 deletions packages/graphql/src/Stream/StreamValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 15 additions & 3 deletions packages/graphql/src/Stream/Types/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<<<GRAPHQL
type {$name} {
"""
Requested items.
"""
items: [{$type}!]!
"""
Total number of items. Not recommended querying it in each query
due to performance.
"""
length: Int
navigator: {$navigator}!
"""
Cursors to navigate within the stream.
"""
navigation: {$navigation}!
}
GRAPHQL,
);
Expand Down

0 comments on commit 654c466

Please sign in to comment.