From 8c648a54fb668c4801e2171b15e2486467638010 Mon Sep 17 00:00:00 2001 From: grynchuk Date: Fri, 8 Mar 2024 15:04:16 +0200 Subject: [PATCH] Fix after pull master and resolving conflicts --- src/Parameters/PrefetchDataParameter.php | 10 ++++---- src/PrefetchBuffer.php | 1 + .../Integration/Types/CompanyType.php | 12 ++++++---- .../Integration/Types/ContactType.php | 23 +++++++++++-------- tests/SchemaFactoryTest.php | 1 - 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/Parameters/PrefetchDataParameter.php b/src/Parameters/PrefetchDataParameter.php index fe81e4aceb..4b233c5e3c 100644 --- a/src/Parameters/PrefetchDataParameter.php +++ b/src/Parameters/PrefetchDataParameter.php @@ -43,7 +43,7 @@ public function resolve(object|null $source, array $args, mixed $context, Resolv } $prefetchBuffer = $context->getPrefetchBuffer($this); - $prefetchBuffer->register($source, $args); + $prefetchBuffer->register($source, $args, $info); // The way this works is simple: GraphQL first iterates over every requested field and calls ->resolve() // 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 // needed, GraphQL calls the callback of Deferred below. That's when we call the prefetch method, // already knowing all the requested fields (source-arguments combinations). return new Deferred(function () use ($info, $context, $args, $prefetchBuffer) { - if (! $prefetchBuffer->hasResult($args)) { + if (! $prefetchBuffer->hasResult($args, $info)) { $prefetchResult = $this->computePrefetch($args, $context, $info, $prefetchBuffer); - $prefetchBuffer->storeResult($prefetchResult, $args); + $prefetchBuffer->storeResult($prefetchResult, $args, $info); } - return $prefetchResult ?? $prefetchBuffer->getResult($args); + return $prefetchResult ?? $prefetchBuffer->getResult($args, $info); }); } /** @param array $args */ private function computePrefetch(array $args, mixed $context, ResolveInfo $info, PrefetchBuffer $prefetchBuffer): mixed { - $sources = $prefetchBuffer->getObjectsByArguments($args); + $sources = $prefetchBuffer->getObjectsByArguments($args, $info); $toPassPrefetchArgs = QueryField::paramsToArguments($this->fieldName, $this->parameters, null, $args, $context, $info, $this->resolver); return ($this->resolver)($sources, ...$toPassPrefetchArgs); diff --git a/src/PrefetchBuffer.php b/src/PrefetchBuffer.php index 84701d744c..9ca9312f8b 100644 --- a/src/PrefetchBuffer.php +++ b/src/PrefetchBuffer.php @@ -36,6 +36,7 @@ private function computeHash( ): string { if ( null === $info + || false === isset($info->operation) || null === ($queryBody = $info->operation->loc?->source?->body) ) { return md5(serialize($arguments)); diff --git a/tests/Fixtures/Integration/Types/CompanyType.php b/tests/Fixtures/Integration/Types/CompanyType.php index 28836a5968..98fded498e 100644 --- a/tests/Fixtures/Integration/Types/CompanyType.php +++ b/tests/Fixtures/Integration/Types/CompanyType.php @@ -6,6 +6,7 @@ use TheCodingMachine\GraphQLite\Annotations\ExtendType; use TheCodingMachine\GraphQLite\Annotations\Field; +use TheCodingMachine\GraphQLite\Annotations\Prefetch; use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Company; use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Contact; @@ -23,14 +24,17 @@ public function getName(Company $company): string } /** - * @Field(prefetchMethod="prefetchContacts") + * @Field() */ - public function getContact(Company $company, array $contacts): ?Contact - { + public function getContact( + Company $company, + #[Prefetch('prefetchContacts')] + array $contacts + ): ?Contact { return $contacts[$company->name] ?? null; } - public function prefetchContacts(array $companies): array + public static function prefetchContacts(array $companies): array { $contacts = []; diff --git a/tests/Fixtures/Integration/Types/ContactType.php b/tests/Fixtures/Integration/Types/ContactType.php index ee01d0ea65..90e2777c74 100644 --- a/tests/Fixtures/Integration/Types/ContactType.php +++ b/tests/Fixtures/Integration/Types/ContactType.php @@ -56,20 +56,23 @@ public static function prefetchContacts(iterable $contacts, string $prefix) } /** - * @Field(prefetchMethod="prefetchPosts") + * @Field() * @return Post[]|null */ - public function getPosts($contact, $posts): ?array - { + public function getPosts( + Contact $contact, + #[Prefetch('prefetchPosts')] + $posts + ): ?array { return $posts[$contact->getName()] ?? null; } - public function prefetchPosts(iterable $contacts): array + public static function prefetchPosts(iterable $contacts): array { $posts = []; foreach ($contacts as $contact) { $contactPost = array_filter( - $this->getContactPosts(), + self::getContactPosts(), fn(Post $post) => $post->author?->getName() === $contact->getName() ); @@ -83,16 +86,16 @@ public function prefetchPosts(iterable $contacts): array return $posts; } - private function getContactPosts(): array + private static function getContactPosts(): array { return [ - $this->generatePost('First Joe post', '1', new Contact('Joe')), - $this->generatePost('First Bill post', '2', new Contact('Bill')), - $this->generatePost('First Kate post', '3', new Contact('Kate')), + self::generatePost('First Joe post', '1', new Contact('Joe')), + self::generatePost('First Bill post', '2', new Contact('Bill')), + self::generatePost('First Kate post', '3', new Contact('Kate')), ]; } - private function generatePost( + private static function generatePost( string $title, string $id, Contact $author, diff --git a/tests/SchemaFactoryTest.php b/tests/SchemaFactoryTest.php index 1639fa040d..9ac3edceb9 100644 --- a/tests/SchemaFactoryTest.php +++ b/tests/SchemaFactoryTest.php @@ -28,7 +28,6 @@ use TheCodingMachine\GraphQLite\Fixtures\Integration\Types\ContactType; use TheCodingMachine\GraphQLite\Fixtures\Integration\Types\ExtendedContactType; use TheCodingMachine\GraphQLite\Fixtures\Integration\Types\PostType; -use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; use TheCodingMachine\GraphQLite\Fixtures\TestSelfType; use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper; use TheCodingMachine\GraphQLite\Mappers\DuplicateMappingException;