-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug with prefetching on different nesting levels (#702)
* Fix bug with prefetching on different nesting levels Allow Promise to be returned from prefetchMethod with returnRequested: true * revert to previous Prefetch behavior * allow promise to be returned in methods * add nesting preload test * CR fixes * CR fixes * add Promise type mapping docs --------- Co-authored-by: Andrii Sudiev <andrii.sudiev@carid.com>
- Loading branch information
Showing
12 changed files
with
408 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models; | ||
|
||
use GraphQL\Deferred; | ||
use TheCodingMachine\GraphQLite\Annotations\Field; | ||
use TheCodingMachine\GraphQLite\Annotations\Prefetch; | ||
use TheCodingMachine\GraphQLite\Annotations\Type; | ||
|
||
#[Type] | ||
class Blog | ||
{ | ||
public function __construct( | ||
private readonly int $id, | ||
) { | ||
} | ||
|
||
#[Field(outputType: 'ID!')] | ||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @param Post[][] $prefetchedPosts | ||
* | ||
* @return Post[] | ||
*/ | ||
#[Field] | ||
public function getPosts( | ||
#[Prefetch('prefetchPosts')] | ||
array $prefetchedPosts, | ||
): array { | ||
return $prefetchedPosts[$this->id]; | ||
} | ||
|
||
/** @param Blog[][] $prefetchedSubBlogs */ | ||
#[Field(outputType: '[Blog!]!')] | ||
public function getSubBlogs( | ||
#[Prefetch('prefetchSubBlogs')] | ||
array $prefetchedSubBlogs, | ||
): Deferred { | ||
return new Deferred(fn () => $prefetchedSubBlogs[$this->id]); | ||
} | ||
|
||
/** | ||
* @param Blog[] $blogs | ||
* | ||
* @return Post[][] | ||
*/ | ||
public static function prefetchPosts(iterable $blogs): array | ||
{ | ||
$posts = []; | ||
foreach ($blogs as $blog) { | ||
$blogId = $blog->getId(); | ||
$posts[$blog->getId()] = [ | ||
new Post('post-' . $blogId . '.1'), | ||
new Post('post-' . $blogId . '.2'), | ||
]; | ||
} | ||
|
||
return $posts; | ||
} | ||
|
||
/** | ||
* @param Blog[] $blogs | ||
* | ||
* @return Blog[][] | ||
*/ | ||
public static function prefetchSubBlogs(iterable $blogs): array | ||
{ | ||
$subBlogs = []; | ||
foreach ($blogs as $blog) { | ||
$blogId = $blog->getId(); | ||
$subBlogId = $blogId * 10; | ||
$subBlogs[$blog->id] = [new Blog($subBlogId)]; | ||
} | ||
|
||
return $subBlogs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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 readonly string $text, | ||
) { | ||
} | ||
|
||
#[Field] | ||
public function getText(): string | ||
{ | ||
return $this->text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.