Skip to content

Commit de245ff

Browse files
committed
Polish tests and docs
1 parent 8d21f38 commit de245ff

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

src/Validator/ValidationCache.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ interface ValidationCache
2222
* This method should return true if the query has previously passed validation for the provided schema.
2323
* Only successful validations should be considered "cached" — failed validations are not cached.
2424
*
25-
* Note: This allows for optimizations in systems where validation may not be necessary on every request —
26-
* for example, when using persisted queries that are known to be valid ahead of time. In such cases, you
27-
* can implement this method to always return true.
25+
* This allows for optimizations in systems where validation may not be necessary on every request.
26+
* For example, you can always return true for persisted queries that are known to be valid ahead of time.
2827
*
2928
* @return bool true if validation for the given schema + AST is already known to be valid; false otherwise
3029
*/

tests/Executor/ValidationWithCacheTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public function testIsValidationCachedWithAdapter(): void
8383
$resultB = GraphQL::executeQuery($schema, $query, null, null, null, null, null, null, $cache)->toArray();
8484

8585
// ✅ Assert that validation only happened once
86-
self::assertEquals(2, $cache->isValidatedCalls, 'Should check cache twice');
87-
self::assertEquals(1, $cache->markValidatedCalls, 'Should mark as validated once');
86+
self::assertSame(2, $cache->isValidatedCalls, 'Should check cache twice');
87+
self::assertSame(1, $cache->markValidatedCalls, 'Should mark as validated once');
8888

8989
$expected = [
9090
'data' => [
@@ -95,7 +95,7 @@ public function testIsValidationCachedWithAdapter(): void
9595
],
9696
];
9797

98-
self::assertEquals($expected, $resultA);
99-
self::assertEquals($expected, $resultB);
98+
self::assertSame($expected, $resultA);
99+
self::assertSame($expected, $resultB);
100100
}
101101
}

tests/PsrValidationCacheAdapter.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,74 @@
22

33
namespace GraphQL\Tests;
44

5+
use GraphQL\Error\Error;
6+
use GraphQL\Error\InvariantViolation;
7+
use GraphQL\Error\SerializationError;
58
use GraphQL\Language\AST\DocumentNode;
69
use GraphQL\Type\Schema;
710
use GraphQL\Utils\SchemaPrinter;
811
use GraphQL\Validator\ValidationCache;
912
use Psr\SimpleCache\CacheInterface;
10-
use Symfony\Component\String\Exception\InvalidArgumentException;
13+
use Psr\SimpleCache\InvalidArgumentException;
1114

1215
class PsrValidationCacheAdapter implements ValidationCache
1316
{
14-
private const KEY_PREFIX = 'gql_validation_';
15-
16-
private int $ttl;
17-
1817
private CacheInterface $cache;
1918

19+
private int $ttlSeconds;
20+
2021
public function __construct(
2122
CacheInterface $cache,
2223
int $ttlSeconds = 300
2324
) {
24-
$this->ttl = $ttlSeconds;
2525
$this->cache = $cache;
26+
$this->ttlSeconds = $ttlSeconds;
2627
}
2728

28-
/** @throws InvalidArgumentException */
29+
/**
30+
* @throws \JsonException
31+
* @throws Error
32+
* @throws InvalidArgumentException&\Throwable
33+
* @throws InvariantViolation
34+
* @throws SerializationError
35+
*/
2936
public function isValidated(Schema $schema, DocumentNode $ast): bool
3037
{
31-
try {
32-
$key = $this->buildKey($schema, $ast);
38+
$key = $this->buildKey($schema, $ast);
3339

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)
3941
}
4042

41-
/** @throws InvalidArgumentException */
43+
/**
44+
* @throws \JsonException
45+
* @throws Error
46+
* @throws InvalidArgumentException&\Throwable
47+
* @throws InvariantViolation
48+
* @throws SerializationError
49+
*/
4250
public function markValidated(Schema $schema, DocumentNode $ast): void
4351
{
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)
5155
}
5256

5357
/**
54-
* @throws \GraphQL\Error\Error
55-
* @throws \GraphQL\Error\InvariantViolation
56-
* @throws \GraphQL\Error\SerializationError
5758
* @throws \JsonException
59+
* @throws Error
60+
* @throws InvariantViolation
61+
* @throws SerializationError
5862
*/
5963
private function buildKey(Schema $schema, DocumentNode $ast): string
6064
{
6165
/**
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.
6569
*/
6670
$schemaHash = md5(SchemaPrinter::doPrint($schema));
6771
$astHash = md5(serialize($ast));
6872

69-
return self::KEY_PREFIX . $schemaHash . '_' . $astHash;
73+
return "graphql_validation_{$schemaHash}_{$astHash}";
7074
}
7175
}

0 commit comments

Comments
 (0)