Skip to content

Allow Opt Out of Cost Analysis on per-request basis #8189

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

Closed
brian-agencyroot opened this issue Mar 27, 2025 · 2 comments
Closed

Allow Opt Out of Cost Analysis on per-request basis #8189

brian-agencyroot opened this issue Mar 27, 2025 · 2 comments

Comments

@brian-agencyroot
Copy link

Product

Hot Chocolate

Is your feature request related to a problem?

We have just upgraded from HotChocolate 13 to 15 and got through all the breaking changes. The one feature we've lost, though, is in our interceptor we are evaluating the HttpHeaders and if the request is coming from one of our internal apps, we were calling:

requestBuilder.SkipComplexityAnalysis();

This is obviously not here anymore and not part of the OperationRequestBuilder. I see how we can opt out globally, but we would like to avoid that as we have APIs open to other teams that aren't ours.

The solution you'd like

Short of having 2 hosted instances of our GraphQL server, one that bypasses cost calculations and another that doesn't, is there (or can there be) some feature that I'm missing that would allow me to have the functionality as we did in version 13 and prior and be able to disable cost analysis on a per-request basis?

@michaelstaib
Copy link
Member

This is already implemented

@michaelstaib
Copy link
Member

michaelstaib commented Mar 27, 2025

With HotChocolate latest you can do:

public class CostInterceptor : DefaultHttpRequestInterceptor
{
    public override ValueTask OnCreateAsync(
        HttpContext context,
        IRequestExecutor requestExecutor,
        OperationRequestBuilder requestBuilder,
        CancellationToken cancellationToken)
    {
        var costOptions = requestExecutor.GetCostOptions();
        requestBuilder.SetCostOptions(costOptions with { MaxTypeCost = 1});
        return base.OnCreateAsync(context, requestExecutor, requestBuilder, cancellationToken);
    }
}

With this you cannot only opt-out but also give users different costs, for instance if you have consumers that paid for your service you could allow them to execute more complex operations and so forth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants