diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ee50abe4f..d5f2911e30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co ## Unreleased +## v6.62.2 + +### Changed + +- Simplify cache configuration access https://github.com/nuwave/lighthouse/pull/2706 + ## v6.62.1 ### Fixed diff --git a/src/GraphQL.php b/src/GraphQL.php index 1cf90c1ebc..99f0276e6e 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -281,18 +281,18 @@ public function executeOperation(OperationParams $params, GraphQLContext $contex */ public function parse(string $query, string $hash): DocumentNode { - $cacheConfig = $this->configRepository->get('lighthouse.query_cache'); + $queryCacheConfig = $this->configRepository->get('lighthouse.query_cache'); - if (! $cacheConfig['enable']) { + if (! $queryCacheConfig['enable']) { return $this->parseQuery($query); } $cacheFactory = Container::getInstance()->make(CacheFactory::class); - $store = $cacheFactory->store($cacheConfig['store']); + $store = $cacheFactory->store($queryCacheConfig['store']); return $store->remember( "lighthouse:query:{$hash}", - $cacheConfig['ttl'], + $queryCacheConfig['ttl'], fn (): DocumentNode => $this->parseQuery($query), ); } @@ -319,17 +319,17 @@ public function toSerializableArray(ExecutionResult $result): array public function loadPersistedQuery(string $sha256hash): DocumentNode { $lighthouseConfig = $this->configRepository->get('lighthouse'); - $cacheConfig = $lighthouseConfig['query_cache'] ?? null; + $queryCacheConfig = $lighthouseConfig['query_cache']; if ( - ! ($lighthouseConfig['persisted_queries'] ?? false) - || ! ($cacheConfig['enable'] ?? false) + ! $lighthouseConfig['persisted_queries'] + || ! $queryCacheConfig['enable'] ) { // https://github.com/apollographql/apollo-server/blob/37a5c862261806817a1d71852c4e1d9cdb59eab2/packages/apollo-server-errors/src/index.ts#L240-L248 throw new Error('PersistedQueryNotSupported', null, null, [], null, null, ['code' => 'PERSISTED_QUERY_NOT_SUPPORTED']); } $cacheFactory = Container::getInstance()->make(CacheFactory::class); - $store = $cacheFactory->store($cacheConfig['store']); + $store = $cacheFactory->store($queryCacheConfig['store']); return $store->get("lighthouse:query:{$sha256hash}") // https://github.com/apollographql/apollo-server/blob/37a5c862261806817a1d71852c4e1d9cdb59eab2/packages/apollo-server-errors/src/index.ts#L230-L239 @@ -407,17 +407,17 @@ protected function validateCacheableRules( return DocumentValidator::validate($schema, $query, $validationRules); // @phpstan-ignore return.type (TODO remove ignore when requiring a newer version of webonyx/graphql-php) } - $cacheConfig = $this->configRepository->get('lighthouse.validation_cache'); + $validationCacheConfig = $this->configRepository->get('lighthouse.validation_cache'); - if (! isset($cacheConfig['enable']) || ! $cacheConfig['enable']) { + if (! $validationCacheConfig['enable']) { return DocumentValidator::validate($schema, $query, $validationRules); // @phpstan-ignore return.type (TODO remove ignore when requiring a newer version of webonyx/graphql-php) } - $cacheKey = "lighthouse:validation:{$schemaHash}:{$queryHash}"; - $cacheFactory = Container::getInstance()->make(CacheFactory::class); + $store = $cacheFactory->store($validationCacheConfig['store']); + + $cacheKey = "lighthouse:validation:{$schemaHash}:{$queryHash}"; - $store = $cacheFactory->store($cacheConfig['store']); $cachedResult = $store->get($cacheKey); if ($cachedResult !== null) { return $cachedResult; @@ -432,7 +432,7 @@ protected function validateCacheableRules( return $result; // @phpstan-ignore return.type (TODO remove ignore when requiring a newer version of webonyx/graphql-php) } - $store->put($cacheKey, $result, $cacheConfig['ttl']); + $store->put($cacheKey, $result, $validationCacheConfig['ttl']); return $result; } diff --git a/tests/Integration/ValidationCachingTest.php b/tests/Integration/ValidationCachingTest.php index f5dadd1042..c211c6183c 100644 --- a/tests/Integration/ValidationCachingTest.php +++ b/tests/Integration/ValidationCachingTest.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Cache\Factory as CacheFactory; use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher; -use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Event; use Tests\TestCase; use Tests\Utils\Queries\Foo; @@ -74,29 +73,6 @@ public function testDisabled(): void $event->assertDispatchedTimes(KeyWritten::class, 0); } - public function testConfigMissing(): void - { - $config = $this->app->make(ConfigRepository::class); - $config->set('lighthouse.query_cache.enable', false); - $config->set('lighthouse.validation_cache', null); - - $event = Event::fake(); - - $this->graphQL(/** @lang GraphQL */ ' - { - foo - } - ')->assertExactJson([ - 'data' => [ - 'foo' => Foo::THE_ANSWER, - ], - ]); - - $event->assertDispatchedTimes(CacheMissed::class, 0); - $event->assertDispatchedTimes(CacheHit::class, 0); - $event->assertDispatchedTimes(KeyWritten::class, 0); - } - public function testErrorsAreNotCached(): void { $config = $this->app->make(ConfigRepository::class);