Skip to content

Commit

Permalink
Merge pull request #1132 from jasonvarga/get-pagination-fields-typehint
Browse files Browse the repository at this point in the history
Relax PaginationType getPaginationFields typehint
  • Loading branch information
mfn committed Mar 6, 2024
2 parents ed5b800 + a901c3a commit cea7c78
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ CHANGELOG

[Next release](https://github.com/rebing/graphql-laravel/compare/9.4.0...master)

### Fixed
- Relax PaginationType/SimplePaginationType getPaginationFields typehint [\#1132 / jasonvarga](https://github.com/rebing/graphql-laravel/pull/1132)

2024-03-04, 9.4.0
-----------------

Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ parameters:
path: src/Support/PaginationType.php

-
message: "#^Parameter \\#1 \\$underlyingType of method Rebing\\\\GraphQL\\\\Support\\\\PaginationType\\:\\:getPaginationFields\\(\\) expects GraphQL\\\\Type\\\\Definition\\\\ObjectType, GraphQL\\\\Type\\\\Definition\\\\Type given\\.$#"
message: "#^Parameter \\#1 \\$type of static method GraphQL\\\\Type\\\\Definition\\\\Type\\:\\:nonNull\\(\\) expects \\(callable\\(\\)\\: \\(GraphQL\\\\Type\\\\Definition\\\\NullableType&GraphQL\\\\Type\\\\Definition\\\\Type\\)\\)\\|\\(GraphQL\\\\Type\\\\Definition\\\\NullableType&GraphQL\\\\Type\\\\Definition\\\\Type\\), GraphQL\\\\Type\\\\Definition\\\\Type given\\.$#"
count: 1
path: src/Support/PaginationType.php

Expand Down Expand Up @@ -366,7 +366,7 @@ parameters:
path: src/Support/SelectFields.php

-
message: "#^Parameter \\#1 \\$underlyingType of method Rebing\\\\GraphQL\\\\Support\\\\SimplePaginationType\\:\\:getPaginationFields\\(\\) expects GraphQL\\\\Type\\\\Definition\\\\ObjectType, GraphQL\\\\Type\\\\Definition\\\\Type given\\.$#"
message: "#^Parameter \\#1 \\$type of static method GraphQL\\\\Type\\\\Definition\\\\Type\\:\\:nonNull\\(\\) expects \\(callable\\(\\)\\: \\(GraphQL\\\\Type\\\\Definition\\\\NullableType&GraphQL\\\\Type\\\\Definition\\\\Type\\)\\)\\|\\(GraphQL\\\\Type\\\\Definition\\\\NullableType&GraphQL\\\\Type\\\\Definition\\\\Type\\), GraphQL\\\\Type\\\\Definition\\\\Type given\\.$#"
count: 1
path: src/Support/SimplePaginationType.php

Expand Down
2 changes: 1 addition & 1 deletion src/Support/PaginationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $typeName, string $customName = null)
parent::__construct($config);
}

protected function getPaginationFields(ObjectType $underlyingType): array
protected function getPaginationFields(GraphQLType $underlyingType): array
{
return [
'data' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Support/SimplePaginationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $typeName, string $customName = null)
/**
* @return array<string, array<string,mixed>>
*/
protected function getPaginationFields(ObjectType $underlyingType): array
protected function getPaginationFields(GraphQLType $underlyingType): array
{
return [
'data' => [
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/SelectFields/PrimaryKeyTests/ModelInterfaceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types = 1);
namespace Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests;

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\InterfaceType;
use Rebing\GraphQL\Tests\Support\Models\Post;

class ModelInterfaceType extends InterfaceType
{
protected $attributes = [
'name' => 'ModelInterface',
];

public function fields(): array
{
return [
'id' => [
'type' => Type::nonNull(Type::ID()),
],
];
}

public function resolveType(Post $root): Type
{
return GraphQL::type('Post');
}
}
55 changes: 55 additions & 0 deletions tests/Database/SelectFields/PrimaryKeyTests/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ protected function getEnvironmentSetUp($app): void
'query' => [
PrimaryKeyQuery::class,
PrimaryKeyPaginationQuery::class,
PrimaryKeyInterfacePaginationQuery::class,
],
]);

$app['config']->set('graphql.types', [
ModelInterfaceType::class,
CommentType::class,
PostType::class,
]);
Expand Down Expand Up @@ -105,4 +107,57 @@ public function testPagination(): void
];
self::assertEquals($expectedResult, $result);
}

public function testInterfacePagination(): void
{
Post::factory(2)->create();

$query = <<<'GRAQPHQL'
{
primaryKeyInterfacePaginationQuery {
current_page
data {
id
}
from
has_more_pages
last_page
per_page
to
total
}
}
GRAQPHQL;

$this->sqlCounterReset();

$result = $this->httpGraphql($query);

$this->assertSqlQueries(
<<<'SQL'
select count(*) as aggregate from "posts";
select * from "posts" limit 1 offset 0;
SQL
);

$expectedResult = [
'data' => [
'primaryKeyInterfacePaginationQuery' => [
'current_page' => 1,
'data' => [
[
'id' => '1',
],
],
'from' => 1,
'has_more_pages' => true,
'last_page' => 2,
'per_page' => 1,
'to' => 1,
'total' => 2,
],
],
];
self::assertEquals($expectedResult, $result);
}
}
7 changes: 7 additions & 0 deletions tests/Database/SelectFields/PrimaryKeyTests/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ public function fields(): array
],
];
}

public function interfaces(): array
{
return [
GraphQL::type('ModelInterface'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types = 1);
namespace Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests;

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;

class PrimaryKeyInterfacePaginationQuery extends PrimaryKeyPaginationQuery
{
protected $attributes = [
'name' => 'primaryKeyInterfacePaginationQuery',
];

public function type(): Type
{
return GraphQL::paginate('ModelInterface');
}
}

0 comments on commit cea7c78

Please sign in to comment.