-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include morph type key in returned model
- Loading branch information
Christian Nielsen
committed
Nov 4, 2019
1 parent
c747b38
commit 3cf3c8d
Showing
9 changed files
with
393 additions
and
13 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
46 changes: 46 additions & 0 deletions
46
tests/Database/SelectFields/MorphRelationshipTests/CommentType.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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\MorphRelationshipTests; | ||
|
||
use GraphQL\Type\Definition\Type; | ||
use Rebing\GraphQL\Support\Facades\GraphQL; | ||
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()), | ||
], | ||
'likes' => [ | ||
'type' => Type::listOf(GraphQL::Type('Like')), | ||
], | ||
'post' => [ | ||
'type' => Type::nonNull(GraphQL::type('Post')), | ||
], | ||
'title' => [ | ||
'type' => Type::nonNull(Type::string()), | ||
], | ||
]; | ||
} | ||
|
||
public function interfaces(): array | ||
{ | ||
return [ | ||
GraphQL::type('LikableInterface'), | ||
]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Database/SelectFields/MorphRelationshipTests/LikableInterfaceType.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\MorphRelationshipTests; | ||
|
||
use GraphQL\Type\Definition\Type; | ||
use Rebing\GraphQL\Support\Facades\GraphQL; | ||
use Rebing\GraphQL\Support\InterfaceType; | ||
use Rebing\GraphQL\Tests\Support\Models\Comment; | ||
use Rebing\GraphQL\Tests\Support\Models\Post; | ||
|
||
class LikableInterfaceType extends InterfaceType | ||
{ | ||
protected $attributes = [ | ||
'name' => 'LikableInterface', | ||
]; | ||
|
||
public function fields(): array | ||
{ | ||
return [ | ||
'id' => [ | ||
'type' => Type::nonNull(Type::id()), | ||
], | ||
]; | ||
} | ||
|
||
public function resolveType($root) | ||
{ | ||
if ($root instanceof Post) { | ||
return GraphQL::type('Post'); | ||
} elseif ($root instanceof Comment) { | ||
return GraphQL::type('Comment'); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Database/SelectFields/MorphRelationshipTests/LikeType.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\MorphRelationshipTests; | ||
|
||
use GraphQL\Type\Definition\Type; | ||
use Rebing\GraphQL\Support\Facades\GraphQL; | ||
use Rebing\GraphQL\Support\Type as GraphQLType; | ||
use Rebing\GraphQL\Tests\Support\Models\Like; | ||
|
||
class LikeType extends GraphQLType | ||
{ | ||
protected $attributes = [ | ||
'name' => 'Like', | ||
'model' => Like::class, | ||
]; | ||
|
||
public function fields(): array | ||
{ | ||
return [ | ||
'id' => [ | ||
'type' => Type::nonNull(Type::id()), | ||
], | ||
'likable' => [ | ||
'type' => Type::nonNull(GraphQL::type('LikableInterface')), | ||
], | ||
'user' => [ | ||
'type' => Type::nonNull(GraphQL::type('User')), | ||
], | ||
]; | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
tests/Database/SelectFields/MorphRelationshipTests/MorphRelationshipTest.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,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\MorphRelationshipTests; | ||
|
||
use Rebing\GraphQL\Tests\Support\Models\Like; | ||
use Rebing\GraphQL\Tests\Support\Models\Post; | ||
use Rebing\GraphQL\Tests\Support\Models\User; | ||
use Rebing\GraphQL\Tests\Support\Traits\SqlAssertionTrait; | ||
use Rebing\GraphQL\Tests\TestCaseDatabase; | ||
|
||
class MorphRelationshipTest extends TestCaseDatabase | ||
{ | ||
use SqlAssertionTrait; | ||
|
||
public function testMorphRelationship(): void | ||
{ | ||
$user = factory(User::class)->create([ | ||
'name' => 'User Name', | ||
]); | ||
|
||
$otherUser = factory(User::class)->create(); | ||
|
||
$post = factory(Post::class)->create([ | ||
'user_id' => $user->id, | ||
]); | ||
|
||
$like = factory(Like::class)->make([ | ||
'user_id' => $otherUser->id, | ||
]); | ||
|
||
$post->likes()->save($like); | ||
|
||
$this->assertNotNull($user->posts[0]->likes[0]->user); | ||
|
||
$query = <<<'GRAQPHQL' | ||
{ | ||
users { | ||
id | ||
posts { | ||
id | ||
title | ||
likes { | ||
id | ||
user { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
GRAQPHQL; | ||
|
||
$this->sqlCounterReset(); | ||
|
||
$result = $this->graphql($query, [ | ||
'expectErrors' => true, | ||
]); | ||
|
||
$this->assertSqlQueries( | ||
<<<'SQL' | ||
select "users"."id" from "users"; | ||
select "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc; | ||
select "likes"."id", "likes"."user_id", "likes"."likable_id", "likes"."likable_type" from "likes" where "likes"."likable_id" in (?) and "likes"."likable_type" = ?; | ||
select "users"."id" from "users" where "users"."id" in (?); | ||
select * from "posts" where "posts"."id" = ? limit 1; | ||
SQL | ||
); | ||
|
||
$expectedResult = [ | ||
'data' => [ | ||
'users' => [ | ||
[ | ||
'id' => $user->id, | ||
'posts' => [ | ||
[ | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'likes' => [ | ||
[ | ||
'id' => $like->id, | ||
'user' => [ | ||
'id' => $otherUser->id, | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
[ | ||
'id' => $otherUser->id, | ||
'posts' => [], | ||
], | ||
], | ||
], | ||
]; | ||
$this->assertEquals($expectedResult, $result); | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
parent::getEnvironmentSetUp($app); | ||
|
||
$app['config']->set('graphql.schemas.default', [ | ||
'query' => [ | ||
UsersQuery::class, | ||
], | ||
]); | ||
|
||
$app['config']->set('graphql.schemas.custom', null); | ||
|
||
$app['config']->set('graphql.types', [ | ||
LikableInterfaceType::class, | ||
CommentType::class, | ||
LikeType::class, | ||
PostType::class, | ||
UserType::class, | ||
]); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/Database/SelectFields/MorphRelationshipTests/PostType.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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\MorphRelationshipTests; | ||
|
||
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::nonNull(Type::string()), | ||
], | ||
'comments' => [ | ||
'type' => Type::listOf(GraphQL::type('Comment')), | ||
], | ||
'id' => [ | ||
'type' => Type::nonNull(Type::id()), | ||
], | ||
'likes' => [ | ||
'type' => Type::listOf(GraphQL::Type('Like')), | ||
], | ||
'title' => [ | ||
'type' => Type::nonNull(Type::string()), | ||
], | ||
'user' => [ | ||
'type' => GraphQL::type('User'), | ||
], | ||
]; | ||
} | ||
|
||
public function interfaces(): array | ||
{ | ||
return [ | ||
GraphQL::type('LikableInterface'), | ||
]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Database/SelectFields/MorphRelationshipTests/UserType.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Tests\Database\SelectFields\MorphRelationshipTests; | ||
|
||
use GraphQL\Type\Definition\Type; | ||
use Rebing\GraphQL\Support\Facades\GraphQL; | ||
use Rebing\GraphQL\Support\Type as GraphQLType; | ||
use Rebing\GraphQL\Tests\Support\Models\User; | ||
|
||
class UserType extends GraphQLType | ||
{ | ||
protected $attributes = [ | ||
'name' => 'User', | ||
'model' => User::class, | ||
]; | ||
|
||
public function fields(): array | ||
{ | ||
return [ | ||
'id' => [ | ||
'type' => Type::nonNull(Type::id()), | ||
], | ||
'likes' => [ | ||
'type' => Type::listOf(GraphQL::Type('Like')), | ||
], | ||
'name' => [ | ||
'type' => Type::nonNull(Type::string()), | ||
], | ||
'posts' => [ | ||
'type' => Type::listOf(GraphQL::type('Post')), | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.