【问题标题】:Am I misunderstanding Projections in HotChocolate?我是否误解了 HotChocolate 中的投影?
【发布时间】: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


【解决方案1】:

经过反复试验,我发现属性标签的顺序错误应该是:

public class Queries
{
  [UseDbContext(typeof(DbAccess))]
  [UseProjection]
  public IQueryable<Name> GetNames([ScopedService] DbAccess db)
  {
    return db.Names;
  }
}

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2017-05-24
    • 2012-08-29
    • 1970-01-01
    相关资源
    最近更新 更多