graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Filter extension method on IQueryable<T> not working

Open hnazari13701371 opened this issue 3 years ago • 5 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

when i use Filter extension method, this method not working unless i expilictly use below code on before resolve middleware:

                 context.LocalContextData = context.LocalContextData.SetItem(QueryableFilterProvider.SkipFilteringKey
                    , false);

Steps to reproduce

public class QueryType : ObjectType<Query>
    {
        protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
        {

            descriptor.Field(f => f.Companies)
                .Authorize()
                .UseOffsetPaging()
                .UseProjection()
                .UseFiltering()
                .UseSorting()
                .Resolve(async ctx =>
                {
                         //load IQueryable<Company>
                });
}
}`

2.
`    public class CompanyType : ObjectType<Company>
    {
        protected override void Configure(IObjectTypeDescriptor<Company> descriptor)
        {
            descriptor.Field(f => f.Jobs)
                .Use(next => async context =>
                {
                    context.LocalContextData = context.LocalContextData.SetItem(QueryableFilterProvider.SkipFilteringKey
                        , false);
                    await next(context);
                })
                .UseOffsetPaging()
                .UseProjection()
                .UseFiltering()
                .UseSorting()
                .IsProjected(false)
                .Resolve(async ctx =>
                {
                    var skip = ctx.ArgumentValue<int>("skip");
                    var take = ctx.ArgumentValue<int>("take");
                     //load jobs from db
                    jobs = jobs.Filter(ctx).Sort(ctx);
                    if (skip > 0)
                        jobs = jobs.Skip(skip);
                    if (take > 0)
                        jobs = jobs.Take(take);
                    return jobs;
                });

}
}

Relevant log output

No response

Additional Context?

No response

Product

Hot Chocolate

Version

12.9.0

hnazari13701371 avatar Jun 06 '22 11:06 hnazari13701371

Is you resolver hit? hoe does the rexpression look like on return jobs?

PascalSenn avatar Jun 06 '22 12:06 PascalSenn

yes my resolver hit for your second question consider I have got 10 records from DB and after using Filter, I still got 10 I checked this Extension method, after the first time of filtering apply u set context.LocalContextData.SetItem(QueryableFilterProvider.SkipFilteringKey , true); see below link: https://github.com/ChilliCream/hotchocolate/blob/ad668d74ef12062eb2210c0714743628c1672e26/src/HotChocolate/Data/src/Data/Filters/Expressions/QueryableFilterProvider.cs

and it makes sense you prevent filtering twice but why this field is already set to true when I never use filtering as u see in my code samples?

Is you resolver hit? hoe does the rexpression look like on return jobs?

hnazari13701371 avatar Jun 06 '22 18:06 hnazari13701371

Possibly because it is "half projected" https://github.com/ChilliCream/hotchocolate/blob/8f0d403b1807b56215e188f2c90aec2bd5511bf2/src/HotChocolate/Data/src/Data/Projections/Expressions/Optimizers/QueryableFilterProjectionOptimizer.cs#L22-L30

i suppose it registers the middleware but does not respect IsProjected(false)

thanks for reporting

PascalSenn avatar Jun 07 '22 11:06 PascalSenn

Can this one be closed?

michaelstaib avatar Aug 08 '22 16:08 michaelstaib

I am not sure , i think its a bug but i solve it by defining a middleware and manually set property then filtering start working

On Mon, 8 Aug 2022, 21:18 Michael Staib, @.***> wrote:

Can this one be closed?

— Reply to this email directly, view it on GitHub https://github.com/ChilliCream/hotchocolate/issues/5126#issuecomment-1208364346, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJFFMF3LDNFL7BLAILLOUYDVYE26TANCNFSM5X7E6RGQ . You are receiving this because you authored the thread.Message ID: @.***>

hnazari13701371 avatar Aug 08 '22 17:08 hnazari13701371