You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/executing-queries.md
+98Lines changed: 98 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -210,3 +210,101 @@ $server = new StandardServer([
210
210
'validationRules' => $myValidationRules
211
211
]);
212
212
```
213
+
## Validation Caching
214
+
215
+
Validation is a required step in GraphQL execution, but it can become a performance bottleneck when the same queries are
216
+
run repeatedly — especially in production environments where queries are often static or pre-generated (e.g., persisted
217
+
queries or queries emitted by client libraries).
218
+
219
+
To optimize for this, graphql-php supports pluggable validation caching. By implementing the GraphQL\Validator\ValidationCache
220
+
interface and passing it to GraphQL::executeQuery(), you can skip validation for queries already known to be valid:
221
+
222
+
To optimize for this, `graphql-php` supports pluggable validation caching. By implementing the `GraphQL\Validator\ValidationCache` interface and passing it to
223
+
`GraphQL::executeQuery()`, you can skip validation for queries that are already known to be valid.
224
+
225
+
```php
226
+
use GraphQL\Validator\ValidationCache;
227
+
use GraphQL\GraphQL;
228
+
use GraphQL\Tests\PsrValidationCacheAdapter;
229
+
230
+
$validationCache = new PsrValidationCacheAdapter();
231
+
232
+
$result = GraphQL::executeQuery(
233
+
$schema,
234
+
$queryString,
235
+
$rootValue,
236
+
$context,
237
+
$variableValues,
238
+
$operationName,
239
+
$fieldResolver,
240
+
$validationRules,
241
+
$validationCache
242
+
);
243
+
```
244
+
245
+
### Key Generation Tips
246
+
You are responsible for generating your own cache keys in a way that uniquely identifies the schema, the query, and
247
+
(optionally) any custom validation rules. Here are some tips:
248
+
249
+
* Hash your schema once at build time and store the result in an environment variable or constant.
250
+
* Avoid using serialize() on schema objects — closures and internal references may cause errors.
251
+
* If using custom validation rules, be sure to account for them in your key (e.g., by serializing or listing their class names).
252
+
* Consider including the graphql-php version number to account for internal rule changes across versions.
253
+
254
+
### Sample Implementation
255
+
```php
256
+
use GraphQL\Validator\ValidationCache;
257
+
use GraphQL\Language\AST\DocumentNode;
258
+
use GraphQL\Type\Schema;
259
+
use GraphQL\Utils\SchemaPrinter;
260
+
use Psr\SimpleCache\CacheInterface;
261
+
use Composer\InstalledVersions;
262
+
263
+
/**
264
+
* Reference implementation of ValidationCache using PSR-16 cache.
265
+
*
266
+
* @see GraphQl\Tests\PsrValidationCacheAdapter
267
+
*/
268
+
class PsrValidationCacheAdapter implements ValidationCache
269
+
{
270
+
private CacheInterface $cache;
271
+
272
+
private int $ttlSeconds;
273
+
274
+
public function __construct(
275
+
CacheInterface $cache,
276
+
int $ttlSeconds = 300
277
+
) {
278
+
$this->cache = $cache;
279
+
$this->ttlSeconds = $ttlSeconds;
280
+
}
281
+
282
+
public function isValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): bool
283
+
{
284
+
$key = $this->buildKey($schema, $ast);
285
+
return $this->cache->has($key);
286
+
}
287
+
288
+
public function markValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): void
0 commit comments