Skip to content

Commit 4b3aafe

Browse files
committed
include rules in the fun
1 parent d166e41 commit 4b3aafe

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/Validator/DocumentValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static function validate(
103103
?ValidationCache $cache = null
104104
): array {
105105
if (isset($cache)) {
106-
$cached = $cache->isValidated($schema, $ast);
106+
$cached = $cache->isValidated($schema, $ast, $rules);
107107
if ($cached) {
108108
return [];
109109
}

src/Validator/ValidationCache.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GraphQL\Language\AST\DocumentNode;
66
use GraphQL\Tests\PsrValidationCacheAdapter;
77
use GraphQL\Type\Schema;
8+
use GraphQL\Validator\Rules\ValidationRule;
89

910
/**
1011
* Implement this interface and pass an instance to GraphQL::executeQuery to enable caching of successful query validations.
@@ -17,23 +18,27 @@
1718
interface ValidationCache
1819
{
1920
/**
20-
* Determine whether the given schema + AST pair has already been successfully validated.
21+
* Determine whether the given schema/AST/rules set has already been successfully validated.
2122
*
2223
* This method should return true if the query has previously passed validation for the provided schema.
2324
* Only successful validations should be considered "cached" — failed validations are not cached.
2425
*
2526
* This allows for optimizations in systems where validation may not be necessary on every request.
2627
* For example, you can always return true for persisted queries that are known to be valid ahead of time.
2728
*
28-
* @return bool true if validation for the given schema + AST is already known to be valid; false otherwise
29+
* @param array<ValidationRule>|null $rules
30+
*
31+
* @return bool true if validation for the given schema + AST + rules is already known to be valid; false otherwise
2932
*/
30-
public function isValidated(Schema $schema, DocumentNode $ast): bool;
33+
public function isValidated(Schema $schema, DocumentNode $ast, array $rules = null): bool;
3134

3235
/**
36+
* @param array<ValidationRule>|null $rules
37+
*
3338
* Mark the given schema + AST pair as successfully validated.
3439
*
3540
* This is typically called after a query passes validation.
3641
* You should store enough information to recognize this combination on future requests.
3742
*/
38-
public function markValidated(Schema $schema, DocumentNode $ast): void;
43+
public function markValidated(Schema $schema, DocumentNode $ast, array $rules = null): void;
3944
}

tests/Executor/TestClasses/SpyValidationCacheAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ final class SpyValidationCacheAdapter extends PsrValidationCacheAdapter
1212

1313
public int $markValidatedCalls = 0;
1414

15-
public function isValidated(Schema $schema, DocumentNode $ast): bool
15+
public function isValidated(Schema $schema, DocumentNode $ast, array $rules = null): bool
1616
{
1717
++$this->isValidatedCalls;
1818

1919
return parent::isValidated($schema, $ast);
2020
}
2121

22-
public function markValidated(Schema $schema, DocumentNode $ast): void
22+
public function markValidated(Schema $schema, DocumentNode $ast, array $rules = null): void
2323
{
2424
++$this->markValidatedCalls;
2525

tests/PsrValidationCacheAdapter.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use GraphQL\Language\AST\DocumentNode;
99
use GraphQL\Type\Schema;
1010
use GraphQL\Utils\SchemaPrinter;
11+
use GraphQL\Validator\Rules\ValidationRule;
1112
use GraphQL\Validator\ValidationCache;
1213
use Psr\SimpleCache\CacheInterface;
1314
use Psr\SimpleCache\InvalidArgumentException;
@@ -27,40 +28,46 @@ public function __construct(
2728
}
2829

2930
/**
31+
* @param array<ValidationRule>|null $rules
32+
*
3033
* @throws \JsonException
3134
* @throws Error
3235
* @throws InvalidArgumentException&\Throwable
3336
* @throws InvariantViolation
3437
* @throws SerializationError
3538
*/
36-
public function isValidated(Schema $schema, DocumentNode $ast): bool
39+
public function isValidated(Schema $schema, DocumentNode $ast, array $rules = null): bool
3740
{
3841
$key = $this->buildKey($schema, $ast);
3942

4043
return $this->cache->has($key); // @phpstan-ignore missingType.checkedException (annotated as a union with Throwable)
4144
}
4245

4346
/**
47+
* @param array<ValidationRule>|null $rules
48+
*
4449
* @throws \JsonException
4550
* @throws Error
4651
* @throws InvalidArgumentException&\Throwable
4752
* @throws InvariantViolation
4853
* @throws SerializationError
4954
*/
50-
public function markValidated(Schema $schema, DocumentNode $ast): void
55+
public function markValidated(Schema $schema, DocumentNode $ast, array $rules = null): void
5156
{
5257
$key = $this->buildKey($schema, $ast);
5358

5459
$this->cache->set($key, true, $this->ttlSeconds); // @phpstan-ignore missingType.checkedException (annotated as a union with Throwable)
5560
}
5661

5762
/**
63+
* @param array<ValidationRule>|null $rules
64+
*
5865
* @throws \JsonException
5966
* @throws Error
6067
* @throws InvariantViolation
6168
* @throws SerializationError
6269
*/
63-
private function buildKey(Schema $schema, DocumentNode $ast): string
70+
private function buildKey(Schema $schema, DocumentNode $ast, array $rules = null): string
6471
{
6572
/**
6673
* This default strategy generates a cache key by hashing the printed schema and AST.
@@ -69,7 +76,8 @@ private function buildKey(Schema $schema, DocumentNode $ast): string
6976
*/
7077
$schemaHash = md5(SchemaPrinter::doPrint($schema));
7178
$astHash = md5(serialize($ast));
79+
$rulesHash = md5(serialize($rules));
7280

73-
return "graphql_validation_{$schemaHash}_{$astHash}";
81+
return "graphql_validation_{$schemaHash}_{$astHash}_$rulesHash";
7482
}
7583
}

0 commit comments

Comments
 (0)