-
-
Notifications
You must be signed in to change notification settings - Fork 570
Cache result of validation #1730
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: master
Are you sure you want to change the base?
Conversation
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 think we are good with the implementation in its current form. Can you add documentation to docs/executing-queries.md
? I think a section after Custom Validation Rules
would work fine.
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.
There are some tricky details to figure out when implementing this. I really value putting in the time now to properly document them now to validate the design. I am going to try implenting the validation cache in https://github.com/nuwave/lighthouse and use that to battle-test it in one of my projects to see if anything else comes up.
docs/executing-queries.md
Outdated
use GraphQL\GraphQL; | ||
use GraphQL\Tests\PsrValidationCacheAdapter; | ||
|
||
$validationCache = new 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.
Do you mean to expose this class to end users? The directory /tests
and subsequently the namespace GraphQL\Tests
is excluded from the composer package through .gitattributes
. My impression was that it serves only as a vehicle for our own unit tests and perhaps as an example implementation that we can point to.
I would probably recommend users to implement the interface concretely, perhaps we can just add something like this?
use GraphQL\GraphQL; | |
use GraphQL\Tests\PsrValidationCacheAdapter; | |
$validationCache = new PsrValidationCacheAdapter(); | |
use GraphQL\GraphQL; | |
final class MyValidationCache implements ValidationCache { ... } | |
$validationCache = new MyValidationCache(); |
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, yeah, this is just meant to be a sample. I'll touch that up.
$errors = $context->getErrors(); | ||
|
||
if (isset($cache) && $errors === []) { | ||
$cache->markValidated($schema, $ast, $rules); |
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.
Good catch on not overwriting $rules
👍
src/Validator/ValidationCache.php
Outdated
* and it's possible these may shift or expand as the library evolves, so it might make sense | ||
* to include the library version number in your keys. | ||
* | ||
* @see PsrValidationCacheAdapter for a simple reference implementation. |
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.
Due to /tests
not being exported, this reference will lead nowhere for users that installed this package.
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 figure devs will be savvy enough to either clone the repo or look on github, but I can remove it for now if you like.
I just found that Lighthouse already implements validation caching, see nuwave/lighthouse#2603. In general, the approach to hashing to schema and hashing the query string is the same, but there are some differences:
To resolve 1., perhaps we should add multi-phase validation to this library or some kind of special treatment for I am going to try adding a pull request to Lighthouse soon that implements its validation cache feature using the facilities offered through this pull request and perhaps extend it where its lacking. |
Not sure I follow. I don't use Lighthouse, and I'm not familiar with it. What does it have to do with what I'm doing in this PR? |
Lighthouse is built atop this library. It already implements a validation cache that has proven to be effective. I would like to take the lessons from the implementation effort we made there to inform the implementation in this library. This is to ensure that I can replace the custom implementation in Lighthouse with what we provide here, and we are not missing anything. |
I see. Lighthouse implements its own high-level query flow, and uses I'll leave you to it. |
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.