Skip to content

Commit e794332

Browse files
authored
Pass the path array to CacheKeyAndTags::key() (#2176)
1 parent 6dedf08 commit e794332

6 files changed

Lines changed: 12 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
- Change `CacheKeyAndTags` interface signature class https://github.com/nuwave/lighthouse/pull/2176
13+
1214
### Changed
1315

1416
- Pass resolver arguments to `FieldBuilderDirective::handleFieldBuilder()` https://github.com/nuwave/lighthouse/pull/2234

src/Cache/CacheDirective.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
7171
? data_get($root, $rootCacheKey)
7272
: null;
7373
$fieldName = $resolveInfo->fieldName;
74+
$path = $resolveInfo->path;
7475

7576
$cache = $shouldUseTags
7677
? $this->cacheRepository->tags([
@@ -85,7 +86,8 @@ function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
8586
$parentName,
8687
$rootID,
8788
$fieldName,
88-
$args
89+
$args,
90+
$path
8991
);
9092

9193
// We found a matching value in the cache, so we can just return early without actually running the query.

src/Cache/CacheKeyAndTags.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ interface CacheKeyAndTags
1111
*
1212
* @param int|string|null $id
1313
* @param array<string, mixed> $args
14+
* @param array<int, string|int> $path
1415
*/
15-
public function key(?Authenticatable $user, bool $isPrivate, string $parentName, $id, string $fieldName, array $args): string;
16+
public function key(?Authenticatable $user, bool $isPrivate, string $parentName, $id, string $fieldName, array $args, array $path): string;
1617

1718
/**
1819
* Generate a tag for the parent.

src/Cache/CacheKeyAndTagsGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ class CacheKeyAndTagsGenerator implements CacheKeyAndTags
1212
/**
1313
* @param int|string|null $id
1414
* @param array<string, mixed> $args
15+
* @param array<int, string|int> $path
1516
*/
1617
public function key(
1718
?Authenticatable $user,
1819
bool $isPrivate,
1920
string $parentName,
2021
$id,
2122
string $fieldName,
22-
array $args
23+
array $args,
24+
array $path
2325
): string {
2426
$parts = [self::PREFIX];
2527

tests/Integration/Execution/DataLoader/RelationBatchLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public function testBatchLoaderWithExpiredCacheEntry(): void
637637
');
638638

639639
Cache::forget(
640-
(new CacheKeyAndTagsGenerator())->key(null, false, 'Post', $post2->id, 'comments', [])
640+
(new CacheKeyAndTagsGenerator())->key(null, false, 'Post', $post2->id, 'comments', [], ['posts', $post2->id, 'comments'])
641641
);
642642

643643
$secondRequest = $this->graphQL(/** @lang GraphQL */ '

tests/Integration/Schema/Directives/LimitDirectiveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function testLimitsWithCache(): void
171171
assert($cache instanceof CacheRepository);
172172

173173
$data = $cache->get(
174-
(new CacheKeyAndTagsGenerator())->key(null, false, 'User', $user2->id, 'tasks', ['limit' => 1])
174+
(new CacheKeyAndTagsGenerator())->key(null, false, 'User', $user2->id, 'tasks', ['limit' => 1], ['user', $user2->id, 'tasks'])
175175
);
176176
$this->assertIsArray($data);
177177

0 commit comments

Comments
 (0)