简体   繁体   English

如何在适用于 EFCore 的 Hotchocolate 中打开和关闭包含

[英]How can you switch includes on and off in Hotchocolate for EFCore

So I'm trying to figure out how to tell EfCore when to apply an include to an IQueryable object based on whether the GraphQl request from the client actually includes the related object.因此,我试图根据来自客户端的 GraphQl 请求是否实际上包含相关的 object,来弄清楚如何告诉 EfCore 何时将包含应用于 IQueryable object。

Example:例子:

  • 2 Classes: Person, Payment 2类:人,付款

  • Theses are kept in 2 different tables and related Person -> Payment in a one to many relationship论文保存在 2 个不同的表和相关人员 -> 一对多关系中的付款

  • I have 1 GraphQl Query set up to retrieve all people in the Database:我设置了 1 个 GraphQl 查询以检索数据库中的所有人:

     [UseFiltering] [UseDbContext(typeof(DbAccess))] public IQueryable<Person> GetPeople([ScopedService] DbAccess db) { return db.People; }
  • This returns just the fields in the People table ex: query: {people{sso,payments{amount, recipient{compayName}}}} response: { "data": { "people": [ { "sso": "000-00-0003", "payments": null } ] } }这仅返回 People 表中的字段 ex:查询:{people{sso,payments{amount, recipient{compayName}}}} 响应:{“data”:{“people”:[{“sso”:“000- 00-0003", "付款": null } ] } }

  • In order to get the payments my GetPeople func needs to be changed to为了获得付款,我的 GetPeople 函数需要更改为

     [UseFiltering] [UseDbContext(typeof(DbAccess))] public IQueryable<Person> GetPeople([ScopedService] DbAccess db) { return db.People.Include(p => p.Payments); }
  • This works but places an unneeded strain when the payments aren't part of the request (as the Include will run every time).这可行,但当付款不是请求的一部分时会造成不必要的压力(因为 Include 每次都会运行)。

What I would like to do would be something like:我想做的是:

    var baseQuery = db.People;
        if (graphqlRequestIncludePayments)
        {
            baseQuery.Include(p => p.Payments);
        }
    return baseQuery;

But I cannot figure out how to check the GraphQl query to see if payments is requested.但我不知道如何检查 GraphQl 查询以查看是否请求付款。 I know GraphQl will remove the excess data before returning to the consumer but that can be a lot of wasted bandwidth and memory on the server side.我知道 GraphQl 会在返回给消费者之前删除多余的数据,但这可能会浪费大量的带宽和服务器端的 memory。

I feel like there is a way to do this in the Hotchocolate ObjectType.Configure function but I cant see how.我觉得在 Hotchocolate ObjectType.Configure function 中有一种方法可以做到这一点,但我看不出怎么做。

Any help would be much appreciated, thanks:)任何帮助将不胜感激,谢谢:)

Tobias Tengler was correct this should be done through Projections Tobias Tengler 是正确的,这应该通过 Projections 来完成

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

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