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

Request Middlewawre: Operation is not valid due to the current state of the object. #7976

Closed
khode-hub opened this issue Feb 2, 2025 · 3 comments

Comments

@khode-hub
Copy link

khode-hub commented Feb 2, 2025

Product

Hot Chocolate

Version

14.3.0

Link to minimal reproduction

https://github.com/khode-hub/psychic-chainsaw

Steps to reproduce

The application has minimal code to reproduce the issue. It has Metrics Request Middleware which is registered in the pipeline:

using HotChocolate.Execution;

namespace MiddlewareTest.Middleware
{
    public class MetricsMiddleware
    {
        private readonly HotChocolate.Execution.RequestDelegate _next;

        public MetricsMiddleware(HotChocolate.Execution.RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(IRequestContext context)
        {
            // Do something
            await _next(context);
            // Do something
        }
    }
}
builder
    .AddGraphQL()
    .AddTypes()
    .UseRequest<MetricsMiddleware>();

Build and run the application. In Nitro, build a query and run.

What is expected?

It is expected to return the response.

What is actually happening?

It is returning an error message:

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "extensions": {
        "message": "Operation is not valid due to the current state of the object.",
        "stackTrace": "   at HotChocolate.Execution.RequestExecutor.ExecuteAsync(IOperationRequest request, Boolean scopeDataLoader, Nullable`1 requestIndex, CancellationToken cancellationToken)\r\n   at HotChocolate.Execution.RequestExecutor.ExecuteAsync(IOperationRequest request, Boolean scopeDataLoader, Nullable`1 requestIndex, CancellationToken cancellationToken)\r\n   at HotChocolate.AspNetCore.MiddlewareBase.ExecuteSingleAsync(HttpContext context, IRequestExecutor requestExecutor, IHttpRequestInterceptor requestInterceptor, IServerDiagnosticEvents diagnosticEvents, GraphQLRequest request, GraphQLRequestFlags flags)\r\n   at HotChocolate.AspNetCore.HttpPostMiddlewareBase.HandleRequestAsync(HttpContext context)"
      }
    }
  ]
}

Relevant log output

at HotChocolate.Execution.RequestExecutor.ExecuteAsync(IOperationRequest request, Boolean scopeDataLoader, Nullable`1 requestIndex, CancellationToken cancellationToken)\r\n   at HotChocolate.Execution.RequestExecutor.ExecuteAsync(IOperationRequest request, Boolean scopeDataLoader, Nullable`1 requestIndex, CancellationToken cancellationToken)\r\n   at HotChocolate.AspNetCore.MiddlewareBase.ExecuteSingleAsync(HttpContext context, IRequestExecutor requestExecutor, IHttpRequestInterceptor requestInterceptor, IServerDiagnosticEvents diagnosticEvents, GraphQLRequest request, GraphQLRequestFlags flags)\r\n   at HotChocolate.AspNetCore.HttpPostMiddlewareBase.HandleRequestAsync(HttpContext context)

Additional context

I have created a Middleware that I want to run in the request pipeline. When I remove the Request Middleware, the application runs as expected. When I add it back, it results in an error.

@tobias-tengler
Copy link
Collaborator

tobias-tengler commented Feb 2, 2025

If you modify the request middleware you also have to register the default parts explicitly, i.e.

builder
    .AddGraphQL()
    .AddTypes()
    .UseRequest<MetricsMiddleware>()
    .UseDefaultPipeline();

@khode-hub
Copy link
Author

khode-hub commented Feb 5, 2025

Thanks, adding default parts of the pipeline has worked.

@lghinet
Copy link

lghinet commented Feb 15, 2025

hello,
using UseDefaultPipeline(), breaks the cost analyzer , because the cost analyzer middleware is not part of the default pipeline
using
.UseDefaultPipeline() .UseCostAnalyzer()

looks it works, but UseCostAnalyzer replaces the pipeline again .. can some point me to some documentation or explain how it should work ?

thnaks

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

3 participants