Skip to content

Commit f6cb9c8

Browse files
authored
Simplify cache configuration access (#2706)
1 parent 26a5b6a commit f6cb9c8

File tree

3 files changed

+20
-38
lines changed

3 files changed

+20
-38
lines changed

CHANGELOG.md

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

1010
## Unreleased
1111

12+
## v6.62.2
13+
14+
### Changed
15+
16+
- Simplify cache configuration access https://github.com/nuwave/lighthouse/pull/2706
17+
1218
## v6.62.1
1319

1420
### Fixed

src/GraphQL.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,18 @@ public function executeOperation(OperationParams $params, GraphQLContext $contex
281281
*/
282282
public function parse(string $query, string $hash): DocumentNode
283283
{
284-
$cacheConfig = $this->configRepository->get('lighthouse.query_cache');
284+
$queryCacheConfig = $this->configRepository->get('lighthouse.query_cache');
285285

286-
if (! $cacheConfig['enable']) {
286+
if (! $queryCacheConfig['enable']) {
287287
return $this->parseQuery($query);
288288
}
289289

290290
$cacheFactory = Container::getInstance()->make(CacheFactory::class);
291-
$store = $cacheFactory->store($cacheConfig['store']);
291+
$store = $cacheFactory->store($queryCacheConfig['store']);
292292

293293
return $store->remember(
294294
"lighthouse:query:{$hash}",
295-
$cacheConfig['ttl'],
295+
$queryCacheConfig['ttl'],
296296
fn (): DocumentNode => $this->parseQuery($query),
297297
);
298298
}
@@ -319,17 +319,17 @@ public function toSerializableArray(ExecutionResult $result): array
319319
public function loadPersistedQuery(string $sha256hash): DocumentNode
320320
{
321321
$lighthouseConfig = $this->configRepository->get('lighthouse');
322-
$cacheConfig = $lighthouseConfig['query_cache'] ?? null;
322+
$queryCacheConfig = $lighthouseConfig['query_cache'];
323323
if (
324-
! ($lighthouseConfig['persisted_queries'] ?? false)
325-
|| ! ($cacheConfig['enable'] ?? false)
324+
! $lighthouseConfig['persisted_queries']
325+
|| ! $queryCacheConfig['enable']
326326
) {
327327
// https://github.com/apollographql/apollo-server/blob/37a5c862261806817a1d71852c4e1d9cdb59eab2/packages/apollo-server-errors/src/index.ts#L240-L248
328328
throw new Error('PersistedQueryNotSupported', null, null, [], null, null, ['code' => 'PERSISTED_QUERY_NOT_SUPPORTED']);
329329
}
330330

331331
$cacheFactory = Container::getInstance()->make(CacheFactory::class);
332-
$store = $cacheFactory->store($cacheConfig['store']);
332+
$store = $cacheFactory->store($queryCacheConfig['store']);
333333

334334
return $store->get("lighthouse:query:{$sha256hash}")
335335
// https://github.com/apollographql/apollo-server/blob/37a5c862261806817a1d71852c4e1d9cdb59eab2/packages/apollo-server-errors/src/index.ts#L230-L239
@@ -407,17 +407,17 @@ protected function validateCacheableRules(
407407
return DocumentValidator::validate($schema, $query, $validationRules); // @phpstan-ignore return.type (TODO remove ignore when requiring a newer version of webonyx/graphql-php)
408408
}
409409

410-
$cacheConfig = $this->configRepository->get('lighthouse.validation_cache');
410+
$validationCacheConfig = $this->configRepository->get('lighthouse.validation_cache');
411411

412-
if (! isset($cacheConfig['enable']) || ! $cacheConfig['enable']) {
412+
if (! $validationCacheConfig['enable']) {
413413
return DocumentValidator::validate($schema, $query, $validationRules); // @phpstan-ignore return.type (TODO remove ignore when requiring a newer version of webonyx/graphql-php)
414414
}
415415

416-
$cacheKey = "lighthouse:validation:{$schemaHash}:{$queryHash}";
417-
418416
$cacheFactory = Container::getInstance()->make(CacheFactory::class);
417+
$store = $cacheFactory->store($validationCacheConfig['store']);
418+
419+
$cacheKey = "lighthouse:validation:{$schemaHash}:{$queryHash}";
419420

420-
$store = $cacheFactory->store($cacheConfig['store']);
421421
$cachedResult = $store->get($cacheKey);
422422
if ($cachedResult !== null) {
423423
return $cachedResult;
@@ -432,7 +432,7 @@ protected function validateCacheableRules(
432432
return $result; // @phpstan-ignore return.type (TODO remove ignore when requiring a newer version of webonyx/graphql-php)
433433
}
434434

435-
$store->put($cacheKey, $result, $cacheConfig['ttl']);
435+
$store->put($cacheKey, $result, $validationCacheConfig['ttl']);
436436

437437
return $result;
438438
}

tests/Integration/ValidationCachingTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Contracts\Cache\Factory as CacheFactory;
77
use Illuminate\Contracts\Config\Repository as ConfigRepository;
88
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
9-
use Illuminate\Support\Facades\Cache;
109
use Illuminate\Support\Facades\Event;
1110
use Tests\TestCase;
1211
use Tests\Utils\Queries\Foo;
@@ -74,29 +73,6 @@ public function testDisabled(): void
7473
$event->assertDispatchedTimes(KeyWritten::class, 0);
7574
}
7675

77-
public function testConfigMissing(): void
78-
{
79-
$config = $this->app->make(ConfigRepository::class);
80-
$config->set('lighthouse.query_cache.enable', false);
81-
$config->set('lighthouse.validation_cache', null);
82-
83-
$event = Event::fake();
84-
85-
$this->graphQL(/** @lang GraphQL */ '
86-
{
87-
foo
88-
}
89-
')->assertExactJson([
90-
'data' => [
91-
'foo' => Foo::THE_ANSWER,
92-
],
93-
]);
94-
95-
$event->assertDispatchedTimes(CacheMissed::class, 0);
96-
$event->assertDispatchedTimes(CacheHit::class, 0);
97-
$event->assertDispatchedTimes(KeyWritten::class, 0);
98-
}
99-
10076
public function testErrorsAreNotCached(): void
10177
{
10278
$config = $this->app->make(ConfigRepository::class);

0 commit comments

Comments
 (0)