Skip to content

Cache result of validation #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Conversation

shmax
Copy link
Contributor

@shmax shmax commented Jul 11, 2025

Heyo, I remember there was some discussion about possibly disabling document validation checks a while back. I finally got around to looking into it and realized to my horror that it was indeed gobbling up a lot of resources in my app.

I don't know if you folks ever really agreed on a plan, but I thought I'd float this simple caching solution that leaves it up to the user.

Let me know what you think. If you like the general direction I can do a little polish and write better tests.

@shmax shmax changed the title cache result of validation Cache result of validation Jul 11, 2025
@shmax shmax force-pushed the validation-cache branch from 2d38364 to a2c38f4 Compare July 11, 2025 03:55
@shmax shmax force-pushed the validation-cache branch from 9ffbbd7 to 6782bad Compare July 11, 2025 04:07
@shmax shmax force-pushed the validation-cache branch from e3bc588 to 6317931 Compare July 12, 2025 00:05
@shmax shmax force-pushed the validation-cache branch from 1a59209 to 73c9774 Compare July 12, 2025 00:40
@shmax shmax force-pushed the validation-cache branch from 5e59b9c to 6d7ace0 Compare July 12, 2025 00:41
@shmax shmax force-pushed the validation-cache branch from 59f0ea0 to 708c85b Compare July 12, 2025 00:45
@shmax shmax force-pushed the validation-cache branch from a12854e to f35cb3c Compare July 12, 2025 01:53
@shmax shmax force-pushed the validation-cache branch from e6fa9a4 to d71e123 Compare July 12, 2025 04:00
Comment on lines 10 to 11
* Implement this interface and pass an instance to GraphQL::executeQuery to cache validation of ASTs. The details
* of how to compute any keys (or whether to validate at all) are left up to you.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Implement this interface and pass an instance to GraphQL::executeQuery to cache validation of ASTs. The details
* of how to compute any keys (or whether to validate at all) are left up to you.
* Implement this interface and pass an instance to GraphQL::executeQuery to cache validation of ASTs.
* The details of how to compute any keys (or whether to validate at all) are left up to you.

Comment on lines 16 to 17
* Return true if the given schema + AST pair has previously been validated successfully.
* Only successful validations are cached. A return value of false means the pair is either unknown or has not been validated yet.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Return true if the given schema + AST pair has previously been validated successfully.
* Only successful validations are cached. A return value of false means the pair is either unknown or has not been validated yet.
* Return true if the given schema + AST pair has previously been validated successfully.
* Only successful validations are cached.
* A return value of false means the pair is either unknown or has not been validated yet.

use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;

final class SpyValidationCacheAdapter extends PsrValidationCacheAdapter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this into a separate file.

Comment on lines 35 to 36
++$this->markValidatedCalls;
parent::markValidated($schema, $ast);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
++$this->markValidatedCalls;
parent::markValidated($schema, $ast);
++$this->markValidatedCalls;
parent::markValidated($schema, $ast);

$petType = new InterfaceType([
'name' => 'Pet',
'fields' => [
'name' => ['type' => Type::string()],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'name' => ['type' => Type::string()],
'name' => Type::string(),

Same in the rest of the file

Comment on lines 103 to 104
GraphQL::executeQuery($schema, $query, null, null, null, null, null, null, $cache)->toArray();
$result = GraphQL::executeQuery($schema, $query, null, null, null, null, null, null, $cache)->toArray();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's assert the result is the same for both calls

use Psr\SimpleCache\CacheInterface;
use Symfony\Component\String\Exception\InvalidArgumentException;

class PsrValidationCacheAdapter implements ValidationCache
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class PsrValidationCacheAdapter implements ValidationCache
final class PsrValidationCacheAdapter implements ValidationCache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would, but I extend this with SpyValidationCacheAdapter

{
try {
$key = $this->buildKey($schema, $ast);
/** @phpstan-ignore-next-line */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use @phpstan-ignore with an error identifier here? I would like to know what is going on here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this has to do with stan wanting me to enumerate all the possible exceptions that could be thrown in the doc block. I argued with it for about 20 minutes but couldn't seem to satisfy it, so I gave up. If you want me to go at it again I can, but ugh. c2a82a4

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this solution!

@shmax shmax force-pushed the validation-cache branch from d1f3597 to c2a82a4 Compare July 14, 2025 14:26
@shmax shmax marked this pull request as ready for review July 14, 2025 17:32
@shmax shmax force-pushed the validation-cache branch from 0c02ce9 to 51d1087 Compare July 15, 2025 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants