|
2 | 2 |
|
3 | 3 | namespace GraphQL\Tests; |
4 | 4 |
|
| 5 | +use GraphQL\Error\Error; |
| 6 | +use GraphQL\Error\InvariantViolation; |
| 7 | +use GraphQL\Error\SerializationError; |
5 | 8 | use GraphQL\Language\AST\DocumentNode; |
6 | 9 | use GraphQL\Type\Schema; |
7 | 10 | use GraphQL\Utils\SchemaPrinter; |
8 | 11 | use GraphQL\Validator\ValidationCache; |
9 | 12 | use Psr\SimpleCache\CacheInterface; |
10 | | -use Symfony\Component\String\Exception\InvalidArgumentException; |
| 13 | +use Psr\SimpleCache\InvalidArgumentException; |
11 | 14 |
|
12 | 15 | class PsrValidationCacheAdapter implements ValidationCache |
13 | 16 | { |
14 | | - private const KEY_PREFIX = 'gql_validation_'; |
15 | | - |
16 | | - private int $ttl; |
17 | | - |
18 | 17 | private CacheInterface $cache; |
19 | 18 |
|
| 19 | + private int $ttlSeconds; |
| 20 | + |
20 | 21 | public function __construct( |
21 | 22 | CacheInterface $cache, |
22 | 23 | int $ttlSeconds = 300 |
23 | 24 | ) { |
24 | | - $this->ttl = $ttlSeconds; |
25 | 25 | $this->cache = $cache; |
| 26 | + $this->ttlSeconds = $ttlSeconds; |
26 | 27 | } |
27 | 28 |
|
28 | | - /** @throws InvalidArgumentException */ |
| 29 | + /** |
| 30 | + * @throws \JsonException |
| 31 | + * @throws Error |
| 32 | + * @throws InvalidArgumentException&\Throwable |
| 33 | + * @throws InvariantViolation |
| 34 | + * @throws SerializationError |
| 35 | + */ |
29 | 36 | public function isValidated(Schema $schema, DocumentNode $ast): bool |
30 | 37 | { |
31 | | - try { |
32 | | - $key = $this->buildKey($schema, $ast); |
| 38 | + $key = $this->buildKey($schema, $ast); |
33 | 39 |
|
34 | | - /** @phpstan-ignore missingType.checkedException */ |
35 | | - return $this->cache->has($key); |
36 | | - } catch (\Throwable $e) { |
37 | | - return false; |
38 | | - } |
| 40 | + return $this->cache->has($key); // @phpstan-ignore missingType.checkedException (annotated as a union with Throwable) |
39 | 41 | } |
40 | 42 |
|
41 | | - /** @throws InvalidArgumentException */ |
| 43 | + /** |
| 44 | + * @throws \JsonException |
| 45 | + * @throws Error |
| 46 | + * @throws InvalidArgumentException&\Throwable |
| 47 | + * @throws InvariantViolation |
| 48 | + * @throws SerializationError |
| 49 | + */ |
42 | 50 | public function markValidated(Schema $schema, DocumentNode $ast): void |
43 | 51 | { |
44 | | - try { |
45 | | - $key = $this->buildKey($schema, $ast); |
46 | | - /** @phpstan-ignore missingType.checkedException */ |
47 | | - $this->cache->set($key, true, $this->ttl); |
48 | | - } catch (\Throwable $e) { |
49 | | - // ignore silently |
50 | | - } |
| 52 | + $key = $this->buildKey($schema, $ast); |
| 53 | + |
| 54 | + $this->cache->set($key, true, $this->ttlSeconds); // @phpstan-ignore missingType.checkedException (annotated as a union with Throwable) |
51 | 55 | } |
52 | 56 |
|
53 | 57 | /** |
54 | | - * @throws \GraphQL\Error\Error |
55 | | - * @throws \GraphQL\Error\InvariantViolation |
56 | | - * @throws \GraphQL\Error\SerializationError |
57 | 58 | * @throws \JsonException |
| 59 | + * @throws Error |
| 60 | + * @throws InvariantViolation |
| 61 | + * @throws SerializationError |
58 | 62 | */ |
59 | 63 | private function buildKey(Schema $schema, DocumentNode $ast): string |
60 | 64 | { |
61 | 65 | /** |
62 | | - * NOTE: This default strategy generates a cache key by hashing the printed schema and AST. |
63 | | - * You'll likely want to replace this with a more stable or efficient method for fingerprinting |
64 | | - * the schema -- for example, a build-time hash, schema version number, or an environment-based identifier. |
| 66 | + * This default strategy generates a cache key by hashing the printed schema and AST. |
| 67 | + * You'll likely want to replace this with a more stable or efficient method for fingerprinting the schema. |
| 68 | + * For example, you may use a build-time hash, schema version number, or an environment-based identifier. |
65 | 69 | */ |
66 | 70 | $schemaHash = md5(SchemaPrinter::doPrint($schema)); |
67 | 71 | $astHash = md5(serialize($ast)); |
68 | 72 |
|
69 | | - return self::KEY_PREFIX . $schemaHash . '_' . $astHash; |
| 73 | + return "graphql_validation_{$schemaHash}_{$astHash}"; |
70 | 74 | } |
71 | 75 | } |
0 commit comments