azu*_*ers 1 c# entity-framework graphql hotchocolate
我希望使用 Hot Chocolate 的过滤来查询一种数据类型;然后将过滤后的输出转换为另一种类型,然后将其作为 IQueryable 返回。但我似乎无法找到捕获过滤器输入以开始转换的方法。
这是我想要实现的目标的示例:
给定数据类
public class TypeA
{
public string Foo { get; set; }
}
public class TypeB
{
public string Fizz { get; set; }
public string Buzz { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望能够创建一个查询端点,例如
public class Query
{
[UseDbContext(typeof(DbContext))]
[UseFiltering(typeof(TypeA))]
public IQueryable<TypeB> GetTypeB(
[ScopedService] DbContext context,
[SomeAttributeToCaptureTheFilter] Filter filter) // <- this is the line I'm trying to figure out
{
IQueryable<TypeA> filteredTypeAs = context.TypeA.Filter(filter); // .Filter() doesn't exist, its just for example.
IQueryable<TypeB> filteredTypeBs;
/* Complex transformation logic that populates 'filteredTypeBs'
* requiring the 'filteredTypeAs' and additional Data from
* the database to complete. */
return filteredTypeBs;
}
}
Run Code Online (Sandbox Code Playgroud)
针对于此,我可以使用如下所示的 GraphQL 查询
query {
typeB(where: { foo: { eq: "bar" } }) {
fizz
buzz
}
}
Run Code Online (Sandbox Code Playgroud)
where: { foo: { eq: "bar" } }作为 的过滤器TypeA,并且
typeB {
fizz
buzz
}
Run Code Online (Sandbox Code Playgroud)
从转换后的TypeB.
使用[UseFiltering(typeof(TypeA))]确实有效,它设置了架构以按照我的意愿行事。
我正在寻找的是符合该行效果的东西[SomeAttributeToCaptureTheFilter] Filter filter。只是捕获过滤器并将其应用于 DbContext 中的数据的某种方法。
我还要说的是,总的来说,我对 GraphQL 非常陌生,所以我处理这个问题的方式可能是完全错误的。任何意见将是有益的。
为将来可能偶然发现这个问题的人回答我自己的问题。
答案就在HotChocolate.Data包裹里。它包含和Filter的扩展;允许您通过传递. 我在文档中找不到对此的任何引用,我在有关数据聚合的示例中遇到了它:https ://github.com/ChilliCream/hotchocolate/issues/924#issuecomment-921793977IQueryable<T>IEnumerable<T>IResolverContext
这基本上就是我的最终查询方法的样子:
using HotChocolate.Data;
using HotChocolate.Data.Filters.Expressions;
public class Query
{
[UseDbContext(typeof(DbContext))]
[UseFiltering(typeof(TypeA))]
public IQueryable<TypeB> GetTypeB(
[ScopedService] DbContext context,
IResolverContext resolverContext)
{
IQueryable<TypeA> filteredTypeAs = context.TypeA.Filter(resolverContext);
IQueryable<TypeB> filteredTypeBs;
/* Complex transformation logic that populates 'filteredTypeBs'
* requiring the 'filteredTypeAs' and additional Data from
* the database to complete. */
return filteredTypeBs;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我的示例有点深奥,但重点是它开放了对查询方法本身中结果的过滤版本的访问。这在数据聚合/操作方面提供了无限的可能性;同时仍然利用 HotChocolate 的内置功能。
作为旁注,似乎还有一个Sort扩展,它相当于[UseSorting].
| 归档时间: |
|
| 查看次数: |
3433 次 |
| 最近记录: |