Skip to content

Commit

Permalink
Add validation support for pagination resolver mode (#2290)
Browse files Browse the repository at this point in the history
* Update PaginateDirective.php

* Cleanup

* Adding a test

* Update tests/Unit/Pagination/PaginateDirectiveTest.php

Co-authored-by: Benedikt Franke <[email protected]>

* Adding a test

---------

Co-authored-by: Benedikt Franke <[email protected]>
  • Loading branch information
Stevemoretz and spawnia authored Feb 7, 2023
1 parent 786ac58 commit 8756e1e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ You can find and compare releases at the [GitHub release page](https://github.co

### Changed

- Add validation support to `Paginator` with `resolver` mode
- Pass resolver arguments to `FieldBuilderDirective::handleFieldBuilder()` https://github.com/nuwave/lighthouse/pull/2234
- Expected resolver arguments in `ResolveInfo::enhanceBuilder()`
- Pass the path array to `CacheKeyAndTags::key()` https://github.com/nuwave/lighthouse/pull/2176
Expand Down
3 changes: 3 additions & 0 deletions src/Pagination/PaginateDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public function resolveField(FieldValue $fieldValue): FieldValue
{
$fieldValue->setResolver(function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Paginator {
if ($this->directiveHasArgument('resolver')) {
// This is done only for validation
PaginationArgs::extractArgs($args, $this->paginationType(), $this->paginateMaxCount());

$paginator = $this->getResolverFromArgument('resolver')($root, $args, $context, $resolveInfo);

assert(
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Pagination/PaginateDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,38 @@ public function testIsLimitedByMaxCountFromDirective(): void
);
}

public function testIsLimitedByMaxCountFromDirectiveWithResolver(): void
{
config(['lighthouse.pagination.max_count' => 5]);

$this->schema = /** @lang GraphQL */ '
type User {
id: ID!
name: String!
}
type Query {
users: [User!]! @paginate(maxCount: 6, resolver: "' . $this->qualifyTestResolver('returnPaginatedDataInsteadOfBuilder') . '")
}
';

$result = $this->graphQL(/** @lang GraphQL */ '
{
users(first: 10) {
data {
id
name
}
}
}
');

$this->assertSame(
PaginationArgs::requestedTooManyItems(6, 10),
$result->json('errors.0.message')
);
}

public function testIsLimitedToMaxCountFromConfig(): void
{
config(['lighthouse.pagination.max_count' => 5]);
Expand Down

0 comments on commit 8756e1e

Please sign in to comment.