-
Notifications
You must be signed in to change notification settings - Fork 100
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 #702
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee84251
Fix bug with prefetching on different nesting levels
ef75504
revert to previous Prefetch behavior
ffbb68b
allow promise to be returned in methods
6012450
add nesting preload test
dae8b0e
CR fixes
b6c54c2
CR fixes
ca0823a
add Promise type mapping docs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is storing the results, maybe "compute" isn't the right term to use for this method name. Also "compute" isn't really a great term to be using here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method was not named by me)
Should I rename it?
doPrefetch | prefetchValues | prefetchResults | processPrefetch | ... ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renaming a method where the logic contained within it has materially changed, is the right move. Otherwise, reading and understanding the code for the next developer becomes more confusing.
Being that it's return type is void, it's not really "prefetching results" - you'd expect a return value on that IMO.
processPrefetch
,cachePrefetchResults
,bufferPrefetch
; maybe something along these lines.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed method name to processPrefetch pushed to branch