From cb2aa2f1ce5885c1738545b9bc46dbfd9c14319a Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 1 Jul 2019 22:21:45 +0200 Subject: [PATCH 1/3] added relationship alias --- src/Support/SelectFields.php | 8 +- .../NestedRelationLoadingTest.php | 81 +++++++++++++++++-- .../NestedRelationLoadingTests/UserType.php | 9 +++ 3 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/Support/SelectFields.php b/src/Support/SelectFields.php index b15956df..d01f900a 100644 --- a/src/Support/SelectFields.php +++ b/src/Support/SelectFields.php @@ -164,7 +164,11 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare if (isset($parentType->config['model'])) { // Get the next parent type, so that 'with' queries could be made // Both keys for the relation are required (e.g 'id' <-> 'user_id') - $relation = call_user_func([app($parentType->config['model']), $key]); + + $relationsKey = Arr::get($fieldObject->config, 'alias', $key); + + $relation = call_user_func([app($parentType->config['model']), $relationsKey]); + // Add the foreign key here, if it's a 'belongsTo'/'belongsToMany' relation if (method_exists($relation, 'getForeignKey')) { $foreignKey = $relation->getForeignKey(); @@ -207,7 +211,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare self::addAlwaysFields($fieldObject, $field, $parentTable, true); - $with[$key] = self::getSelectableFieldsAndRelations($field, $newParentType, $customQuery, false); + $with[$relationsKey] = self::getSelectableFieldsAndRelations($field, $newParentType, $customQuery, false); } else { self::handleFields($field, $fieldObject->config['type'], $select, $with); } diff --git a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php index 3a8668c8..5c39176d 100644 --- a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php +++ b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php @@ -56,7 +56,8 @@ public function testQueryNoSelectFields(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries(<<<'SQL' + $this->assertSqlQueries( + <<<'SQL' select * from "users" order by "users"."id" asc; select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null order by "posts"."id" asc; 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 $result = GraphQL::query($graphql); - $this->assertSqlQueries(<<<'SQL' + $this->assertSqlQueries( + <<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null order by "posts"."id" asc; 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 $result = GraphQL::query($graphql); - $this->assertSqlQueries(<<<'SQL' + $this->assertSqlQueries( + <<<'SQL' select * from "users" order by "users"."id" asc; select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc; 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 $result = GraphQL::query($graphql); - $this->assertSqlQueries(<<<'SQL' + $this->assertSqlQueries( + <<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc; 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 $result = GraphQL::query($graphql); - $this->assertSqlQueries(<<<'SQL' + $this->assertSqlQueries( + <<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; 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; 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 $result = GraphQL::query($graphql); - $this->assertSqlQueries(<<<'SQL' + $this->assertSqlQueries( + <<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; 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; 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 $this->assertEquals($expectedResult, $result); } + public function testRelationshipAlias(): void + { + $users = factory(User::class, 1) + ->create() + ->each(function (User $user): void { + factory(Post::class) + ->create([ + 'flag' => true, + 'user_id' => $user->id, + ]); + }); + + + + $graphql = <<sqlCounterReset(); + + + $result = GraphQL::query($graphql); + + $this->assertSqlQueries( + <<<'SQL' +select "users"."id", "users"."name" from "users" order by "users"."id" asc; +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; +SQL + ); + + $expectedResult = [ + 'data' => [ + 'users' => [ + [ + 'id' => (string) $users[0]->id, + 'name' => $users[0]->name, + 'flaggedPosts' => [ + [ + 'body' => $users[0]->posts[0]->body, + 'id' => (string) $users[0]->posts[0]->id, + 'title' => $users[0]->posts[0]->title + ], + ], + ], + ], + ], + ]; + + $this->assertEquals($expectedResult, $result); + } + + protected function getEnvironmentSetUp($app) { parent::getEnvironmentSetUp($app); diff --git a/tests/Database/SelectFields/NestedRelationLoadingTests/UserType.php b/tests/Database/SelectFields/NestedRelationLoadingTests/UserType.php index 44525eab..ab6f7493 100644 --- a/tests/Database/SelectFields/NestedRelationLoadingTests/UserType.php +++ b/tests/Database/SelectFields/NestedRelationLoadingTests/UserType.php @@ -38,6 +38,15 @@ public function fields(): array $query->where('posts.flag', '=', $args['flag']); } + return $query; + }, + ], + 'flaggedPosts' => [ + 'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Post')))), + 'alias' => 'posts', + 'query' => function (array $args, HasMany $query): HasMany { + $query->where('posts.flag', '=', 1); + return $query; }, ], From 023ab076a9f2ffd72ab0c951bf72f8a10957785f Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 1 Jul 2019 20:31:17 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- .../NestedRelationLoadingTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php index 5c39176d..15885b03 100644 --- a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php +++ b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php @@ -821,9 +821,7 @@ public function testRelationshipAlias(): void ]); }); - - - $graphql = <<sqlCounterReset(); - $result = GraphQL::query($graphql); $this->assertSqlQueries( @@ -860,7 +857,7 @@ public function testRelationshipAlias(): void [ 'body' => $users[0]->posts[0]->body, 'id' => (string) $users[0]->posts[0]->id, - 'title' => $users[0]->posts[0]->title + 'title' => $users[0]->posts[0]->title, ], ], ], @@ -871,7 +868,6 @@ public function testRelationshipAlias(): void $this->assertEquals($expectedResult, $result); } - protected function getEnvironmentSetUp($app) { parent::getEnvironmentSetUp($app); From e9f8b8dc9532410e00b90b6a955b0d2ab3eee149 Mon Sep 17 00:00:00 2001 From: Markus Fischer Date: Mon, 1 Jul 2019 23:03:08 +0200 Subject: [PATCH 3/3] Fix codestyle and revert unrelated changes --- src/Support/SelectFields.php | 2 -- .../NestedRelationLoadingTest.php | 21 +++++++------------ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Support/SelectFields.php b/src/Support/SelectFields.php index d01f900a..d02a2e07 100644 --- a/src/Support/SelectFields.php +++ b/src/Support/SelectFields.php @@ -164,9 +164,7 @@ protected static function handleFields(array $requestedFields, GraphqlType $pare if (isset($parentType->config['model'])) { // Get the next parent type, so that 'with' queries could be made // Both keys for the relation are required (e.g 'id' <-> 'user_id') - $relationsKey = Arr::get($fieldObject->config, 'alias', $key); - $relation = call_user_func([app($parentType->config['model']), $relationsKey]); // Add the foreign key here, if it's a 'belongsTo'/'belongsToMany' relation diff --git a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php index 15885b03..db5e8a29 100644 --- a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php +++ b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php @@ -56,8 +56,7 @@ public function testQueryNoSelectFields(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select * from "users" order by "users"."id" asc; select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null order by "posts"."id" asc; select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null order by "comments"."id" asc; @@ -198,8 +197,7 @@ public function testQuerySelect(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null order by "posts"."id" asc; select * from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null order by "comments"."id" asc; @@ -340,8 +338,7 @@ public function testQueryWith(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select * from "users" order by "users"."id" asc; select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc; select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?, ?, ?) order by "comments"."id" asc; @@ -478,8 +475,7 @@ public function testQuerySelectAndWith(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; select "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc; select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?, ?, ?) order by "comments"."id" asc; @@ -629,8 +625,7 @@ public function testQuerySelectAndWithAndSubArgs(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; 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; select "comments"."body", "comments"."id", "comments"."title", "comments"."post_id" from "comments" where "comments"."post_id" in (?, ?) order by "comments"."id" asc; @@ -756,8 +751,7 @@ public function testQuerySelectAndWithAndNestedSubArgs(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; 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; 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; @@ -840,8 +834,7 @@ public function testRelationshipAlias(): void $result = GraphQL::query($graphql); - $this->assertSqlQueries( - <<<'SQL' + $this->assertSqlQueries(<<<'SQL' select "users"."id", "users"."name" from "users" order by "users"."id" asc; 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; SQL