Skip to content

Commit cea7c78

Browse files
authored
Merge pull request #1132 from jasonvarga/get-pagination-fields-typehint
Relax PaginationType getPaginationFields typehint
2 parents ed5b800 + a901c3a commit cea7c78

File tree

8 files changed

+118
-4
lines changed

8 files changed

+118
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ CHANGELOG
33

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

6+
### Fixed
7+
- Relax PaginationType/SimplePaginationType getPaginationFields typehint [\#1132 / jasonvarga](https://github.com/rebing/graphql-laravel/pull/1132)
8+
69
2024-03-04, 9.4.0
710
-----------------
811

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ parameters:
226226
path: src/Support/PaginationType.php
227227

228228
-
229-
message: "#^Parameter \\#1 \\$underlyingType of method Rebing\\\\GraphQL\\\\Support\\\\PaginationType\\:\\:getPaginationFields\\(\\) expects GraphQL\\\\Type\\\\Definition\\\\ObjectType, GraphQL\\\\Type\\\\Definition\\\\Type given\\.$#"
229+
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\\.$#"
230230
count: 1
231231
path: src/Support/PaginationType.php
232232

@@ -366,7 +366,7 @@ parameters:
366366
path: src/Support/SelectFields.php
367367

368368
-
369-
message: "#^Parameter \\#1 \\$underlyingType of method Rebing\\\\GraphQL\\\\Support\\\\SimplePaginationType\\:\\:getPaginationFields\\(\\) expects GraphQL\\\\Type\\\\Definition\\\\ObjectType, GraphQL\\\\Type\\\\Definition\\\\Type given\\.$#"
369+
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\\.$#"
370370
count: 1
371371
path: src/Support/SimplePaginationType.php
372372

src/Support/PaginationType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(string $typeName, string $customName = null)
2929
parent::__construct($config);
3030
}
3131

32-
protected function getPaginationFields(ObjectType $underlyingType): array
32+
protected function getPaginationFields(GraphQLType $underlyingType): array
3333
{
3434
return [
3535
'data' => [

src/Support/SimplePaginationType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(string $typeName, string $customName = null)
3232
/**
3333
* @return array<string, array<string,mixed>>
3434
*/
35-
protected function getPaginationFields(ObjectType $underlyingType): array
35+
protected function getPaginationFields(GraphQLType $underlyingType): array
3636
{
3737
return [
3838
'data' => [
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Facades\GraphQL;
8+
use Rebing\GraphQL\Support\InterfaceType;
9+
use Rebing\GraphQL\Tests\Support\Models\Post;
10+
11+
class ModelInterfaceType extends InterfaceType
12+
{
13+
protected $attributes = [
14+
'name' => 'ModelInterface',
15+
];
16+
17+
public function fields(): array
18+
{
19+
return [
20+
'id' => [
21+
'type' => Type::nonNull(Type::ID()),
22+
],
23+
];
24+
}
25+
26+
public function resolveType(Post $root): Type
27+
{
28+
return GraphQL::type('Post');
29+
}
30+
}

tests/Database/SelectFields/PrimaryKeyTests/PaginationTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ protected function getEnvironmentSetUp($app): void
2020
'query' => [
2121
PrimaryKeyQuery::class,
2222
PrimaryKeyPaginationQuery::class,
23+
PrimaryKeyInterfacePaginationQuery::class,
2324
],
2425
]);
2526

2627
$app['config']->set('graphql.types', [
28+
ModelInterfaceType::class,
2729
CommentType::class,
2830
PostType::class,
2931
]);
@@ -105,4 +107,57 @@ public function testPagination(): void
105107
];
106108
self::assertEquals($expectedResult, $result);
107109
}
110+
111+
public function testInterfacePagination(): void
112+
{
113+
Post::factory(2)->create();
114+
115+
$query = <<<'GRAQPHQL'
116+
{
117+
primaryKeyInterfacePaginationQuery {
118+
current_page
119+
data {
120+
id
121+
}
122+
from
123+
has_more_pages
124+
last_page
125+
per_page
126+
to
127+
total
128+
}
129+
}
130+
GRAQPHQL;
131+
132+
$this->sqlCounterReset();
133+
134+
$result = $this->httpGraphql($query);
135+
136+
$this->assertSqlQueries(
137+
<<<'SQL'
138+
select count(*) as aggregate from "posts";
139+
select * from "posts" limit 1 offset 0;
140+
SQL
141+
);
142+
143+
$expectedResult = [
144+
'data' => [
145+
'primaryKeyInterfacePaginationQuery' => [
146+
'current_page' => 1,
147+
'data' => [
148+
[
149+
'id' => '1',
150+
],
151+
],
152+
'from' => 1,
153+
'has_more_pages' => true,
154+
'last_page' => 2,
155+
'per_page' => 1,
156+
'to' => 1,
157+
'total' => 2,
158+
],
159+
],
160+
];
161+
self::assertEquals($expectedResult, $result);
162+
}
108163
}

tests/Database/SelectFields/PrimaryKeyTests/PostType.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public function fields(): array
3232
],
3333
];
3434
}
35+
36+
public function interfaces(): array
37+
{
38+
return [
39+
GraphQL::type('ModelInterface'),
40+
];
41+
}
3542
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Facades\GraphQL;
8+
9+
class PrimaryKeyInterfacePaginationQuery extends PrimaryKeyPaginationQuery
10+
{
11+
protected $attributes = [
12+
'name' => 'primaryKeyInterfacePaginationQuery',
13+
];
14+
15+
public function type(): Type
16+
{
17+
return GraphQL::paginate('ModelInterface');
18+
}
19+
}

0 commit comments

Comments
 (0)