title: Aspnetcore Midlleware not reversed in .net6 when using Graphql
body: <p>In versions before .net6, I used to add an <code>ErrorHandlerMiddleware</code> to the pipeline, so I can centralize the need of returning error types in the application with different status codes.
  an example is something exactly like this:</p>
  <pre><code>public class ErrorHandlerMiddleware
  {
      private readonly RequestDelegate _next;
  
      public ErrorHandlerMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          try
          {
              await _next(context);
          }
          catch (Exception error)
          {
              string responseMessage = error.Message;
              string exceptionMessage;
              object ResponseObject;
              var response = context.Response;
              response.ContentType = &quot;application/json&quot;;
              switch (error)
              {
                  case ApplicationBadRequestException e:
                      if (e.metaData is { })
                          ResponseObject = new { responseMessage, e.metaData };
                      else ResponseObject = new { responseMessage };
                      response.StatusCode = StatusCodes.Status400BadRequest;
                      break;
                  case ApplicationNotFoundException e:
                      if (e.metaData is { })
                          ResponseObject = new { responseMessage, e.metaData };
                      else ResponseObject = new { responseMessage };
                      response.StatusCode = StatusCodes.Status404NotFound;
                      break;
                  case ApplicationUnAuthorizedException e:
                      responseMessage = &quot;You are not authorized to perform this operation.&quot;;
                      ResponseObject = new { responseMessage };
                      response.StatusCode = StatusCodes.Status401Unauthorized;
                      break;
                  default:
                      responseMessage = &quot;An error has occurred...Please try again later.&quot;;
                      exceptionMessage = getExMessage(error);
                      ResponseObject = new { responseMessage, exceptionMessage };
                      response.StatusCode = (int)HttpStatusCode.InternalServerError;
                      break;
              }
  
              var result = JsonSerializer.Serialize(ResponseObject);
              await response.WriteAsync(result);
          }
      }
  }
  </code></pre>
  <p>Then at any controller in my application, when I need to return a certain error code in the response - for example 400 BadRequest - I do it like so</p>
  <pre><code>throw new ApplicationBadRequestException(&quot;this is the error message&quot;);
  </code></pre>
  <p>This causes the middleware pipeline to be reversed and this exception is fetched inside the <code>ErrorhandlerMiddleware</code>.</p>
  <p>Now I am trying to use GraphQL in.net6, when I make this I can see that the Middleware pipeline is NOT reversed and the <code>ErrorHandlerMiddleware</code> class is not invoked. why does this happen?
  EDIT: I have tried the same scenario with rest apis and the code works perfectly, seems that the problem happens due to the existence of <code>GraphQL</code> configuration.</p>
  <p>this is my Program.cs file</p>
  <pre><code>var builder = WebApplication.CreateBuilder(args);
  
  // Add services to the container.
  
  builder.Services.AddControllers();
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddSwaggerGen();
  var connectionString = builder.Configuration[&quot;ConnectionStrings:DBConnectionString&quot;];
  builder.Services.AddDbContextFactory&lt;DBEntities&gt;(options =&gt; options.UseSqlServer(connectionString, x =&gt; x.UseNetTopologySuite()));
  builder.Services.AddScoped&lt;DBEntities&gt;(options =&gt; options.GetRequiredService&lt;IDbContextFactory&lt;DBEntities&gt;&gt;().CreateDbContext());
  builder.Services.AddGraphQLServer().AddQueryType&lt;Query&gt;().AddMutationType&lt;Mutation&gt;()
      .AddProjections().AddFiltering().AddSorting();
  var app = builder.Build();
  
  // Configure the HTTP request pipeline.
  if (app.Environment.IsDevelopment())
  {
      app.UseSwagger();
      app.UseSwaggerUI();
  }
  
  app.UseHttpsRedirection();
  app.UseStaticFiles();
  
  app.UseRouting();
  app.UseAuthorization();
  app.UseMiddleware&lt;ErrorHandlerMiddleware&gt;();
  
  app.MapGraphQL(&quot;/graphql&quot;);
  
  app.Run();
  </code></pre>