简体   繁体   中英

Lazy loading data in Dapper using HotChocolate GraphQL

I have a problem using GraphQL with the HotChocolate library in a C# backend. 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? Maybe with DapperExtensions using a predicate? It would be hard anyway as the filters are not really accessible in the GraphQL query.

You can use arguments in order to receive the parameters on the resolver method. It allows complex objects and mark arguments as required, this adds a lot of flexibility.

Graphql:

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

C#:

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

You try using [UseFiltering()] and add.AddFiltering() to services.AddGraphQLServer()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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