繁体   English   中英

使用投影而不返回 IQueryable,GraphQL HotChocolate

[英]Using projections without returning IQueryable, GraphQL HotChocolate

如果我不希望我的服务返回 IQueryable,有没有办法使用预测?

我正在考虑类似的事情,我的解析器可以将 map 输入到表达式,然后调用服务 class,然后根据提供的表达式构建和执行查询,转换为一些只读集合并返回给用户。

类似于以下内容:

 public async Task<IReadOnlyList<TestPayload>> GetAllTests(
                                             [Service] ITestService, 
                                             CancellationToken cancellationToken, 
                                             object[] requestedFields) => 
            await service.GetAllAsync(requestedFields, cancellationToken);

然后在我的服务中:

var expression = BuildExpression(requestedFields);

var output = testRepository.Queryable()
                                 .Select(expression)
                                    .AsNoTracking();
return output.ToListAsync();

你能给我指出一些关于这个的方向吗?


编辑:

根据@Pascal 的回答,我添加了 HotChocolate.Data package 并在我的 IQueryable 上调用了 Project(context) 扩展方法。

现在我的有效载荷(模型)类有问题,但我不太清楚为什么。

以下示例将返回所有列并且不会应用投影。

       [UseProjection]
       public async Task<IReadOnlyList<TestPayload>> TestGetAll([Service] ITestService testService, IResolverContext context, CancellationToken cancellationToken) 
        {
            return await testService.GetAllAsync(context, cancellationToken);
        }

这将起作用,并将投射到查询并仅返回所请求的内容。 请注意,唯一不同的是返回类型(IReadOnly 而不是 IReadOnly)。

       [UseProjection]
       public async Task<IReadOnlyList<Test>> TestGetAll([Service] ITestService testService, IResolverContext context, CancellationToken cancellationToken) 
        {
            return await testService.GetAllAsync(context, cancellationToken);
        }

服务内部:

var tests = testRepository.Queryable()
                         .Project<Test>(context)
                        .AsNoTracking();

我猜这是因为解析器现在正在使用 dto TestPayload 类型,然后在服务内部尝试投影到实体(测试)的 IQueryable,但我不确定如何克服它。

是的,HotChocolate.Data 在 IQueryable 之上提供了扩展方法

 public async Task<IReadOnlyList<TestPayload>> GetAllTests(
    [Service] ITestService, 
    CancellationToken cancellationToken, 
    IResolverContext context) => 
    await service.GetAllAsync(context, cancellationToken);
var output = testRepository.Queryable()
     .Project(context)
     .AsNoTracking();
return output.ToListAsync();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM