-
-
Notifications
You must be signed in to change notification settings - Fork 266
support simple pagination #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f188343
support simple pagination
lamtb6693 a882b56
Update code review
lamtranb 3069205
Update code review
lamtranb 6ed1978
Update code review
lamtranb 6cf9b4a
Update code review src/Support/SimplePaginationType.php
lamtranb 54918f6
Update code review src/Support/SimplePaginationType.php
lamtranb 34c7393
fix alias + update test return empty pagination list
lamtranb 3c57c99
Merge branch 'master' into lamtranb_master
mfn 90dc61d
composer phpstan-baseline
mfn 170901d
set Type::nonNull
lamtb6693 feda7db
Revert "set Type::nonNull"
lamtb6693 537b443
chore: update changelog
mfn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Support; | ||
|
||
use GraphQL\Type\Definition\ObjectType; | ||
use GraphQL\Type\Definition\Type as GraphQLType; | ||
use Illuminate\Pagination\Paginator; | ||
use Illuminate\Support\Collection; | ||
use Rebing\GraphQL\Support\Facades\GraphQL; | ||
|
||
class SimplePaginationType extends ObjectType | ||
{ | ||
public function __construct(string $typeName, string $customName = null) | ||
{ | ||
$name = $customName ?: $typeName.'SimplePagination'; | ||
|
||
$config = [ | ||
'name' => $name, | ||
'fields' => $this->getPaginationFields($typeName), | ||
]; | ||
|
||
$underlyingType = GraphQL::type($typeName); | ||
if (isset($underlyingType->config['model'])) { | ||
$config['model'] = $underlyingType->config['model']; | ||
} | ||
|
||
parent::__construct($config); | ||
} | ||
|
||
/** | ||
* @param string $typeName | ||
* | ||
* @return array<string, array<string,mixed>> | ||
*/ | ||
protected function getPaginationFields(string $typeName): array | ||
{ | ||
return [ | ||
'data' => [ | ||
'type' => GraphQLType::listOf(GraphQL::type($typeName)), | ||
'description' => 'List of items on the current page', | ||
'resolve' => function (Paginator $data): Collection { | ||
return $data->getCollection(); | ||
}, | ||
], | ||
'per_page' => [ | ||
'type' => GraphQLType::nonNull(GraphQLType::int()), | ||
'description' => 'Number of items returned per page', | ||
'resolve' => function (Paginator $data): int { | ||
return $data->perPage(); | ||
}, | ||
'selectable' => false, | ||
], | ||
'current_page' => [ | ||
'type' => GraphQLType::nonNull(GraphQLType::int()), | ||
'description' => 'Current page of the cursor', | ||
'resolve' => function (Paginator $data): int { | ||
return $data->currentPage(); | ||
}, | ||
'selectable' => false, | ||
], | ||
'from' => [ | ||
'type' => GraphQLType::int(), | ||
'description' => 'Number of the first item returned', | ||
'resolve' => function (Paginator $data): ?int { | ||
return $data->firstItem(); | ||
}, | ||
'selectable' => false, | ||
], | ||
'to' => [ | ||
'type' => GraphQLType::int(), | ||
'description' => 'Number of the last item returned', | ||
'resolve' => function (Paginator $data): ?int { | ||
return $data->lastItem(); | ||
}, | ||
'selectable' => false, | ||
], | ||
'has_more_pages' => [ | ||
'type' => GraphQLType::nonNull(GraphQLType::boolean()), | ||
'description' => 'Determines if cursor has more pages after the current page', | ||
'resolve' => function (Paginator $data): bool { | ||
return $data->hasMorePages(); | ||
}, | ||
'selectable' => false, | ||
], | ||
]; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/Database/SelectFields/PrimaryKeyTests/PrimaryKeySimplePaginationQuery.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests; | ||
|
||
use Closure; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use GraphQL\Type\Definition\Type; | ||
use Illuminate\Contracts\Pagination\Paginator; | ||
use Rebing\GraphQL\Support\Facades\GraphQL; | ||
use Rebing\GraphQL\Support\Query; | ||
use Rebing\GraphQL\Support\SelectFields; | ||
use Rebing\GraphQL\Tests\Support\Models\Post; | ||
|
||
class PrimaryKeySimplePaginationQuery extends Query | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
protected $attributes = [ | ||
'name' => 'primaryKeySimplePaginationQuery', | ||
]; | ||
|
||
/** | ||
* @return Type | ||
*/ | ||
public function type(): Type | ||
{ | ||
return GraphQL::simplePaginate('Post'); | ||
} | ||
|
||
/** | ||
* @param mixed $root | ||
* @param mixed $args | ||
* @param mixed $ctx | ||
* @param ResolveInfo $info | ||
* @param Closure $getSelectFields | ||
* | ||
* @return Paginator | ||
*/ | ||
public function resolve($root, $args, $ctx, ResolveInfo $info, Closure $getSelectFields) | ||
{ | ||
/** @var SelectFields $selectFields */ | ||
$selectFields = $getSelectFields(); | ||
|
||
return Post | ||
::with($selectFields->getRelations()) | ||
->select($selectFields->getSelect()) | ||
->simplePaginate(1); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this field can ever return
null
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type::nonNull seems not work with ListOf. ListOf can return an empty list. I will add a new test case for this. Or you mean this?
GraphQLType::listOf(Type::nonNull(GraphQL::type($typeName)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question!
Oh, but it should!
In this case I meant that the value returned by
resolve
cannot benull
, ever, because even the closure has the typehintCollection
.This means there will always be a collection returned, never null, so wrapping the existing
GraphQLType::listOf(GraphQL::type($typeName)),
into non-null likeGraphQLType::nonNull(GraphQLType::listOf(GraphQL::type($typeName))),
should be possible!(sorry I see in my feedback I just write
Type::nonNull
which is usually the alias used, but in this class it'sGraphQLType
, so maybe that's why it may look confusing).Exactly! But "empty list" !== "null"!
I would love to (always good to avoid null values, if possible), but I think a collection containing null values is technically allowed (however unlikely), so therefore sadly no, didn't meant this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mfn
Thanks for taking your time to review my code.
I updated the code into this:
GraphQLType::nonNull(GraphQLType::listOf(GraphQL::type($typeName)))
The tests failed.
I checked in the file tests/Database/SelectFields/PrimaryKeyTests/PrimaryKeySimplePaginationQuery.php
/** @var SelectFields $selectFields */ $selectFields = $getSelectFields(); $selectFields->getSelect()
only return
posts.id
. If I remove GraphQLType::nonNull, it will returnposts.id, posts.title
. I think there is a bug in src/Support/SelectFields.php or miss check somewhere. Can you help me? I'm not familiar with this. Thanks very much.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I've no idea.
I don't consider it a showstopper and I also didn't write the existing
PaginationType
which works exactly the same way, so I actually do feel bad for side-tracking this 😞