Skip to content

Commit

Permalink
prepare non-working test for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarsn committed Jan 13, 2023
1 parent 2db6692 commit 42edc4b
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 0 deletions.
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,26 @@ parameters:
count: 1
path: tests/Database/SelectFields/QueryArgsAndContextTests/UsersQuery.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has no return type specified\\.$#"
count: 1
path: tests/Database/SelectFields/UnionTests/SearchQuery.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has parameter \\$args with no type specified\\.$#"
count: 1
path: tests/Database/SelectFields/UnionTests/SearchQuery.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has parameter \\$ctx with no type specified\\.$#"
count: 1
path: tests/Database/SelectFields/UnionTests/SearchQuery.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\UnionTests\\\\SearchQuery\\:\\:resolve\\(\\) has parameter \\$root with no type specified\\.$#"
count: 1
path: tests/Database/SelectFields/UnionTests/SearchQuery.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Tests\\\\Database\\\\SelectFields\\\\ValidateDiffNodeTests\\\\UsersQuery\\:\\:resolve\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/SelectFields/UnionTests/CommentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;
use Rebing\GraphQL\Tests\Support\Models\Comment;

class CommentType extends GraphQLType
{
protected $attributes = [
'name' => 'Comment',
'model' => Comment::class,
];

public function fields(): array
{
return [
'body' => [
'type' => Type::string(),
],
'id' => [
'type' => Type::nonNull(Type::ID()),
],
'title' => [
'type' => Type::nonNull(Type::string()),
],
];
}
}
38 changes: 38 additions & 0 deletions tests/Database/SelectFields/UnionTests/PostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

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

class PostType extends GraphQLType
{
protected $attributes = [
'name' => 'Post',
'model' => Post::class,
];

public function fields(): array
{
return [
'body' => [
'type' => Type::string(),
],
'comments' => [
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Comment')))),
'query' => function (array $args, $query, $ctx) {
$query->where('title', 'like', 'lorem');
},
],
'id' => [
'type' => Type::nonNull(Type::ID()),
],
'title' => [
'type' => Type::nonNull(Type::string()),
],
];
}
}
53 changes: 53 additions & 0 deletions tests/Database/SelectFields/UnionTests/SearchQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

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

use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Str;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;
use Rebing\GraphQL\Support\SelectFields;
use Rebing\GraphQL\Tests\Support\Models\Comment;
use Rebing\GraphQL\Tests\Support\Models\Post;

class SearchQuery extends Query
{
protected $attributes = [
'name' => 'searchQuery',
];

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

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

public function resolve($root, $args, $ctx, ResolveInfo $info, Closure $getSelectFields)
{
/** @var SelectFields $selectFields */
$selectFields = $getSelectFields();

if (Str::startsWith($args['id'], 'comment:')) {
return Comment::select($selectFields->getSelect())
->with($selectFields->getRelations())
->where('id', (int)Str::after($args['id'], 'comment:'))
->first();
} else {
return Post::select($selectFields->getSelect())
->with($selectFields->getRelations())
->where('id', (int)$args['id'])
->first();
}
}
}
71 changes: 71 additions & 0 deletions tests/Database/SelectFields/UnionTests/SearchUnionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Database\SelectFields\UnionTests;

use Rebing\GraphQL\Tests\Support\Models\Comment;
use Rebing\GraphQL\Tests\Support\Models\Post;
use Rebing\GraphQL\Tests\TestCaseDatabase;

class SearchUnionTest extends TestCaseDatabase
{
protected function getEnvironmentSetUp($app): void
{
parent::getEnvironmentSetUp($app);

$app['config']->set('graphql.schemas.default', [
'query' => [
SearchQuery::class,
],
]);

$app['config']->set('graphql.types', [
CommentType::class,
PostType::class,
SearchUnionType::class,
]);
}

public function testCustomQueryIsExecutedUsingUnionTypeOnQuery(): void
{
/** @var Post $post */
$post = factory(Post::class)->create();
/** @var Comment $comment1 */
$comment1 = factory(Comment::class)->create(['post_id' => $post->id, 'title' => 'lorem']);
/** @var Comment $comment2 */
$comment2 = factory(Comment::class)->create(['post_id' => $post->id, 'title' => 'ipsum']);

$query = <<<'GRAQPHQL'
query ($id: String!) {
searchQuery(id: $id) {
... on Post {
id
comments {
id
}
}
... on Comment {
id
}
}
}
GRAQPHQL;

$result = $this->httpGraphql($query, [
'variables' => ['id' => (string)$post->id],
]);

$expectedResult = [
'data' => [
'searchQuery' => [
'id' => (string)$post->id,
'comments' => [
['id' => $comment1->id],
],
],
],
];
self::assertEquals($expectedResult, $result);
}
}
40 changes: 40 additions & 0 deletions tests/Database/SelectFields/UnionTests/SearchUnionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

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

class SearchUnionType extends UnionType
{
protected $attributes = [
'name' => 'SearchUnion',
];

public function types(): array
{
return [
GraphQL::type('Post'),
GraphQL::type('Comment'),
];
}

/**
* @param object $value
* @return Type|null
*/
public function resolveType($value): ?Type
{
if ($value instanceof Post) {
return GraphQL::type('Post');
} elseif ($value instanceof Comment) {
return GraphQL::type('Comment');
} else {
return null;
}
}
}

0 comments on commit 42edc4b

Please sign in to comment.