-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare non-working test for reference
- Loading branch information
Showing
6 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
tests/Database/SelectFields/UnionTests/SearchUnionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
tests/Database/SelectFields/UnionTests/SearchUnionType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |