Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
24 changes: 0 additions & 24 deletions tests/Integration/ValidationCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realistically, this never happens due to the configuration merging.


$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);
Expand Down