How can I access ContextData in my middleware? #5895
-
I am using HC 12 and enabled the operation complexity feature like this: .ModifyRequestOptions(o =>
{
// See https://chillicream.com/docs/hotchocolate/v12/security/operation-complexity
o.Complexity.Enable = true;
o.Complexity.DefaultComplexity = 1;
o.Complexity.DefaultResolverComplexity = 5;
o.Complexity.ApplyDefaults = true;
o.Complexity.ContextDataKey = "QueryComplexity";
}) This works nicely and will add an entry I also have a middleware that tracks requests. A simplified version looks like this: public RequestTrackerMiddleware(RequestDelegate next, ITransmitter<ApiTrackerEvent> eventDataTransmitter)
{
_next = next;
_eventDataTransmitter = eventDataTransmitter;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
await ExtractValuesAsync(context);
}
finally
{
context.Request.Body.Seek(0, SeekOrigin.Begin);
await _next(context);
}
} It extracts some values from the HttpContext and sends them somewhere else for storing. Now the million dollar question: How can I access the value that is stored in the What I tried so far:
Due to time constraints I can't update to HC13 now, so an idea that would work on HC12 would be much appreciated. Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@sgabler I guess i would just use a request middleware. new ServiceCollection()
.AddGraphQLServer()|
....
.UseRequest< RequestTrackerMiddleware>()
.UseDefaultPipeline() public RequestTrackerMiddleware(RequestDelegate next, ITransmitter<ApiTrackerEvent> eventDataTransmitter)
{
_next = next;
_eventDataTransmitter = eventDataTransmitter;
}
public async ValueTask InvokeAsync(IRequestContext context)
{
// We execute the operation first. If you need you opteration to first execute you need to register the middlewars manually
await _next(context);
context.Request.Body.Seek(0, SeekOrigin.Begin);
await ExtractValuesAsync(context);
} We execute the operation first. If you need you opteration to first execute you need to register the middlewars manually (like in https://github.com/ChilliCream/graphql-platform/blob/main-version-12/src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.UseRequest.cs#L184-L206) **** |
Beta Was this translation helpful? Give feedback.
@sgabler I guess i would just use a request middleware.