Skip to content
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

Feature: cache validation rules #2603

Merged
merged 25 commits into from
Sep 20, 2024
Merged

Feature: cache validation rules #2603

merged 25 commits into from
Sep 20, 2024

Conversation

k0ka
Copy link
Collaborator

@k0ka k0ka commented Aug 22, 2024

resolves #2018

  • Added or updated tests
  • Documented user facing changes
  • Updated CHANGELOG.md

Changes

Added possibility to cache query validation successful result. So the 2nd time the query would appear, most validator won't run against it.

Notes:

  • added hash to \Nuwave\Lighthouse\Schema\AST\DocumentAST
  • created interface ProvidesCacheableValidationRules which extends ProvidesValidationRules for backward compatibility. In the next release we might remove ProvidesValidationRules.
  • some execution paths doesn't provide query hash, so they won't use this feature
    • Defer::executeDeferred
    • SubscriptionBroadcaster::broadcast
    • AsyncMutation::handle
  • validation cache is disabled by default for now.
  • it only caches successful results as Errors might fail to serialize.
  • passing hashes with schema and query is quite ugly

    lighthouse/src/GraphQL.php

    Lines 393 to 397 in 0d8ea16

    protected function executeAndCacheValidationRules(
    SchemaType $schema,
    ?string $schemaHash,
    DocumentNode $query,
    ?string $queryHash,
    but these objects are created by webonyx/graphql-php, so there is no easy way to extend them and store hashes inside objects.

I also added different benchmarks. HugeRequestBench creates requests with a lot of simple fragments: 1, 10 and 100 for benchmark1, benchmark10 and benchmark100 respectively. Enabling validation cache improves execution time:

  • benchmark1 from 4.222ms to 2.886ms
  • benchmark10 from 14.984ms to 4.429ms
  • benchmark100 from 189.641ms to 19.783ms

@k0ka k0ka requested a review from spawnia August 26, 2024 18:32
@k0ka
Copy link
Collaborator Author

k0ka commented Sep 19, 2024

Hello @spawnia,

do you have time to check this PR?
It significantly improves speed and is disabled by default. So it won't break backward compatibility and we can gradually test it on real loads.

docs/master/performance/query-caching.md Outdated Show resolved Hide resolved
src/GraphQL.php Outdated Show resolved Hide resolved
src/LighthouseServiceProvider.php Outdated Show resolved Hide resolved
src/Schema/AST/DocumentAST.php Outdated Show resolved Hide resolved
src/Support/Contracts/ProvidesCacheableValidationRules.php Outdated Show resolved Hide resolved
src/Support/Contracts/ProvidesCacheableValidationRules.php Outdated Show resolved Hide resolved
tests/Integration/ValidationCachingTest.php Outdated Show resolved Hide resolved
tests/Integration/ValidationCachingTest.php Show resolved Hide resolved
tests/Integration/ValidationCachingTest.php Outdated Show resolved Hide resolved
tests/Unit/Schema/AST/DocumentASTTest.php Outdated Show resolved Hide resolved
@spawnia spawnia added the performance Fix or optimization of performance label Sep 19, 2024
src/GraphQL.php Show resolved Hide resolved
src/GraphQL.php Show resolved Hide resolved
src/GraphQL.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@spawnia spawnia left a comment

Choose a reason for hiding this comment

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

Nice work @k0ka!

@spawnia spawnia merged commit bebb209 into master Sep 20, 2024
41 checks passed
@spawnia spawnia deleted the feat-cache-validation-rules branch September 20, 2024 10:03
@spawnia
Copy link
Collaborator

spawnia commented Sep 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Fix or optimization of performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query validation caching
2 participants