Skip to content

Commit 51d1087

Browse files
committed
documentation
1 parent c2a82a4 commit 51d1087

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Validator/ValidationCache.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,38 @@
33
namespace GraphQL\Validator;
44

55
use GraphQL\Language\AST\DocumentNode;
6+
use GraphQL\Tests\PsrValidationCacheAdapter;
67
use GraphQL\Type\Schema;
78

89
/**
9-
* Implement this interface and pass an instance to GraphQL::executeQuery to cache validation of ASTs.
10-
* The details of how to compute any keys (or whether to validate at all) are left up to you.
10+
* Implement this interface and pass an instance to GraphQL::executeQuery to enable caching of successful query validations.
11+
*
12+
* This can improve performance by skipping validation for known-good query and schema combinations.
13+
* You are responsible for defining how cache keys are computed, and when validation should be skipped.
14+
*
15+
* @see PsrValidationCacheAdapter for a toy implementation.
1116
*/
1217
interface ValidationCache
1318
{
1419
/**
15-
* Return true if the given schema + AST pair has previously been validated successfully.
16-
* Only successful validations are cached.
17-
* A return value of false means the pair is either unknown or has not been validated yet.
20+
* Determine whether the given schema + AST pair has already been successfully validated.
21+
*
22+
* This method should return true if the query has previously passed validation for the provided schema.
23+
* Only successful validations should be considered "cached" — failed validations are not cached.
24+
*
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.
28+
*
29+
* @return bool True if validation for the given schema + AST is already known to be valid; false otherwise.
1830
*/
1931
public function isValidated(Schema $schema, DocumentNode $ast): bool;
2032

2133
/**
22-
* Cache validation status for this schema/query.
34+
* Mark the given schema + AST pair as successfully validated.
35+
*
36+
* This is typically called after a query passes validation.
37+
* You should store enough information to recognize this combination on future requests.
2338
*/
2439
public function markValidated(Schema $schema, DocumentNode $ast): void;
2540
}

tests/PsrValidationCacheAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public function markValidated(Schema $schema, DocumentNode $ast): void
5959
*/
6060
private function buildKey(Schema $schema, DocumentNode $ast): string
6161
{
62-
// NOTE: You can override this strategy if you want to make schema fingerprinting cheaper
62+
/**
63+
* NOTE: This default strategy generates a cache key by hashing the printed schema and AST.
64+
* You'll likely want to replace this with a more stable or efficient method for fingerprinting
65+
* the schema -- for example, a build-time hash, schema version number, or an environment-based identifier.
66+
*/
6367
$schemaHash = md5(SchemaPrinter::doPrint($schema));
6468
$astHash = md5($ast->__toString());
6569

0 commit comments

Comments
 (0)