1

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.

gallargit
  • 11
  • 2

2 Answers2

0

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)
{
    ...
}

João Sequeira
  • 157
  • 3
  • 12
  • The point is not to use that but use dynamic filters instead, for example ```query { filteredPersons( where: { id: { gt: 1}, name: { contains: "oger" } } ) { id name } }``` I already switched to EntityFramework, by using that the "where" clause is done at the database - as it should be – gallargit Aug 08 '21 at 17:45
0

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

  • That was already in place. Filtering works, the problem is that full table scan is run every time a query is made, and the filtering happens in the client side. – gallargit Aug 08 '21 at 17:45