【发布时间】:2022-01-22 03:57:50
【问题描述】:
我似乎无法让使用 HotChocolate 的 Projections 为 GraphQl 工作。根据文档,Projections 应该防止从数据库中过度请求数据,并帮助连接相关表中的数据。作为一个简单的例子,我设置了以下内容:
public class Name
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Queries
{
[UseProjection]
[UseDbContext(typeof(DbAccess))]
public IQueryable<Name> GetNames([ScopedService] DbAccess db)
{
return db.Names;
}
}
public class NameType : ObjectType<Name>
{ }
在 Startup.ConfigureServices 中:
services.AddGraphQLServer()
.AddType<NameType>()
.AddQueryType<Queries>()
.AddProjections();
所以有了这个设置,我运行了一个 Graphql 查询,例如: {names{firstName}}
我希望生成的 sql 类似于
SELECT `n`.`FirstName` FROM `Names` AS `n`
虽然是这样
SELECT `n`.`Id`, `n`.`FirstName`, `n`.`LastName` FROM `Names` AS `n`
我有什么明显的遗漏吗?
编辑版本:
NetCore 5.0
EfCore 5.0.12
HotChocolate 11.0.7
Pomelo.EntityFrameworkCore.MySql 5.0.3
【问题讨论】:
-
这就是它的工作原理。您使用的是哪个版本的 EFCore、.NET 和 HotChocolate?
-
哈哈好吧,我很高兴我不只是笨:P 我已经为所有正在使用的库添加了版本
-
@MichaelIngmarStaib ^
标签: c# sql graphql hotchocolate