【发布时间】:2021-06-17 00:07:12
【问题描述】:
我们在现有的应用数据库之上有一个 GraphQL dotnet 实现。
<PackageReference Include="GraphQL" Version="3.3.2" />
<PackageReference Include="GraphQL.SystemTextJson" Version="3.3.2" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="4.4.1" />
<PackageReference Include="GraphQL.Server.Transports.WebSockets" Version="4.4.1" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="4.4.1" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="4.4.1" />
<PackageReference Include="GraphQL.Server.Authorization.AspNetCore" Version="4.4.1" />
我们有一个相当复杂的数据结构,所以当从我们的一些顶级字段查询时,包括它们的一些子字段,我们可能会连接大量的表。但是,并非每个查询都需要所有这些连接。
我希望能够手动解析 Query ObjectGraphType 中的上下文。当我解析它以消除未查询特定子字段时的连接时。
一个非常简单的高级版本如下:
Field<ListGraphType<OrganisationType>>(
"organisations",
resolve: context =>
{
var retVal = database.Organisations();
//Are we joining on employers?
if(context.SubFields.ContainsKey("employers"))
{
retVal.Include(x => x.Employers.Where(x => x.Deleted != true))
.ThenInclude(x => x.Departments.Where(x => x.Deleted != true));
}
return retVal;
}
如果用户有疑问,我们只会加入雇主。但是,问题是雇主可以拥有部门、雇员、经理等……而这些本身可以拥有大量的子属性。
目前,我们的查询几乎连接了查询的每一个排列,产生了一个非常庞大的 SQL 查询。如果用户只想要组织名称和每个雇主的名称,这将是一项繁重的工作。
在最顶层过滤很容易(如上所示),但我不知道如何从那时起进行查询,我似乎最终陷入了儿童的无限循环......例如:
var employerSubField = context.SubFields["employers"];
var otherJoins = employerSubField.SelectionSet.Children.Where(x => x.Children)
我似乎找不到 where name == "Employees" 或类似的名称。我是否需要在某个时候将 GraphQL.Language.AST.INode 转换为某些东西?当我在调试时检查值时,看起来我应该能够看到我所追求的值。但这不会编译。
var deptsField = employerSubField.SelectionSet.Selections.Where(x => x.Name == "departments");
【问题讨论】:
标签: graphql-dotnet