Skip to content

Commit 910a218

Browse files
authored
Make Automatic Persisted Queries work with hybrid cache mode (#2727)
1 parent 8bf8a33 commit 910a218

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

CHANGELOG.md

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

1010
## Unreleased
1111

12+
### Fixed
13+
14+
- Automatic Persisted Queries work with hybrid cache mode https://github.com/nuwave/lighthouse/pull/2727
15+
1216
## v6.63.1
1317

1418
### Fixed

src/GraphQL.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,19 @@ public function toSerializableArray(ExecutionResult $result): array
313313
public function loadPersistedQuery(string $sha256hash): DocumentNode
314314
{
315315
$lighthouseConfig = $this->configRepository->get('lighthouse');
316-
$queryCacheConfig = $lighthouseConfig['query_cache'];
317316
if (
318317
! $lighthouseConfig['persisted_queries']
319-
|| ! $queryCacheConfig['enable']
318+
|| ! $this->queryCache->isEnabled()
320319
) {
321320
// https://github.com/apollographql/apollo-server/blob/37a5c862261806817a1d71852c4e1d9cdb59eab2/packages/apollo-server-errors/src/index.ts#L240-L248
322321
throw new Error(message: 'PersistedQueryNotSupported', extensions: ['code' => 'PERSISTED_QUERY_NOT_SUPPORTED']);
323322
}
324323

325-
$cacheFactory = Container::getInstance()->make(CacheFactory::class);
326-
$store = $cacheFactory->store($queryCacheConfig['store']);
327-
328-
return $store->get("lighthouse:query:{$sha256hash}")
324+
return $this->queryCache->fromCacheOrParse(
325+
hash: $sha256hash,
329326
// https://github.com/apollographql/apollo-server/blob/37a5c862261806817a1d71852c4e1d9cdb59eab2/packages/apollo-server-errors/src/index.ts#L230-L239
330-
?? throw new Error(message: 'PersistedQueryNotFound', extensions: ['code' => 'PERSISTED_QUERY_NOT_FOUND']);
327+
parse: fn () => throw new Error(message: 'PersistedQueryNotFound', extensions: ['code' => 'PERSISTED_QUERY_NOT_FOUND']),
328+
);
331329
}
332330

333331
/** @return ErrorsHandler */

tests/Integration/AutomaticPersistedQueriesTest.php

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
<?php declare(strict_types=1);
22

33
use Illuminate\Contracts\Config\Repository as ConfigRepository;
4+
use Illuminate\Filesystem\FilesystemAdapter;
5+
use Illuminate\Support\Facades\Cache;
6+
use Illuminate\Support\Facades\Storage;
47
use Tests\TestCase;
58
use Tests\Utils\Queries\Foo;
69

710
final class AutomaticPersistedQueriesTest extends TestCase
811
{
9-
public function testEnabled(): void
12+
public function testEnabledWithStoreMode(): void
1013
{
1114
$config = $this->app->make(ConfigRepository::class);
1215
$config->set('lighthouse.query_cache.enable', true);
1316
$config->set('lighthouse.persisted_queries', true);
17+
$config->set('lighthouse.query_cache.mode', 'store');
1418

1519
$query = /** @lang GraphQL */ <<<'GRAPHQL'
1620
{
@@ -68,6 +72,77 @@ public function testEnabled(): void
6872
]);
6973
}
7074

75+
public function testEnabledWithHybridStore(): void
76+
{
77+
$filesystem = Storage::fake();
78+
$this->assertInstanceOf(FilesystemAdapter::class, $filesystem);
79+
80+
$config = $this->app->make(ConfigRepository::class);
81+
$config->set('lighthouse.query_cache.enable', true);
82+
$config->set('lighthouse.persisted_queries', true);
83+
$config->set('lighthouse.query_cache.mode', 'hybrid');
84+
$config->set('lighthouse.query_cache.opcache_path', $filesystem->path(''));
85+
86+
$query = /** @lang GraphQL */ <<<'GRAPHQL'
87+
{
88+
foo
89+
}
90+
GRAPHQL;
91+
92+
$sha256 = hash('sha256', $query);
93+
94+
$this->postGraphQL([
95+
'extensions' => [
96+
'persistedQuery' => [
97+
'version' => 1,
98+
'sha256Hash' => $sha256,
99+
],
100+
],
101+
])->assertJson([
102+
'errors' => [
103+
[
104+
'message' => 'PersistedQueryNotFound',
105+
'extensions' => [
106+
'code' => 'PERSISTED_QUERY_NOT_FOUND',
107+
],
108+
],
109+
],
110+
]);
111+
112+
// run sending the query
113+
$this->postGraphQL([
114+
'query' => $query,
115+
'extensions' => [
116+
'persistedQuery' => [
117+
'version' => 1,
118+
'sha256Hash' => $sha256,
119+
],
120+
],
121+
])->assertExactJson([
122+
'data' => [
123+
'foo' => Foo::THE_ANSWER,
124+
],
125+
]);
126+
127+
$filesystem->assertExists('lighthouse-query-' . $sha256 . '.php');
128+
// Simulate different server by deleting the local file - cache will fall back now
129+
$filesystem->delete('lighthouse-query-' . $sha256 . '.php');
130+
131+
// run without query, the query should still be cached
132+
$this->postGraphQL([
133+
'extensions' => [
134+
'persistedQuery' => [
135+
'version' => 1,
136+
'sha256Hash' => $sha256,
137+
],
138+
],
139+
])->assertExactJson([
140+
'data' => [
141+
'foo' => Foo::THE_ANSWER,
142+
],
143+
]);
144+
}
145+
71146
public function testConfigDisabled(): void
72147
{
73148
$config = $this->app->make(ConfigRepository::class);

0 commit comments

Comments
 (0)