diff --git a/src/Support/SelectFields.php b/src/Support/SelectFields.php index b15956df..d02a2e07 100644 --- a/src/Support/SelectFields.php +++ b/src/Support/SelectFields.php @@ -164,7 +164,9 @@ 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 +209,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..db5e8a29 100644 --- a/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php +++ b/tests/Database/SelectFields/NestedRelationLoadingTests/NestedRelationLoadingTest.php @@ -803,6 +803,64 @@ 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 = <<<'GRAQPHQL' +{ + users(select: true, with: true) { + id + name + flaggedPosts { + body + id + title + + } + } +} +GRAQPHQL; + + $this->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; }, ],