Cache result of validation
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.
I just found that Lighthouse already implements validation caching, see https://github.com/nuwave/lighthouse/pull/2603. In general, the approach to hashing to schema and hashing the query string is the same, but there are some differences:
- Lighthouse offers special consideration for the
QueryComplexityrule, which requires variables to work. Generally, validation rules do not have access to variable values, but this one has special treatment even inwebonyx/graphql-php. The solution in Lighthouse splits validation in two phases, one where the cacheable rules (everything exceptQueryComplexity) are run/cached, thenQueryComplexitymaybe runs afterwards. - Lighthouse has no considerations for the used library version of
webonyx/graphql-phpornuwave/lighthouseitself, but probably should consider that. - Lighthouse does not allow dynamically passing
$rulesad-hoc, but users may overwrite the interfaceProvidesCacheableValidationRulesand change which validation rules are used. However, I found that using just the keys or class names of the used validation rules array is not sufficient. The validation rules for security such asQueryDepthare configurable and behave differently based on the configuration.
To resolve 1., perhaps we should add multi-phase validation to this library or some kind of special treatment for QueryComplexity.
Points 2. and 3. seem like something where Lighthouse is currently lacking but could be improved.
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?
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 DocumentValidator:validate directly. In the meantime, I modified my own code to just disable validation on production (I use persisted queries), so I'm not blocked.
I'll leave you to it.