How to get access to class IList members through GraphQL?

How to get access to class IList members through GraphQL?


0

I have a simple Graphql query like this

 public class Query
    { 
        public async Task<IPagedList<Book>> Books(int pageIndex, int pageSize)
         {
             var data = new List<Book>() {
                 new()
             {
                 Title = "C# in depth.",
                 Author = new Author
                 {
                     Name = "Jon Skeet"
                 }
             },new()
                 {
                 Title = "How to cook Phở",
                 Author = new Author
                 {
                     Name = "Nam Vo"
                 }
             },new()
                 {
                 Title = "How to cook Phở2",
                 Author = new Author
                 {
                     Name = "Nam Vo"
                 }
             },new()
                 {
                 Title = "How to cook Phở3",
                 Author = new Author
                 {
                     Name = "Nam Vo"
                 }
             }
             };
             var ret= await data.ToPagedListAsync(pageIndex, pageSize, data.Count);
             
             return ret;
         }
    }

Definition for PagedList:

public interface IPagedList<T> : IList<T>
{       
    int PageIndex { get; }    
    
    int PageSize { get; }

   
    int TotalCount { get; }

    
    int TotalPages { get; }

  
    bool HasPreviousPage { get; }

   
    bool HasNextPage { get; }
}



    public class PagedList<T> : List<T>, IPagedList<T>
    {

        public PagedList(IList<T> source, int pageIndex, int pageSize, int? totalCount = null)
        {
            //min allowed page size is 1
            pageSize = Math.Max(pageSize, 1);

            TotalCount = totalCount ?? source.Count;
            TotalPages = TotalCount / pageSize;

            if (TotalCount % pageSize > 0)
                TotalPages++;

            PageSize = pageSize;
            PageIndex = pageIndex;
            AddRange(totalCount != null ? source : source.Skip(pageIndex * pageSize).Take(pageSize));
        }
       
     
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public bool HasPreviousPage => PageIndex > 0;
        public bool HasNextPage => PageIndex + 1 < TotalPages;
    }

Static class to call ToPagedListAsync:

public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T> source,
                                                          int pageIndex,
                                                          int pageSize,
                                                          int totalCount)
{

    if (source == null)
        return new PagedList<T>(new List<T>(), pageIndex, pageSize);

    //min allowed page size is 1
    pageSize = Math.Max(pageSize, 1);
    var data = new List<T>();



    data.AddRange(await source.Skip(pageIndex * pageSize)
        .Take(pageSize)
        .ToAsyncEnumerable()
        .ToListAsync());


    return new PagedList<T>(data, pageIndex, pageSize, totalCount);
}

The result returns the subset of the datasource. I just wonder why through Graphql I cannot get access to PagedList members (pageIndex, pageSize, totalCount, hasPreviousPage…) . I know I can refactor the method to return new model, but that’s extra work.

How to get access to class IList members through GraphQL?


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *