Skip to content

Commit 42edc4b

Browse files
committed
prepare non-working test for reference
1 parent 2db6692 commit 42edc4b

File tree

6 files changed

+253
-0
lines changed

6 files changed

+253
-0
lines changed

phpstan-baseline.neon

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,26 @@ parameters:
640640
count: 1
641641
path: tests/Database/SelectFields/QueryArgsAndContextTests/UsersQuery.php
642642

643+
-
644+
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has no return type specified\\.$#"
645+
count: 1
646+
path: tests/Database/SelectFields/UnionTests/SearchQuery.php
647+
648+
-
649+
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has parameter \\$args with no type specified\\.$#"
650+
count: 1
651+
path: tests/Database/SelectFields/UnionTests/SearchQuery.php
652+
653+
-
654+
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has parameter \\$ctx with no type specified\\.$#"
655+
count: 1
656+
path: tests/Database/SelectFields/UnionTests/SearchQuery.php
657+
658+
-
659+
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has parameter \\$root with no type specified\\.$#"
660+
count: 1
661+
path: tests/Database/SelectFields/UnionTests/SearchQuery.php
662+
643663
-
644664
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\ValidateDiffNodeTests\\\\UsersQuery\\:\\:resolve\\(\\) has no return type specified\\.$#"
645665
count: 1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Database\SelectFields\UnionTests;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Type as GraphQLType;
8+
use Rebing\GraphQL\Tests\Support\Models\Comment;
9+
10+
class CommentType extends GraphQLType
11+
{
12+
protected $attributes = [
13+
'name' => 'Comment',
14+
'model' => Comment::class,
15+
];
16+
17+
public function fields(): array
18+
{
19+
return [
20+
'body' => [
21+
'type' => Type::string(),
22+
],
23+
'id' => [
24+
'type' => Type::nonNull(Type::ID()),
25+
],
26+
'title' => [
27+
'type' => Type::nonNull(Type::string()),
28+
],
29+
];
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Database\SelectFields\UnionTests;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Facades\GraphQL;
8+
use Rebing\GraphQL\Support\Type as GraphQLType;
9+
use Rebing\GraphQL\Tests\Support\Models\Post;
10+
11+
class PostType extends GraphQLType
12+
{
13+
protected $attributes = [
14+
'name' => 'Post',
15+
'model' => Post::class,
16+
];
17+
18+
public function fields(): array
19+
{
20+
return [
21+
'body' => [
22+
'type' => Type::string(),
23+
],
24+
'comments' => [
25+
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Comment')))),
26+
'query' => function (array $args, $query, $ctx) {
27+
$query->where('title', 'like', 'lorem');
28+
},
29+
],
30+
'id' => [
31+
'type' => Type::nonNull(Type::ID()),
32+
],
33+
'title' => [
34+
'type' => Type::nonNull(Type::string()),
35+
],
36+
];
37+
}
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Database\SelectFields\UnionTests;
5+
6+
use Closure;
7+
use GraphQL\Type\Definition\ResolveInfo;
8+
use GraphQL\Type\Definition\Type;
9+
use Illuminate\Support\Str;
10+
use Rebing\GraphQL\Support\Facades\GraphQL;
11+
use Rebing\GraphQL\Support\Query;
12+
use Rebing\GraphQL\Support\SelectFields;
13+
use Rebing\GraphQL\Tests\Support\Models\Comment;
14+
use Rebing\GraphQL\Tests\Support\Models\Post;
15+
16+
class SearchQuery extends Query
17+
{
18+
protected $attributes = [
19+
'name' => 'searchQuery',
20+
];
21+
22+
public function type(): Type
23+
{
24+
return GraphQL::type('SearchUnion');
25+
}
26+
27+
public function args(): array
28+
{
29+
return [
30+
'id' => [
31+
'type' => Type::nonNull(Type::string()),
32+
],
33+
];
34+
}
35+
36+
public function resolve($root, $args, $ctx, ResolveInfo $info, Closure $getSelectFields)
37+
{
38+
/** @var SelectFields $selectFields */
39+
$selectFields = $getSelectFields();
40+
41+
if (Str::startsWith($args['id'], 'comment:')) {
42+
return Comment::select($selectFields->getSelect())
43+
->with($selectFields->getRelations())
44+
->where('id', (int)Str::after($args['id'], 'comment:'))
45+
->first();
46+
} else {
47+
return Post::select($selectFields->getSelect())
48+
->with($selectFields->getRelations())
49+
->where('id', (int)$args['id'])
50+
->first();
51+
}
52+
}
53+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Database\SelectFields\UnionTests;
6+
7+
use Rebing\GraphQL\Tests\Support\Models\Comment;
8+
use Rebing\GraphQL\Tests\Support\Models\Post;
9+
use Rebing\GraphQL\Tests\TestCaseDatabase;
10+
11+
class SearchUnionTest extends TestCaseDatabase
12+
{
13+
protected function getEnvironmentSetUp($app): void
14+
{
15+
parent::getEnvironmentSetUp($app);
16+
17+
$app['config']->set('graphql.schemas.default', [
18+
'query' => [
19+
SearchQuery::class,
20+
],
21+
]);
22+
23+
$app['config']->set('graphql.types', [
24+
CommentType::class,
25+
PostType::class,
26+
SearchUnionType::class,
27+
]);
28+
}
29+
30+
public function testCustomQueryIsExecutedUsingUnionTypeOnQuery(): void
31+
{
32+
/** @var Post $post */
33+
$post = factory(Post::class)->create();
34+
/** @var Comment $comment1 */
35+
$comment1 = factory(Comment::class)->create(['post_id' => $post->id, 'title' => 'lorem']);
36+
/** @var Comment $comment2 */
37+
$comment2 = factory(Comment::class)->create(['post_id' => $post->id, 'title' => 'ipsum']);
38+
39+
$query = <<<'GRAQPHQL'
40+
query ($id: String!) {
41+
searchQuery(id: $id) {
42+
... on Post {
43+
id
44+
comments {
45+
id
46+
}
47+
}
48+
... on Comment {
49+
id
50+
}
51+
}
52+
}
53+
GRAQPHQL;
54+
55+
$result = $this->httpGraphql($query, [
56+
'variables' => ['id' => (string)$post->id],
57+
]);
58+
59+
$expectedResult = [
60+
'data' => [
61+
'searchQuery' => [
62+
'id' => (string)$post->id,
63+
'comments' => [
64+
['id' => $comment1->id],
65+
],
66+
],
67+
],
68+
];
69+
self::assertEquals($expectedResult, $result);
70+
}
71+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Database\SelectFields\UnionTests;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Facades\GraphQL;
8+
use Rebing\GraphQL\Support\UnionType;
9+
use Rebing\GraphQL\Tests\Support\Models\Comment;
10+
use Rebing\GraphQL\Tests\Support\Models\Post;
11+
12+
class SearchUnionType extends UnionType
13+
{
14+
protected $attributes = [
15+
'name' => 'SearchUnion',
16+
];
17+
18+
public function types(): array
19+
{
20+
return [
21+
GraphQL::type('Post'),
22+
GraphQL::type('Comment'),
23+
];
24+
}
25+
26+
/**
27+
* @param object $value
28+
* @return Type|null
29+
*/
30+
public function resolveType($value): ?Type
31+
{
32+
if ($value instanceof Post) {
33+
return GraphQL::type('Post');
34+
} elseif ($value instanceof Comment) {
35+
return GraphQL::type('Comment');
36+
} else {
37+
return null;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)