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

Custom Filtering is not working for null values

Open Sangeetha-Murugesan opened this issue 3 years ago • 10 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

I was trying to implement case insensitive filtering and found this example on your documentation.

It is working fine if the resultant field has values. If the resultant field has value as null,it throws Object Reference error.

In my case, api return an array of data(shiptoCust) in which some might have value and some might be null. while applyig filter to this field,I am getting object reference not set to an instance.

query Query {
    getpartSaleOrder{
      data(skip:0,take:5,where: {shiptoCust:{eq:"4001"}}) {
    		shiptoCust
      }
    }
  }

Steps to reproduce

public class CustomConvention : FilterConvention
       {
           protected override void Configure(IFilterConventionDescriptor descriptor)
           {
               descriptor.AddDefaults();
               descriptor.Provider(
                   new QueryableFilterProvider(
                       x => x
                           .AddDefaultFieldHandlers().AddFieldHandler<QueryableStringInvariantContainsHandler>()
                           .AddFieldHandler< QueryableStringInvariantEqualsHandler>()));
           }
       }

 public class QueryableStringInvariantEqualsHandler : QueryableStringOperationHandler
        {

            public QueryableStringInvariantEqualsHandler(InputParser inputParser) : base(inputParser){ }

            private static readonly MethodInfo _toLower = typeof(string).GetMethods().Single(
                 x => x.Name == nameof(string.ToLower) && x.GetParameters().Length == 0);

            protected override int Operation => DefaultFilterOperations.Equals;

            public override Expression HandleOperation(QueryableFilterContext context, IFilterOperationField field, IValueNode value, object? parsedValue)
            {

         
                Expression property = context.GetInstance();

                if (parsedValue != null && parsedValue is string str)
                {

                    return Expression.Equal(
                        Expression.Call(property, _toLower),
                        Expression.Constant(str.ToLower()));
                }

                throw new InvalidOperationException();
           
            }

        }

In Configure Method:

services.AddGraphQLServer(component)
                  .AddFiltering<CustomConvention>()
                  .AddConvention<IFilterConvention>(new FilterConventionExtension(
                       x => x.AddProviderExtension(new QueryableFilterProviderExtension(
                       y => y.AddFieldHandler<QueryableStringInvariantEqualsHandler>().AddFieldHandler<QueryableStringInvariantContainsHandler>())
                  )));

Relevant log output

No response

Additional Context?

No response

Product

Hot Chocolate

Version

12.6.1

Sangeetha-Murugesan avatar Mar 08 '22 13:03 Sangeetha-Murugesan

I guess something like this should work

 public class QueryableStringInvariantEqualsHandler : QueryableStringOperationHandler
        {

            public QueryableStringInvariantEqualsHandler(InputParser inputParser) : base(inputParser){ }

            private static readonly MethodInfo _toLower = typeof(string).GetMethods().Single(
                 x => x.Name == nameof(string.ToLower) && x.GetParameters().Length == 0);

            protected override int Operation => DefaultFilterOperations.Equals;

            public override Expression HandleOperation(QueryableFilterContext context, IFilterOperationField field, IValueNode value, object? parsedValue)
            {

         
                Expression property = context.GetInstance();
                if ( parsedValue is null)
                { 
                    return Expression.Equal(property, Expression.Constant(null));
                }

                if ( parsedValue is string str)
                {

                    return Expression.Equal(
                        Expression.Call(property, _toLower),
                        Expression.Constant(str.ToLower()));
                }

                throw new InvalidOperationException();
           
            }

        }

PascalSenn avatar Mar 08 '22 14:03 PascalSenn

@PascalSenn , adding the null condition in QueryableStringInvariantEqualsHandler class also not works,getting the same error

PS: I am getting object reference error in this line await next.Invoke(context); in useField Middleware.

Sangeetha-Murugesan avatar Mar 09 '22 04:03 Sangeetha-Murugesan

@Sangeetha-Murugesan can you push a reproduction into a simple example project?

PascalSenn avatar Mar 16 '22 08:03 PascalSenn

@PascalSenn , Source code is available in Source Code - Custom Filtering

Sangeetha-Murugesan avatar Mar 24 '22 12:03 Sangeetha-Murugesan

@PascalSenn any update on this issue?

Sangeetha-Murugesan avatar Apr 11 '22 04:04 Sangeetha-Murugesan

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 10 '22 05:06 stale[bot]

@Sangeetha-Murugesan I do not know what should not work in your example project

PascalSenn avatar Jun 11 '22 18:06 PascalSenn

can you provide an example query or something similar?

PascalSenn avatar Jun 11 '22 18:06 PascalSenn

PFB. Sample query to simulate the issue. On query execution, you will get object reference error because in response,one of the field value for which I have applied filter is null. If the response doesnt have any null value,it works fine. you can refer the sample response data in source file.

{ getpartSaleOrderInquiry{ data{ items{ partDetails(where:{mrstatusArr:{eq:"Pend"}}){ items{ mrstatusArr } } saleOrderNo saleOrderStatus } } } }

Sangeetha-Murugesan avatar Jun 12 '22 12:06 Sangeetha-Murugesan

@PascalSenn @tobias-tengler , any update on this?

Sangeetha-Murugesan avatar Sep 08 '22 12:09 Sangeetha-Murugesan

https://github.com/ChilliCream/hotchocolate/issues/4822#issuecomment-1061826600

this worked for me

lucas-garrido avatar Dec 21 '22 11:12 lucas-garrido