Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interface fields selection #607

Merged
merged 5 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ parameters:

-
message: "#^Function factory invoked with 1 parameter, 0 required\\.$#"
count: 14
count: 17
path: tests/Database/SelectFields/InterfaceTests/InterfaceTest.php

-
Expand Down
4 changes: 2 additions & 2 deletions src/Support/SelectFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ protected static function handleFields(
}
}

// If parent type is an union we select all fields
// If parent type is an union or interface we select all fields
// because we don't know which other fields are required
if (is_a($parentType, UnionType::class)) {
if (is_a($parentType, UnionType::class) || is_a($parentType, \GraphQL\Type\Definition\InterfaceType::class)) {
$select = ['*'];
}
}
Expand Down
91 changes: 86 additions & 5 deletions tests/Database/SelectFields/InterfaceTests/InterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testGeneratedSqlQuery(): void

$this->assertSqlQueries(
<<<'SQL'
select "title" from "posts";
select * from "posts";
SQL
);

Expand Down Expand Up @@ -82,7 +82,7 @@ public function testGeneratedRelationSqlQuery(): void

$this->assertSqlQueries(
<<<'SQL'
select "id", "title" from "posts";
select * from "posts";
select "comments"."title", "comments"."post_id", "comments"."id" from "comments" where "comments"."post_id" in (?) and "id" >= ? order by "comments"."id" asc;
SQL
);
Expand Down Expand Up @@ -155,7 +155,86 @@ public function testGeneratedInterfaceFieldSqlQuery(): void
<<<'SQL'
select "users"."id" from "users";
select "likes"."likable_id", "likes"."likable_type", "likes"."user_id", "likes"."id" from "likes" where "likes"."user_id" in (?);
select "id", "title" from "posts" where "posts"."id" in (?);
select * from "posts" where "posts"."id" in (?);
SQL
);
}

$expectedResult = [
'data' => [
'userQuery' => [
[
'id' => (string) $user->id,
'likes' => [
[
'likable' => [
'id' => (string) $post->id,
'title' => $post->title,
],
],
],
],
],
],
];
$this->assertSame($expectedResult, $result);
}

public function testGeneratedInterfaceFieldInlineFragmentsAndAlias(): void
{
$post = factory(Post::class)
->create([
'title' => 'Title of the post',
]);
factory(Comment::class)
->create([
'title' => 'Title of the comment',
'post_id' => $post->id,
]);

$user = factory(User::class)->create();
Like::create([
'likable_id' => $post->id,
'likable_type' => Post::class,
'user_id' => $user->id,
]);

$graphql = <<<'GRAPHQL'
{
userQuery {
id
likes{
likable{
id
title
...on Post {
created_at
alias_updated_at
}
}
}
}
}
GRAPHQL;

$this->sqlCounterReset();

$result = $this->graphql($graphql);

if (Application::VERSION < '5.6') {
$this->assertSqlQueries(
<<<'SQL'
select "users"."id" from "users";
select "likes"."likable_id", "likes"."likable_type", "likes"."user_id", "likes"."id" from "likes" where "likes"."user_id" in (?);
select * from "posts" where "posts"."id" in (?);
SQL
);
} else {
$this->assertSqlQueries(
<<<'SQL'
select "users"."id" from "users";
select "likes"."likable_id", "likes"."likable_type", "likes"."user_id", "likes"."id" from "likes" where "likes"."user_id" in (?);
select * from "posts" where "posts"."id" in (?);
SQL
);
}
Expand All @@ -170,6 +249,8 @@ public function testGeneratedInterfaceFieldSqlQuery(): void
'likable' => [
'id' => (string) $post->id,
'title' => $post->title,
'created_at' => $post->created_at->toDateTimeString(),
'alias_updated_at' => $post->updated_at->toDateTimeString(),
],
],
],
Expand Down Expand Up @@ -240,7 +321,7 @@ public function testGeneratedInterfaceFieldWithRelationSqlQuery(): void
<<<'SQL'
select "users"."id" from "users";
select "likes"."likable_id", "likes"."likable_type", "likes"."user_id", "likes"."id" from "likes" where "likes"."user_id" in (?, ?);
select "id", "title" from "posts" where "posts"."id" in (?);
select * from "posts" where "posts"."id" in (?);
select "likes"."id", "likes"."likable_id", "likes"."likable_type" from "likes" where "likes"."likable_id" in (?) and "likes"."likable_type" = ? and 0=0;
SQL
);
Expand Down Expand Up @@ -353,7 +434,7 @@ public function testGeneratedInterfaceFieldWithRelationAndCustomQueryOnInterface
<<<'SQL'
select "users"."id" from "users";
select "likes"."likable_id", "likes"."likable_type", "likes"."user_id", "likes"."id" from "likes" where "likes"."user_id" in (?, ?);
select "id", "title" from "comments" where "comments"."id" in (?);
select * from "comments" where "comments"."id" in (?);
select "likes"."id", "likes"."likable_id", "likes"."likable_type" from "likes" where "likes"."likable_id" in (?) and "likes"."likable_type" = ? and 1=1;
SQL
);
Expand Down
7 changes: 7 additions & 0 deletions tests/Database/SelectFields/InterfaceTests/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public function fields(): array

return
[
'created_at' => [
'type' => Type::string(),
],
'alias_updated_at' => [
'type' => Type::string(),
'alias' => 'updated_at',
],
'likes' => [
'type' => Type::listOf(GraphQL::type('Like')),
'query' => function (array $args, MorphMany $query) {
Expand Down