如何解决在 GraphQL dotnet
我们在现有的应用数据库之上有一个 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 中的 context.Arguments,当我解析它以在未查询特定子字段时消除连接。
一个非常简单的高级版本如下:
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 查询。如果用户只想要组织名称和每个雇主名称,这将是一项繁重的工作。
在最顶层过滤很容易(如上所示),但我无法弄清楚如何从那时起查询,我似乎最终陷入了 Children 的无限循环......例如:
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");
解决方法
我找到了以下方法来获取基本信息,然后我可以轻松地解析自己并添加自己的自定义逻辑。
var query = context.Document.OriginalQuery.Substring(context.Document.OriginalQuery.IndexOf("{")).RemoveWhitespace();
返回:
{organisations{id,name,employers{id,clientNotes,departments{id,name}}}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。