Skip to content

Commit 6317931

Browse files
committed
use custom interface
1 parent 6782bad commit 6317931

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"require": {
1212
"php": "^7.4 || ^8",
1313
"ext-json": "*",
14-
"ext-mbstring": "*",
15-
"psr/simple-cache": "^1.0"
14+
"ext-mbstring": "*"
1615
},
1716
"require-dev": {
1817
"amphp/amp": "^2.6",
@@ -29,6 +28,7 @@
2928
"phpstan/phpstan-strict-rules": "2.0.4",
3029
"phpunit/phpunit": "^9.5 || ^10.5.21 || ^11",
3130
"psr/http-message": "^1 || ^2",
31+
"psr/simple-cache": "^1.0",
3232
"react/http": "^1.6",
3333
"react/promise": "^2.0 || ^3.0",
3434
"rector/rector": "^2.0",

src/GraphQL.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use GraphQL\Validator\DocumentValidator;
2020
use GraphQL\Validator\Rules\QueryComplexity;
2121
use GraphQL\Validator\Rules\ValidationRule;
22-
use Psr\SimpleCache\CacheInterface;
22+
use GraphQL\Validator\ValidationCache;
2323

2424
/**
2525
* This is the primary facade for fulfilling GraphQL operations.
@@ -92,7 +92,7 @@ public static function executeQuery(
9292
?string $operationName = null,
9393
?callable $fieldResolver = null,
9494
?array $validationRules = null,
95-
?CacheInterface $cache = null,
95+
?ValidationCache $cache = null,
9696
): ExecutionResult {
9797
$promiseAdapter = new SyncPromiseAdapter();
9898

@@ -136,7 +136,7 @@ public static function promiseToExecute(
136136
?string $operationName = null,
137137
?callable $fieldResolver = null,
138138
?array $validationRules = null,
139-
?CacheInterface $cache = null
139+
?ValidationCache $cache = null
140140
): Promise {
141141
try {
142142
$documentNode = $source instanceof DocumentNode

src/Validator/DocumentValidator.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
use GraphQL\Validator\Rules\ValuesOfCorrectType;
5050
use GraphQL\Validator\Rules\VariablesAreInputTypes;
5151
use GraphQL\Validator\Rules\VariablesInAllowedPosition;
52-
use Psr\SimpleCache\CacheInterface;
53-
use Psr\SimpleCache\InvalidArgumentException;
5452

5553
/**
5654
* Implements the "Validation" section of the spec.
@@ -102,18 +100,13 @@ public static function validate(
102100
DocumentNode $ast,
103101
?array $rules = null,
104102
?TypeInfo $typeInfo = null,
105-
?CacheInterface $cache = null
103+
?ValidationCache $cache = null
106104
): array {
107-
$cacheKey = null;
108-
109105
if (isset($cache)) {
110-
$cacheKey = 'gql_validation_' . md5($ast->__toString());
111-
112-
try {
113-
if ($cache->has($cacheKey)) {
114-
return $cache->get($cacheKey);
115-
}
116-
} catch (InvalidArgumentException $e) {}
106+
$cached = $cache->isValidated($schema, $ast);
107+
if ($cached) {
108+
return [];
109+
}
117110
}
118111

119112
$rules ??= static::allRules();
@@ -140,11 +133,9 @@ public static function validate(
140133
$errors = $context->getErrors();
141134

142135
// Only cache clean results
143-
try {
144-
if (isset($cacheKey) && count($errors) === 0) {
145-
$cache->set($cacheKey, $errors);
146-
}
147-
} catch (InvalidArgumentException $e) {}
136+
if (isset($cache) && count($errors) === 0) {
137+
$cache->markValidated($schema, $ast);
138+
}
148139

149140
return $errors;
150141
}

src/Validator/ValidationCache.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace GraphQL\Validator;
4+
5+
use GraphQL\GraphQL;
6+
use GraphQL\Language\AST\DocumentNode;
7+
use GraphQL\Type\Schema;
8+
9+
/**
10+
* Implement this interface and pass an instance to GraphQL::executeQuery to cache validation of ASTs. The details
11+
* of how to compute any keys (or whether to validate at all) are left up to you.
12+
*/
13+
interface ValidationCache
14+
{
15+
/**
16+
* Return true if the given schema + AST pair has previously been validated successfully.
17+
* Only successful validations are cached. A return value of false means the pair is either unknown or has not been validated yet.
18+
*/
19+
public function isValidated(Schema $schema, DocumentNode $ast): bool;
20+
21+
/**
22+
* Cache validation status for this schema/query.
23+
*/
24+
public function markValidated(Schema $schema, DocumentNode $ast): void;
25+
}

0 commit comments

Comments
 (0)