HotChocolate GraphQL query to page/filter/sort on nested array fields

Viewed 492

HotChocolate Version=12.3.2.0

I want to be able to page/filter/sort on nested fields. For example, where user id = 1234, get the user's 1st document set, then the 1st docFile in the document set, ordered by docFile createdDate.

    public class User
    {
        public int Id {get;set}
    
        [UsePaging]
        [UseFiltering]
        [UseSorting]
        public List<Document> Documents { get; set; }
    }
    
    public class Document
    {
        [UsePaging]
        [UseFiltering]
        [UseSorting]
        public List<DocFile> DocFiles { get; set; }
        public User User {get;set;}
    }
    
    public class DocFile
    {
       public string Name {get;set}
       public DateTime CreatedDate {get;set;}
       public Document Document {get;set;}
    }
  


    [UseAppDbContext]
    [UsePaging]
    [UseProjection]
    [UseFiltering]
    [UseSorting]
    public async Task<Connection<User>> GetUsersAsync(
        IResolverContext context,
        [ScopedService] DbContext dbContext,
        CancellationToken cancellationToken
    )
    {
        var dbResult = dbContext.Users.Filter(context).Sort(context).Project(context).ToArray();
        var result = await dbResult.ApplyCursorPaginationAsync(context, cancellationToken);
        return result;
    }

GraphQL Query

  users(
    where: {id: {eq: 1234}} 
  ) {
    nodes {
      documents(first:1){
        id
        files(first:1 order:{createdDate: DESC}) {
          nodes {                 
            name
            createdDate
          }
        }
      }
    }
  }
    

But when I execute the GraphQL query I currently get the following error:

"exceptionType": "InvalidOperationException", "message": "No generic method 'OrderByDescending' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. "

Any idea on how to do this?

1 Answers

If the [UseSorting] annotation comes from HotChocolate.Types its the old way of filtering (uses a different syntax). Then it should be order_by in your query.

try [HotChocolate.Data.UseSorting] to match your query.

Related