Search code examples
graphqlhotchocolate

hotchocolate throws error when using UseFiltering() on a field


I have a pretty simple setyp where I'm putting graphql over an entityframework datacontext (sql server).

I'm trying to get filtering to work. I've tried adding .UseFiltering() to a field descriptor like so...

descriptor.Field(t => t.AccountName).Type<NonNullType<StringType>>().UseFiltering();

But it causes this error on startup...

HotChocolate.SchemaException: 'Unable to infer or resolve a schema type from the type reference Input: System.Char.'

I assume I'm doing something wrong somewhere...


Solution

  • "UseFiltering" is supposed to be used to filter data which represents a collection of items in some way (IQueryable, IEnumerable, etc). For instance, if you have users collection and each user has AccountName property you could filter that collection by AccountName:

    [ExtendObjectType(Name = "Query")]
    public class UserQuery
    {
        [UseFiltering]
        public async Task<IEnumerable<User>> GetUsers([Service]usersRepo)
        {
           IQueryable<User> users = usersRepo.GetUsersQueryable();
        }
    }
    

    In that example the HotChocolate implementation of filtering will generate a number of filters by user fields which you can use in the following way:

    users(where: {AND: [{accountName_starts_with: "Tech"}, {accountName_not_ends_with: "Test"}]})
    

    According to your example: the system thinks that AccountName is a collection, so tries to build filtering across the chars the AccountName consists of.