-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathLighthouseValidationCache.php
More file actions
64 lines (52 loc) · 1.85 KB
/
LighthouseValidationCache.php
File metadata and controls
64 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types=1);
namespace Nuwave\Lighthouse\Execution;
use Composer\InstalledVersions;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Type\Schema;
use GraphQL\Validator\ValidationCache;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
class LighthouseValidationCache implements ValidationCache
{
/** @var array<string> */
protected const RELEVANT_PACKAGES = [
'nuwave/lighthouse',
'webonyx/graphql-php',
];
protected string $packagesHash;
public function __construct(
protected CacheRepository $cache,
protected string $schemaHash,
protected string $queryHash,
protected string $rulesConfigHash,
protected int $ttl,
) {}
public function isValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): bool
{
return $this->cache->has($this->cacheKey());
}
public function markValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): void
{
$this->cache->put($this->cacheKey(), true, $this->ttl);
}
protected function cacheKey(): string
{
return "lighthouse:validation:{$this->schemaHash}:{$this->queryHash}:{$this->rulesConfigHash}:{$this->packagesHash()}";
}
protected function packagesHash(): string
{
return $this->packagesHash ??= $this->buildPackagesHash();
}
protected function buildPackagesHash(): string
{
$versions = [];
foreach (self::RELEVANT_PACKAGES as $package) {
$versions[$package] = $this->requireVersion($package);
}
return hash('sha256', \Safe\json_encode($versions));
}
protected function requireVersion(string $package): string
{
return InstalledVersions::getVersion($package)
?? throw new \RuntimeException("Could not determine version of {$package} package.");
}
}