Skip to content

Commit 8c648a5

Browse files
committed
Fix after pull master and resolving conflicts
1 parent 7746a77 commit 8c648a5

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

src/Parameters/PrefetchDataParameter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function resolve(object|null $source, array $args, mixed $context, Resolv
4343
}
4444

4545
$prefetchBuffer = $context->getPrefetchBuffer($this);
46-
$prefetchBuffer->register($source, $args);
46+
$prefetchBuffer->register($source, $args, $info);
4747

4848
// The way this works is simple: GraphQL first iterates over every requested field and calls ->resolve()
4949
// on it. That, in turn, calls this method. GraphQL doesn't need the actual value just yet; it simply
@@ -53,20 +53,20 @@ public function resolve(object|null $source, array $args, mixed $context, Resolv
5353
// needed, GraphQL calls the callback of Deferred below. That's when we call the prefetch method,
5454
// already knowing all the requested fields (source-arguments combinations).
5555
return new Deferred(function () use ($info, $context, $args, $prefetchBuffer) {
56-
if (! $prefetchBuffer->hasResult($args)) {
56+
if (! $prefetchBuffer->hasResult($args, $info)) {
5757
$prefetchResult = $this->computePrefetch($args, $context, $info, $prefetchBuffer);
5858

59-
$prefetchBuffer->storeResult($prefetchResult, $args);
59+
$prefetchBuffer->storeResult($prefetchResult, $args, $info);
6060
}
6161

62-
return $prefetchResult ?? $prefetchBuffer->getResult($args);
62+
return $prefetchResult ?? $prefetchBuffer->getResult($args, $info);
6363
});
6464
}
6565

6666
/** @param array<string, mixed> $args */
6767
private function computePrefetch(array $args, mixed $context, ResolveInfo $info, PrefetchBuffer $prefetchBuffer): mixed
6868
{
69-
$sources = $prefetchBuffer->getObjectsByArguments($args);
69+
$sources = $prefetchBuffer->getObjectsByArguments($args, $info);
7070
$toPassPrefetchArgs = QueryField::paramsToArguments($this->fieldName, $this->parameters, null, $args, $context, $info, $this->resolver);
7171

7272
return ($this->resolver)($sources, ...$toPassPrefetchArgs);

src/PrefetchBuffer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private function computeHash(
3636
): string {
3737
if (
3838
null === $info
39+
|| false === isset($info->operation)
3940
|| null === ($queryBody = $info->operation->loc?->source?->body)
4041
) {
4142
return md5(serialize($arguments));

tests/Fixtures/Integration/Types/CompanyType.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
88
use TheCodingMachine\GraphQLite\Annotations\Field;
9+
use TheCodingMachine\GraphQLite\Annotations\Prefetch;
910
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Company;
1011
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Contact;
1112

@@ -23,14 +24,17 @@ public function getName(Company $company): string
2324
}
2425

2526
/**
26-
* @Field(prefetchMethod="prefetchContacts")
27+
* @Field()
2728
*/
28-
public function getContact(Company $company, array $contacts): ?Contact
29-
{
29+
public function getContact(
30+
Company $company,
31+
#[Prefetch('prefetchContacts')]
32+
array $contacts
33+
): ?Contact {
3034
return $contacts[$company->name] ?? null;
3135
}
3236

33-
public function prefetchContacts(array $companies): array
37+
public static function prefetchContacts(array $companies): array
3438
{
3539
$contacts = [];
3640

tests/Fixtures/Integration/Types/ContactType.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,23 @@ public static function prefetchContacts(iterable $contacts, string $prefix)
5656
}
5757

5858
/**
59-
* @Field(prefetchMethod="prefetchPosts")
59+
* @Field()
6060
* @return Post[]|null
6161
*/
62-
public function getPosts($contact, $posts): ?array
63-
{
62+
public function getPosts(
63+
Contact $contact,
64+
#[Prefetch('prefetchPosts')]
65+
$posts
66+
): ?array {
6467
return $posts[$contact->getName()] ?? null;
6568
}
6669

67-
public function prefetchPosts(iterable $contacts): array
70+
public static function prefetchPosts(iterable $contacts): array
6871
{
6972
$posts = [];
7073
foreach ($contacts as $contact) {
7174
$contactPost = array_filter(
72-
$this->getContactPosts(),
75+
self::getContactPosts(),
7376
fn(Post $post) => $post->author?->getName() === $contact->getName()
7477
);
7578

@@ -83,16 +86,16 @@ public function prefetchPosts(iterable $contacts): array
8386
return $posts;
8487
}
8588

86-
private function getContactPosts(): array
89+
private static function getContactPosts(): array
8790
{
8891
return [
89-
$this->generatePost('First Joe post', '1', new Contact('Joe')),
90-
$this->generatePost('First Bill post', '2', new Contact('Bill')),
91-
$this->generatePost('First Kate post', '3', new Contact('Kate')),
92+
self::generatePost('First Joe post', '1', new Contact('Joe')),
93+
self::generatePost('First Bill post', '2', new Contact('Bill')),
94+
self::generatePost('First Kate post', '3', new Contact('Kate')),
9295
];
9396
}
9497

95-
private function generatePost(
98+
private static function generatePost(
9699
string $title,
97100
string $id,
98101
Contact $author,

tests/SchemaFactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use TheCodingMachine\GraphQLite\Fixtures\Integration\Types\ContactType;
2929
use TheCodingMachine\GraphQLite\Fixtures\Integration\Types\ExtendedContactType;
3030
use TheCodingMachine\GraphQLite\Fixtures\Integration\Types\PostType;
31-
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
3231
use TheCodingMachine\GraphQLite\Fixtures\TestSelfType;
3332
use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper;
3433
use TheCodingMachine\GraphQLite\Mappers\DuplicateMappingException;

0 commit comments

Comments
 (0)