【发布时间】:2020-01-09 12:27:17
【问题描述】:
我正在尝试使用 hotchocolate 提高 sql 查询性能。为此,我想在我的应用程序的另一层访问由 hotchololate 生成的查询请求。我能找到的唯一方法是拦截请求,使用 HttpContext 项存储我需要的信息,然后在我需要的地方注入 IHttpContextAccessor。
services.AddQueryRequestInterceptor(GraphQLRequestInterceptor);
...
private Task GraphQLRequestInterceptor(HttpContext context, IQueryRequestBuilder requestBuilder, CancellationToken cancellationToken)
{
IReadOnlyQueryRequest request = requestBuilder.Create();
context.Items.Add("graph", request);
}
然后通过注入IHttpContextAccessor来恢复
public ClientesQueries(Microsoft.AspNetCore.Http.IHttpContextAccessor contextAccessor)
{
var queryRequest = contextAccessor.HttpContext.Items["graph"] as IReadOnlyQueryRequest;
}
使用该代码,我可以创建一个表达式来仅查询我的数据库以获取客户端请求的数据。
有没有更好的方法来实现这一点?
【问题讨论】:
标签: graphql entity-framework-core hotchocolate