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 bug with prefetching on different nesting levels #519

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/PrefetchBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class PrefetchBuffer
/** @param array<int,mixed> $arguments The input arguments passed from GraphQL to the field. */
public function register(object $object, array $arguments): void
{
$this->objects[$this->computeHash($arguments)][] = $object;
$hash = $this->computeHash($arguments);

$this->objects[$hash][] = $object;
unset($this->results[$hash]);
}

/** @param array<int,mixed> $arguments The input arguments passed from GraphQL to the field. */
Expand All @@ -44,7 +47,8 @@ public function getObjectsByArguments(array $arguments): array
/** @param array<int,mixed> $arguments The input arguments passed from GraphQL to the field. */
public function purge(array $arguments): void
{
unset($this->objects[$this->computeHash($arguments)]);
$hash = $this->computeHash($arguments);
unset($this->objects[$hash], $this->results[$hash]);
}

/** @param array<int,mixed> $arguments The input arguments passed from GraphQL to the field. */
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/Integration/Controllers/BlogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Controllers;

use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Blog;

class BlogController
{
/** @return Blog[] */
#[Query]
public function getBlogs(): array
{
return [
new Blog(1),
new Blog(2),
];
}
}
71 changes: 71 additions & 0 deletions tests/Fixtures/Integration/Models/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;

use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
class Blog
{
public function __construct(
private int $id,
) {
}

#[Field(outputType: 'ID!')]
public function getId(): int
{
return $this->id;
}

/**
* @param Post[][] $prefetchedPosts
* @return Post[]
*/
#[Field(prefetchMethod: 'prefetchPosts')]
public function getPosts(array $prefetchedPosts): array
{
return $prefetchedPosts[$this->id] ?? [];
}

/**
* @param Blog[][] $prefetchedSubBlogs
* @return Blog[]
*/
#[Field(prefetchMethod: 'prefetchSubBlogs')]
public function getSubBlogs(array $prefetchedSubBlogs): array
{
return $prefetchedSubBlogs[$this->id] ?? [];
}

/**
* @param self[] $blogs
* @return Post[][]
*/
public function prefetchPosts(array $blogs): array
{
$posts = [];
foreach ($blogs as $blog) {
$blogId = $blog->getId();
$posts[$blogId][] = new Post("post-$blogId.1");
$posts[$blogId][] = new Post("post-$blogId.2");
}
return $posts;
}

/**
* @param self[] $blogs
* @return Blog[][]
*/
public function prefetchSubBlogs(array $blogs): array
{
$subBlogs = [];
foreach ($blogs as $blog) {
$blogId = $blog->getId();
$subBlogId = $blogId * 10;
$subBlogs[$blogId][] = new Blog($subBlogId);
}
return $subBlogs;
}
}
21 changes: 21 additions & 0 deletions tests/Fixtures/Integration/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;

use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
class Comment
{
public function __construct(
private string $text
) {
}

#[Field]
public function getText(): string
{
return $this->text;
}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/Integration/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ public function setSummary(?string $summary): void
$this->summary = $summary;
}

/**
* @param Comment[][] $prefetchedComments
* @return Comment[]
*/
#[Field(prefetchMethod: 'prefetchComments')]
public function getComments(array $prefetchedComments): array
{
return $prefetchedComments[$this->title] ?? [];
}

/**
* @param self[] $posts
* @return Comment[][]
*/
public function prefetchComments(array $posts): array
{
$comments = [];
foreach ($posts as $post) {
$comments[$post->title][] = new Comment("comment for $post->title");
}
return $comments;
}

/**
* @param string $inaccessible
*/
Expand Down
115 changes: 115 additions & 0 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2500,4 +2500,119 @@ public function isAllowed(string $right, $subject = null): bool
$data = $this->getSuccessResult($result);
$this->assertSame(["graph", "ql"], $data['updateTrickyProduct']['list']);
}

public function testPrefetchingOfSameTypeInDifferentNestingLevels(): void
{
/** @var Schema $schema */
$schema = $this->mainContainer->get(Schema::class);

$schema->assertValid();

$queryString = '
query {
blogs {
id
subBlogs {
id
posts {
title
comments {
text
}
}
}
posts {
title
comments {
text
}
}
}
}
';

$result = GraphQL::executeQuery(
$schema,
$queryString,
null,
new Context()
);

$this->assertSame([
'blogs' => [
[
'id' => '1',
'subBlogs' => [
[
'id' => '10',
'posts' => [
[
'title' => 'post-10.1',
'comments' => [
['text' => 'comment for post-10.1'],
],
],
[
'title' => 'post-10.2',
'comments' => [
['text' => 'comment for post-10.2'],
],
]
]
]
],
'posts' => [
[
'title' => 'post-1.1',
'comments' => [
['text' => 'comment for post-1.1'],
],
],
[
'title' => 'post-1.2',
'comments' => [
['text' => 'comment for post-1.2'],
],
]
]
],
[
'id' => '2',
'subBlogs' => [
[
'id' => '20',
'posts' => [
[
'title' => 'post-20.1',
'comments' => [
['text' => 'comment for post-20.1'],
],
],
[
'title' => 'post-20.2',
'comments' => [
['text' => 'comment for post-20.2'],
],
]
]
]
],
'posts' => [
[
'title' => 'post-2.1',
'comments' => [
['text' => 'comment for post-2.1'],
],
],
[
'title' => 'post-2.2',
'comments' => [
['text' => 'comment for post-2.2'],
],
]
]
],
]
], $this->getSuccessResult($result));
}
}