I am trying to improve the sql query performance with hotchocolate. For that, I wanted to access the queryrequest generated by hotchololate in another layer of my app. The only way I could find to do this was to intercept the request, store the information I need withing HttpContext items, and then, inject the IHttpContextAccessor wherever I needed it.
services.AddQueryRequestInterceptor(GraphQLRequestInterceptor);
...
private Task GraphQLRequestInterceptor(HttpContext context, IQueryRequestBuilder requestBuilder, CancellationToken cancellationToken)
{
IReadOnlyQueryRequest request = requestBuilder.Create();
context.Items.Add("graph", request);
}
And then recover it by injecting IHttpContextAccessor
public ClientesQueries(Microsoft.AspNetCore.Http.IHttpContextAccessor contextAccessor)
{
var queryRequest = contextAccessor.HttpContext.Items["graph"] as IReadOnlyQueryRequest;
}
With that code, I can create an expression to query my database for only the data that was requested by the client.
Is there a better way to achive this?