简体   繁体   English

使用 HotChocolate GraphQL 在 Dapper 中延迟加载数据

[英]Lazy loading data in Dapper using HotChocolate GraphQL

I have a problem using GraphQL with the HotChocolate library in a C# backend.我在 C# 后端中使用带有 HotChocolate 库的 GraphQL 时遇到问题。 When I use filtering the whole table is loaded and after that it is filtered.当我使用过滤时,整个表被加载,然后被过滤。 This of course means an awful performance as a full table scan is performed each time.这当然意味着每次执行全表扫描时性能很差。

This is the query这是查询

        [UseFiltering]
        public IQueryable<Person> GetFilteredPersons(
            [Service] IPersonRepo repo)
        {
            return repo.GetAll().AsQueryable();
        }

And this is the repo这是回购

        public IEnumerable<Person> GetAll()
        {
            var query = "SELECT * FROM Persons";
            var param = new DynamicParameters();
            return SqlMapper.Query<Person>(GetConnection()
                query, param,
                commandType: CommandType.Text);
        }

How can I make the filters be passed to the WHERE clause?如何使过滤器传递给 WHERE 子句? Maybe with DapperExtensions using a predicate?也许 DapperExtensions 使用谓词? It would be hard anyway as the filters are not really accessible in the GraphQL query.无论如何都很难,因为在 GraphQL 查询中无法真正访问过滤器。

You can use arguments in order to receive the parameters on the resolver method.您可以使用arguments来接收解析器方法的参数。 It allows complex objects and mark arguments as required, this adds a lot of flexibility.它允许复杂的对象并根据需要标记 arguments,这增加了很多灵活性。

Graphql: Graphql:

{
  person(country: String!) {
     ...
  }
}

C#: C#:

public IEnumerable<Person> GetAll(string country)
{
    ...
}

You try using [UseFiltering()] and add.AddFiltering() to services.AddGraphQLServer()您尝试使用 [UseFiltering()] 和 add.AddFiltering() 到 services.AddGraphQLServer()

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

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