Skip to content

Commit 76f16fa

Browse files
committed
added relationship alias
1 parent 86e42b9 commit 76f16fa

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

src/Support/SelectFields.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
164164
if (isset($parentType->config['model'])) {
165165
// Get the next parent type, so that 'with' queries could be made
166166
// Both keys for the relation are required (e.g 'id' <-> 'user_id')
167-
$relation = call_user_func([app($parentType->config['model']), $key]);
167+
168+
$relationsKey = Arr::get($fieldObject->config, 'alias', $key);
169+
170+
$relation = call_user_func([app($parentType->config['model']), $relationsKey]);
171+
168172
// Add the foreign key here, if it's a 'belongsTo'/'belongsToMany' relation
169173
if (method_exists($relation, 'getForeignKey')) {
170174
$foreignKey = $relation->getForeignKey();
@@ -207,7 +211,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare
207211

208212
self::addAlwaysFields($fieldObject, $field, $parentTable, true);
209213

210-
$with[$key] = self::getSelectableFieldsAndRelations($field, $newParentType, $customQuery, false);
214+
$with[$relationsKey] = self::getSelectableFieldsAndRelations($field, $newParentType, $customQuery, false);
211215
} else {
212216
self::handleFields($field, $fieldObject->config['type'], $select, $with);
213217
}

tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function testQueryNoSelectFields(): void
5656

5757
$result = GraphQL::query($graphql);
5858

59-
$this->assertSqlQueries(<<<'SQL'
59+
$this->assertSqlQueries(
60+
<<<'SQL'
6061
select * from "users" order by "users"."id" asc;
6162
select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null order by "posts"."id" asc;
6263
select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null order by "comments"."id" asc;
@@ -197,7 +198,8 @@ public function testQuerySelect(): void
197198

198199
$result = GraphQL::query($graphql);
199200

200-
$this->assertSqlQueries(<<<'SQL'
201+
$this->assertSqlQueries(
202+
<<<'SQL'
201203
select "users"."id", "users"."name" from "users" order by "users"."id" asc;
202204
select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null order by "posts"."id" asc;
203205
select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null order by "comments"."id" asc;
@@ -338,7 +340,8 @@ public function testQueryWith(): void
338340

339341
$result = GraphQL::query($graphql);
340342

341-
$this->assertSqlQueries(<<<'SQL'
343+
$this->assertSqlQueries(
344+
<<<'SQL'
342345
select * from "users" order by "users"."id" asc;
343346
select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc;
344347
select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?, ?, ?) order by "comments"."id" asc;
@@ -475,7 +478,8 @@ public function testQuerySelectAndWith(): void
475478

476479
$result = GraphQL::query($graphql);
477480

478-
$this->assertSqlQueries(<<<'SQL'
481+
$this->assertSqlQueries(
482+
<<<'SQL'
479483
select "users"."id", "users"."name" from "users" order by "users"."id" asc;
480484
select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc;
481485
select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?, ?, ?) order by "comments"."id" asc;
@@ -625,7 +629,8 @@ public function testQuerySelectAndWithAndSubArgs(): void
625629

626630
$result = GraphQL::query($graphql);
627631

628-
$this->assertSqlQueries(<<<'SQL'
632+
$this->assertSqlQueries(
633+
<<<'SQL'
629634
select "users"."id", "users"."name" from "users" order by "users"."id" asc;
630635
select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) and "posts"."flag" = ? order by "posts"."id" asc;
631636
select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?) order by "comments"."id" asc;
@@ -751,7 +756,8 @@ public function testQuerySelectAndWithAndNestedSubArgs(): void
751756

752757
$result = GraphQL::query($graphql);
753758

754-
$this->assertSqlQueries(<<<'SQL'
759+
$this->assertSqlQueries(
760+
<<<'SQL'
755761
select "users"."id", "users"."name" from "users" order by "users"."id" asc;
756762
select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) and "posts"."flag" = ? order by "posts"."id" asc;
757763
select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?) and "comments"."flag" = ? order by "comments"."id" asc;
@@ -803,6 +809,69 @@ public function testQuerySelectAndWithAndNestedSubArgs(): void
803809
$this->assertEquals($expectedResult, $result);
804810
}
805811

812+
public function testRelationshipAlias(): void
813+
{
814+
$users = factory(User::class, 1)
815+
->create()
816+
->each(function (User $user): void {
817+
factory(Post::class)
818+
->create([
819+
'flag' => true,
820+
'user_id' => $user->id,
821+
]);
822+
});
823+
824+
825+
826+
$graphql = <<<GRAQPHQL
827+
{
828+
users(select: true, with: true) {
829+
id
830+
name
831+
flaggedPosts {
832+
body
833+
id
834+
title
835+
836+
}
837+
}
838+
}
839+
GRAQPHQL;
840+
841+
$this->sqlCounterReset();
842+
843+
844+
$result = GraphQL::query($graphql);
845+
846+
$this->assertSqlQueries(
847+
<<<'SQL'
848+
select "users"."id", "users"."name" from "users" order by "users"."id" asc;
849+
select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?) and "posts"."flag" = ? order by "posts"."id" asc;
850+
SQL
851+
);
852+
853+
$expectedResult = [
854+
'data' => [
855+
'users' => [
856+
[
857+
'id' => (string) $users[0]->id,
858+
'name' => $users[0]->name,
859+
'flaggedPosts' => [
860+
[
861+
'body' => $users[0]->posts[0]->body,
862+
'id' => (string) $users[0]->posts[0]->id,
863+
'title' => $users[0]->posts[0]->title
864+
],
865+
],
866+
],
867+
],
868+
],
869+
];
870+
871+
$this->assertEquals($expectedResult, $result);
872+
}
873+
874+
806875
protected function getEnvironmentSetUp($app)
807876
{
808877
parent::getEnvironmentSetUp($app);

tests/Database/SelectFields/NestedRelationLoadingTests/UserType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public function fields(): array
3838
$query->where('posts.flag', '=', $args['flag']);
3939
}
4040

41+
return $query;
42+
},
43+
],
44+
'flaggedPosts' => [
45+
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Post')))),
46+
'alias' => 'posts',
47+
'query' => function (array $args, HasMany $query): HasMany {
48+
$query->where('posts.flag', '=', 1);
49+
4150
return $query;
4251
},
4352
],

0 commit comments

Comments
 (0)