Skip to content

Commit 35ce576

Browse files
committed
cache result of validation
1 parent 1a7aac3 commit 35ce576

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"require": {
1212
"php": "^7.4 || ^8",
1313
"ext-json": "*",
14-
"ext-mbstring": "*"
14+
"ext-mbstring": "*",
15+
"psr/simple-cache": "^3.0"
1516
},
1617
"require-dev": {
1718
"amphp/amp": "^2.6",
@@ -31,6 +32,7 @@
3132
"react/http": "^1.6",
3233
"react/promise": "^2.0 || ^3.0",
3334
"rector/rector": "^2.0",
35+
"symfony/cache": "^6.4",
3436
"symfony/polyfill-php81": "^1.23",
3537
"symfony/var-exporter": "^5 || ^6 || ^7",
3638
"thecodingmachine/safe": "^1.3 || ^2 || ^3"

src/GraphQL.php

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

2324
/**
2425
* This is the primary facade for fulfilling GraphQL operations.
@@ -90,7 +91,8 @@ public static function executeQuery(
9091
?array $variableValues = null,
9192
?string $operationName = null,
9293
?callable $fieldResolver = null,
93-
?array $validationRules = null
94+
?array $validationRules = null,
95+
?CacheInterface $cache = null,
9496
): ExecutionResult {
9597
$promiseAdapter = new SyncPromiseAdapter();
9698

@@ -103,7 +105,8 @@ public static function executeQuery(
103105
$variableValues,
104106
$operationName,
105107
$fieldResolver,
106-
$validationRules
108+
$validationRules,
109+
$cache
107110
);
108111

109112
return $promiseAdapter->wait($promise);
@@ -132,7 +135,8 @@ public static function promiseToExecute(
132135
?array $variableValues = null,
133136
?string $operationName = null,
134137
?callable $fieldResolver = null,
135-
?array $validationRules = null
138+
?array $validationRules = null,
139+
?CacheInterface $cache = null
136140
): Promise {
137141
try {
138142
$documentNode = $source instanceof DocumentNode
@@ -152,7 +156,7 @@ public static function promiseToExecute(
152156
}
153157
}
154158

155-
$validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules);
159+
$validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules, null, $cache);
156160

157161
if ($validationErrors !== []) {
158162
return $promiseAdapter->createFulfilled(

src/Validator/DocumentValidator.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use GraphQL\Validator\Rules\ValuesOfCorrectType;
5050
use GraphQL\Validator\Rules\VariablesAreInputTypes;
5151
use GraphQL\Validator\Rules\VariablesInAllowedPosition;
52+
use Psr\SimpleCache\CacheInterface;
5253

5354
/**
5455
* Implements the "Validation" section of the spec.
@@ -99,16 +100,25 @@ public static function validate(
99100
Schema $schema,
100101
DocumentNode $ast,
101102
?array $rules = null,
102-
?TypeInfo $typeInfo = null
103+
?TypeInfo $typeInfo = null,
104+
?CacheInterface $cache = null
103105
): array {
104-
$rules ??= static::allRules();
106+
$cacheKey = null;
107+
108+
if ($cache) {
109+
$cacheKey = 'gql_validation_' . md5($ast->__toString());
105110

111+
if ($cache->has($cacheKey)) {
112+
return $cache->get($cacheKey);
113+
}
114+
}
115+
116+
$rules ??= static::allRules();
106117
if ($rules === []) {
107118
return [];
108119
}
109120

110121
$typeInfo ??= new TypeInfo($schema);
111-
112122
$context = new QueryValidationContext($schema, $ast, $typeInfo);
113123

114124
$visitors = [];
@@ -124,7 +134,14 @@ public static function validate(
124134
)
125135
);
126136

127-
return $context->getErrors();
137+
$errors = $context->getErrors();
138+
139+
// Only cache clean results
140+
if ($cache && $cacheKey && count($errors) === 0) {
141+
$cache->set($cacheKey, $errors, 300); // TTL = 5 min
142+
}
143+
144+
return $errors;
128145
}
129146

130147
/**

0 commit comments

Comments
 (0)