Skip to content

Commit cdc9e84

Browse files
committed
tests: show that alias alone doens't work as expected
Future fix prepared in #283
1 parent 23bc8bf commit cdc9e84

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

tests/Database/SelectFieldsTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Rebing\GraphQL\Tests\Support\Queries\PostQuery;
1111
use Rebing\GraphQL\Tests\Support\Types\PostWithModelType;
1212
use Rebing\GraphQL\Tests\Support\Traits\SqlAssertionTrait;
13+
use Rebing\GraphQL\Tests\Support\Types\PostWithModelAndAliasType;
1314
use Rebing\GraphQL\Tests\Support\Queries\PostWithSelectFieldsAndModelQuery;
1415
use Rebing\GraphQL\Tests\Support\Queries\PostWithSelectFieldsNoModelQuery;
16+
use Rebing\GraphQL\Tests\Support\Queries\PostWithSelectFieldsAndModelAndAliasQuery;
1517

1618
class SelectFieldsTest extends TestCaseDatabase
1719
{
@@ -95,6 +97,40 @@ public function testWithSelectFieldsAndModel(): void
9597
$this->assertEquals($expectedResult, $response->json());
9698
}
9799

100+
public function testWithSelectFieldsAndModelAndAlias(): void
101+
{
102+
$post = factory(Post::class)->create([
103+
'title' => 'Description of the post',
104+
]);
105+
106+
$graphql = <<<GRAQPHQL
107+
{
108+
postWithSelectFieldsAndModelAndAlias(id: $post->id) {
109+
id
110+
description
111+
}
112+
}
113+
GRAQPHQL;
114+
115+
$this->sqlCounterReset();
116+
117+
$response = $this->call('GET', '/graphql', [
118+
'query' => $graphql,
119+
]);
120+
121+
$this->assertSqlQueries(<<<'SQL'
122+
select "posts"."id", "posts"."title" from "posts" where "posts"."id" = ? limit 1;
123+
SQL
124+
);
125+
126+
$this->assertEquals($response->getStatusCode(), 200);
127+
128+
$result = $response->json();
129+
130+
$this->assertNull($result['data']['postWithSelectFieldsAndModelAndAlias']);
131+
$this->assertSame('Cannot return null for non-nullable field PostWithModelAndAlias.description.', $result['errors'][0]['debugMessage']);
132+
}
133+
98134
public function testWithSelectFieldsNoModel(): void
99135
{
100136
$post = factory(Post::class)->create([
@@ -142,6 +178,7 @@ protected function getEnvironmentSetUp($app)
142178
'query' => [
143179
PostQuery::class,
144180
PostWithSelectFieldsAndModelQuery::class,
181+
PostWithSelectFieldsAndModelAndAliasQuery::class,
145182
PostWithSelectFieldsNoModelQuery::class,
146183
],
147184
]);
@@ -151,6 +188,7 @@ protected function getEnvironmentSetUp($app)
151188
$app['config']->set('graphql.types', [
152189
PostType::class,
153190
PostWithModelType::class,
191+
PostWithModelAndAliasType::class,
154192
]);
155193

156194
$app['config']->set('app.debug', true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Support\Queries;
6+
7+
use GraphQL\Type\Definition\Type;
8+
use Rebing\GraphQL\Support\Query;
9+
use Rebing\GraphQL\Support\SelectFields;
10+
use Rebing\GraphQL\Support\Facades\GraphQL;
11+
use Rebing\GraphQL\Tests\Support\Models\Post;
12+
13+
class PostWithSelectFieldsAndModelAndAliasQuery extends Query
14+
{
15+
protected $attributes = [
16+
'name' => 'postWithSelectFieldsAndModelAndAlias',
17+
];
18+
19+
public function type()
20+
{
21+
return GraphQL::type('PostWithModelAndAlias');
22+
}
23+
24+
public function args()
25+
{
26+
return [
27+
'id' => [
28+
'type' => Type::nonNull(Type::id()),
29+
],
30+
];
31+
}
32+
33+
public function resolve($root, $args, SelectFields $selectFields)
34+
{
35+
return Post
36+
::select($selectFields->getSelect())
37+
->findOrFail($args['id']);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Support\Types;
6+
7+
use GraphQL\Type\Definition\Type;
8+
use Rebing\GraphQL\Tests\Support\Models\Post;
9+
use Rebing\GraphQL\Support\Type as GraphQLType;
10+
11+
class PostWithModelAndAliasType extends GraphQLType
12+
{
13+
protected $attributes = [
14+
'name' => 'PostWithModelAndAlias',
15+
'model' => Post::class,
16+
];
17+
18+
public function fields()
19+
{
20+
return [
21+
'id' => [
22+
'type' => Type::nonNull(Type::id()),
23+
],
24+
'description' => [
25+
'type' => Type::nonNull(Type::string()),
26+
'alias' => 'title',
27+
],
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)