1

I expose my 'Example' class using IQueryable and apply the [UseSorting] attribute so the user can define the sort order of the results. This works fine and the Playground allows me to do exactly that.

public class QueryType : ObjectType
{
    [UseSorting]
    public IQueryable<Example> GetExamples([Service]IExampleRepository repository)
    {
        return repository.GetExamples();
    }
}

public class ExampleType : ObjectType<Example>
{
    protected override void Configure(IObjectTypeDescriptor<Example> descriptor)
    {
    }
}

But the 'Example' class has three properties and I only want 2 of them to be orderable by the user. It makes no sense for the user to order by the third property. How do you specify one of the properties of 'Example' to be excluded from the ordering middleware?

4

1 回答 1

1

Suppose, you have Prop1, Prop2 and Prop3 and want to allow only sorting Prop1 and Prop2. To reach that you just need to implement the "sorting metadata" type in the following way:

 public class ExampleSortType : SortInputType<Example>
{
    protected override void Configure(ISortInputTypeDescriptor<Example> descriptor)
    {
        ISortInputTypeDescriptor<Example> sortInputTypeDescriptor = descriptor.BindFieldsExplicitly();

        sortInputTypeDescriptor.Sortable(d => d.Prop1);
        sortInputTypeDescriptor.Sortable(d => d.Prop2);
    }
}

and provide the UseSorting attribute with that metadata type:

[UseSorting(SortType = typeof(ExampleSortType))]
 public IQueryable<Example> GetExamples([Service]IExampleRepository repository)...
于 2020-06-17T18:02:22.417 回答