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

Allow access to calculated query complexity #1475

Merged
merged 2 commits into from
Nov 14, 2023

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Nov 1, 2023

Early return when complexity is disabled
This allows other validation rules to depend on the QueryComplexity rule, and disable the query complexity completely on some condition.

$rules[QueryComplexity::class] = $queryComplexity = new QueryComplexity($this->maxQueryComplexity);

// Always enable the query complexity rule, but ignore for introspection queries
$rules[WhenIntrospection::class] = new WhenIntrospection(function () use ($queryComplexity) : void {
    $queryComplexity->setMaxQueryComplexity(QueryComplexity::DISABLED);
});

Allow access to calculated query complexity
This allows you to read the calculated query complexity. A use case could be: add a rule that logs when queries are detected with a complexity higher than X.

This helps with #1204

$rules[QueryComplexity::class] = $queryComplexity = new QueryComplexity($this->maxQueryComplexity);

$rules[WhenLeaveOperation::class] = new WhenLeaveOperation(function () use ($queryComplexity) : void {
    if ($queryComplexity->getQueryComplexity() >= 400) {
        $this->logger->info('Found a query with high complexity');
    }
});

ruudk added 2 commits November 1, 2023 13:03
This allows other validation rules to depend on the `QueryComplexity` rule, and disable
the query complexity completely on some condition.
This allows you to read the calculated query complexity.

A use case could be: add a rule that logs when queries are detected with a complexity higher than X.
@ruudk
Copy link
Contributor Author

ruudk commented Nov 14, 2023

@spawnia this one is ready for review, whenever you have time.

@spawnia spawnia merged commit 7a44e4e into webonyx:master Nov 14, 2023
12 of 13 checks passed
@ruudk ruudk deleted the query-complexity branch November 11, 2024 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants