1- using HotChocolate ;
21using NetAPI . Features . GraphQL . DataLoaders ;
32using NetAPI . Features . GraphQL . Models ;
43using NetAPI . Features . GraphQL . Services ;
@@ -44,7 +43,7 @@ public async Task<IEnumerable<CommentType>> GetRecentCommentsAsync(
4443 CancellationToken cancellationToken )
4544 {
4645 var comments = await commentLoader . LoadAsync ( blog . Id , cancellationToken ) ;
47- return comments . Take ( 10 ) . Select ( MapCommentToType ) ; // Return only recent 10 comments
46+ return comments ? . Take ( 10 ) . Select ( MapCommentToType ) ?? Enumerable . Empty < CommentType > ( ) ; // Return only recent 10 comments
4847 }
4948
5049 /// <summary>
@@ -56,7 +55,7 @@ public async Task<int> GetPublishedPostCountAsync(
5655 CancellationToken cancellationToken )
5756 {
5857 var posts = await postLoader . LoadAsync ( blog . Id , cancellationToken ) ;
59- return posts . Count ( p => p . IsPublished ) ;
58+ return posts ? . Count ( p => p . IsPublished ) ?? 0 ;
6059 }
6160
6261 /// <summary>
@@ -68,7 +67,7 @@ public async Task<int> GetDraftPostCountAsync(
6867 CancellationToken cancellationToken )
6968 {
7069 var posts = await postLoader . LoadAsync ( blog . Id , cancellationToken ) ;
71- return posts . Count ( p => ! p . IsPublished ) ;
70+ return posts ? . Count ( p => ! p . IsPublished ) ?? 0 ;
7271 }
7372
7473 /// <summary>
@@ -80,7 +79,7 @@ public async Task<int> GetTotalViewsAsync(
8079 CancellationToken cancellationToken )
8180 {
8281 var posts = await postLoader . LoadAsync ( blog . Id , cancellationToken ) ;
83- return posts . Sum ( p => p . ViewCount ) ;
82+ return posts ? . Sum ( p => p . ViewCount ) ?? 0 ;
8483 }
8584
8685 /// <summary>
@@ -92,7 +91,7 @@ public async Task<int> GetTotalLikesAsync(
9291 CancellationToken cancellationToken )
9392 {
9493 var posts = await postLoader . LoadAsync ( blog . Id , cancellationToken ) ;
95- return posts . Sum ( p => p . LikeCount ) ;
94+ return posts ? . Sum ( p => p . LikeCount ) ?? 0 ;
9695 }
9796
9897 /// <summary>
@@ -105,7 +104,7 @@ public async Task<int> GetTotalLikesAsync(
105104 CancellationToken cancellationToken )
106105 {
107106 var posts = await postLoader . LoadAsync ( blog . Id , cancellationToken ) ;
108- var mostPopular = posts . OrderByDescending ( p => p . ViewCount ) . FirstOrDefault ( ) ;
107+ var mostPopular = posts ? . OrderByDescending ( p => p . ViewCount ) . FirstOrDefault ( ) ;
109108
110109 if ( mostPopular == null ) return null ;
111110
@@ -122,7 +121,7 @@ public async Task<int> GetTotalLikesAsync(
122121 CancellationToken cancellationToken )
123122 {
124123 var posts = await postLoader . LoadAsync ( blog . Id , cancellationToken ) ;
125- var latest = posts . OrderByDescending ( p => p . CreatedAt ) . FirstOrDefault ( ) ;
124+ var latest = posts ? . OrderByDescending ( p => p . CreatedAt ) . FirstOrDefault ( ) ;
126125
127126 if ( latest == null ) return null ;
128127
@@ -226,7 +225,7 @@ public async Task<IEnumerable<CommentType>> GetCommentsAsync(
226225 CancellationToken cancellationToken )
227226 {
228227 var comments = await commentLoader . LoadAsync ( post . Id , cancellationToken ) ;
229- return comments . Select ( MapCommentToType ) ;
228+ return comments ? . Select ( MapCommentToType ) ?? Enumerable . Empty < CommentType > ( ) ;
230229 }
231230
232231 /// <summary>
@@ -238,7 +237,7 @@ public async Task<IEnumerable<CommentType>> GetApprovedCommentsAsync(
238237 CancellationToken cancellationToken )
239238 {
240239 var comments = await commentLoader . LoadAsync ( post . Id , cancellationToken ) ;
241- return comments . Where ( c => c . IsApproved ) . Select ( MapCommentToType ) ;
240+ return comments ? . Where ( c => c . IsApproved ) . Select ( MapCommentToType ) ?? Enumerable . Empty < CommentType > ( ) ;
242241 }
243242
244243 /// <summary>
@@ -250,10 +249,10 @@ public async Task<IEnumerable<CommentType>> GetRecentCommentsAsync(
250249 CancellationToken cancellationToken )
251250 {
252251 var comments = await commentLoader . LoadAsync ( post . Id , cancellationToken ) ;
253- return comments . Where ( c => c . IsApproved )
252+ return comments ? . Where ( c => c . IsApproved )
254253 . OrderByDescending ( c => c . CreatedAt )
255254 . Take ( 5 )
256- . Select ( MapCommentToType ) ;
255+ . Select ( MapCommentToType ) ?? Enumerable . Empty < CommentType > ( ) ;
257256 }
258257
259258 /// <summary>
@@ -394,9 +393,9 @@ public TimeSpan GetTimeAgo([Parent] CommentType comment)
394393 }
395394
396395 // Helper methods
397- private static async Task < BlogType > MapBlogToTypeAsync ( Blog blog , IBlogService blogService , CancellationToken cancellationToken )
396+ private static Task < BlogType > MapBlogToTypeAsync ( Blog blog , IBlogService blogService , CancellationToken cancellationToken )
398397 {
399- return new BlogType
398+ return Task . FromResult ( new BlogType
400399 {
401400 Id = blog . Id ,
402401 Title = blog . Title ,
@@ -410,12 +409,12 @@ private static async Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService b
410409 PostCount = blog . Posts ? . Count ?? 0 ,
411410 CommentCount = blog . Comments ? . Count ?? 0 ,
412411 LastPostDate = blog . Posts ? . OrderByDescending ( p => p . CreatedAt ) . FirstOrDefault ( ) ? . CreatedAt
413- } ;
412+ } ) ;
414413 }
415414
416- private static async Task < PostType > MapPostToTypeAsync ( Post post , IPostService postService , CancellationToken cancellationToken )
415+ private static Task < PostType > MapPostToTypeAsync ( Post post , IPostService postService , CancellationToken cancellationToken )
417416 {
418- return new PostType
417+ return Task . FromResult ( new PostType
419418 {
420419 Id = post . Id ,
421420 Title = post . Title ,
@@ -434,7 +433,7 @@ private static async Task<PostType> MapPostToTypeAsync(Post post, IPostService p
434433 CommentCount = post . Comments ? . Count ?? 0 ,
435434 ReadingTime = CalculateReadingTime ( post . Content ) ,
436435 Slug = GenerateSlug ( post . Title )
437- } ;
436+ } ) ;
438437 }
439438
440439 private static CommentType MapCommentToType ( Comment comment )
0 commit comments