1

I haven't worked with expressions that much, I am trying to reference an Expression property by string name but I cam getting this error:

c# The member expression must specify a property or method that is public and that belongs to the type Soly.Models.Profile (Parameter 'expression')

public class ProfileFilterType : FilterInputType<Profile> {
        protected override void Configure(
        IFilterInputTypeDescriptor<Profile> descriptor) {
            descriptor.BindFieldsExplicitly();

            descriptor.Field(f => Build<IFilterInputTypeDescriptor<Profile>, string>("firstName"));
        }

        public static Expression<Func<TClass, TProperty>> Build<TClass, TProperty>(string fieldName) {
            var param = Expression.Parameter(typeof(TClass));
            var field = Expression.PropertyOrField(param, fieldName);
            return Expression.Lambda<Func<TClass, TProperty>>(field, param);
        }
    }

descriptor.field signature:

IFilterFieldDescriptor Field<TField>(Expression<Func<T, TField>> propertyOrMember);

I am trying to iterate over the Profile properties with reflection and add a field descriptor for each in HotChocolate GraphQL.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
THE AMAZING
  • 1,496
  • 2
  • 16
  • 38

1 Answers1

0

Change descriptor.Field(f => Build<IFilterInputTypeDescriptor<Profile>, string>("firstName")); to:

descriptor.Field(Build<Profile, string>("firstName"));

Otherwise your are creating an expression with following type Expression<Func<Profile, Expression<Func<IFilterInputTypeDescriptor<Profile>,string>>>> which is definitely not what is expected.

P.S.

Not sure, but should not something like descriptor.Field("firstName"); or descriptor.Field(p => p.firstName); just work without need to manually handle expression trees?

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Thank you! that works. My issue with descriptor.Field("firstName") is that GraphQL errors with "For the field FirstName of type ProfileFilterInput was no handler found." descriptor.Field(p => p.firstName); wont work for me because i am attempting to make something a bit more dynamic for all of my different queries – THE AMAZING Jan 17 '23 at 19:10
  • I would prefer to use descriptor.Field("firstName") but i just don't understand the error. I do have a bounty started here https://stackoverflow.com/questions/74417023/how-to-set-a-dummy-filter-handler-for-an-extending-field-in-hotchocolate-12-grap for an unrelated use-case – THE AMAZING Jan 17 '23 at 19:16