Skip to content

Commit

Permalink
Fix after pull master and resolving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
grynchuk committed Mar 8, 2024
1 parent 7746a77 commit 8c648a5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/Parameters/PrefetchDataParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, mixed> $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);
Expand Down
1 change: 1 addition & 0 deletions src/PrefetchBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 8 additions & 4 deletions tests/Fixtures/Integration/Types/CompanyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = [];

Expand Down
23 changes: 13 additions & 10 deletions tests/Fixtures/Integration/Types/ContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tests/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8c648a5

Please sign in to comment.