-
-
Notifications
You must be signed in to change notification settings - Fork 568
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
base: master
Are you sure you want to change the base?
Conversation
src/Validator/ValidationCache.php
Outdated
* 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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. |
src/Validator/ValidationCache.php
Outdated
* 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
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.
++$this->markValidatedCalls; | ||
parent::markValidated($schema, $ast); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++$this->markValidatedCalls; | |
parent::markValidated($schema, $ast); | |
++$this->markValidatedCalls; | |
parent::markValidated($schema, $ast); |
$petType = new InterfaceType([ | ||
'name' => 'Pet', | ||
'fields' => [ | ||
'name' => ['type' => Type::string()], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'name' => ['type' => Type::string()], | |
'name' => Type::string(), |
Same in the rest of the file
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(); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class PsrValidationCacheAdapter implements ValidationCache | |
final class PsrValidationCacheAdapter implements ValidationCache |
There was a problem hiding this comment.
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
tests/PsrValidationCacheAdapter.php
Outdated
{ | ||
try { | ||
$key = $this->buildKey($schema, $ast); | ||
/** @phpstan-ignore-next-line */ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this solution!
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.