File tree Expand file tree Collapse file tree 6 files changed +70
-7
lines changed Expand file tree Collapse file tree 6 files changed +70
-7
lines changed Original file line number Diff line number Diff line change 1+ export  default  { 
2+  commentId : ( parent ,  args ,  context )  =>  { 
3+  return  parent . id ; 
4+  } , 
5+ } ; 
Original file line number Diff line number Diff line change 1+ import  {  sort  }  from  "../utils/common" ; 
2+ import  {  fetchPostComments  }  from  "../utils/fetchPostData" ; 
3+ 
14export  default  { 
2-  postId : async   ( parent ,  args ,  context )  =>  { 
5+  postId : ( parent ,  args ,  context )  =>  { 
36 return  parent . id ; 
47 } , 
8+  comments : async  ( parent ,  args ,  context )  =>  { 
9+  const  comments  =  await  fetchPostComments ( parent . id ) ; 
10+  return  sort ( comments ,  "asc" ) ; 
11+  } , 
512} ; 
Original file line number Diff line number Diff line change 11import  {  sort  }  from  "../utils/common" ; 
2- import  {  fetchUsers ,  fetchUser  }  from  "../utils/fetchUserData" ; 
3- import  {  fetchPosts ,  fetchPost ,  fetchUserPosts  }  from  "../utils/fetchPostData" ; 
2+ import  {  fetchUsers ,  fetchUser ,  fetchUserPosts  }  from  "../utils/fetchUserData" ; 
3+ import  { 
4+  fetchPosts , 
5+  fetchPost , 
6+  fetchPostComments , 
7+ }  from  "../utils/fetchPostData" ; 
8+ import  {  fetchComments ,  fetchComment  }  from  "../utils/fetchCommentData" ; 
49
510// 'parent' parameter carries the return value of the previous resolver execution level 
611export  default  { 
@@ -45,4 +50,25 @@ export default {
4550 throw  new  Error ( err ) ; 
4651 } 
4752 } , 
53+  comments : async  ( parent ,  args ,  context )  =>  { 
54+  try  { 
55+  let  comments ; 
56+  if  ( args . postId )  comments  =  await  fetchPostComments ( args . postId ) ; 
57+  else  comments  =  await  fetchComments ( ) ; 
58+ 
59+  return  sort ( comments ,  args . sort  ? args . sort  : "asc" ) ; 
60+  }  catch  ( err )  { 
61+  console . log ( err ) ; 
62+  throw  new  Error ( err ) ; 
63+  } 
64+  } , 
65+  comment : async  ( parent ,  args ,  context )  =>  { 
66+  try  { 
67+  const  comment  =  await  fetchComment ( args . commentId ) ; 
68+  return  comment ; 
69+  }  catch  ( err )  { 
70+  console . log ( err ) ; 
71+  throw  new  Error ( err ) ; 
72+  } 
73+  } , 
4874} ; 
Original file line number Diff line number Diff line change @@ -2,10 +2,12 @@ import Query from "./Query";
22import  Mutation  from  "./Mutation" ; 
33import  User  from  "./User" ; 
44import  Post  from  "./Post" ; 
5+ import  Comment  from  "./Comment" ; 
56
67export  default  { 
78 Query, 
89 Mutation, 
910 User, 
1011 Post, 
12+  Comment, 
1113} ; 
Original file line number Diff line number Diff line change 1+ import  {  jsonPlaceholder  }  from  "../base-axios" ; 
2+ 
3+ //* fetch all Comments 
4+ export  const  fetchComments  =  async  ( )  =>  { 
5+  try  { 
6+  const  comments  =  await  jsonPlaceholder . get ( `/comments` ) ; 
7+  return  comments . data ; 
8+  }  catch  ( err )  { 
9+  console . log ( err ) ; 
10+  throw  new  Error ( err ) ; 
11+  } 
12+ } ; 
13+ 
14+ //* fetch a single Comment 
15+ export  const  fetchComment  =  async  ( commentId )  =>  { 
16+  try  { 
17+  const  comment  =  await  jsonPlaceholder . get ( `/comments/${ commentId }  ` ) ; 
18+  return  comment . data ; 
19+  }  catch  ( err )  { 
20+  console . log ( err ) ; 
21+  throw  new  Error ( err ) ; 
22+  } 
23+ } ; 
Original file line number Diff line number Diff line change @@ -22,12 +22,12 @@ export const fetchPost = async (postId) => {
2222 } 
2323} ; 
2424
25- //* fetch User 's all Posts  
25+ //* fetch Post 's all Comments  
2626//! returns [] if no data 
27- export  const  fetchUserPosts  =  async  ( userId )  =>  { 
27+ export  const  fetchPostComments  =  async  ( postId )  =>  { 
2828 try  { 
29-  const  post  =  await  jsonPlaceholder . get ( `/posts?userId= ${ userId }  ` ) ; 
30-  return  post . data ; 
29+  const  comments  =  await  jsonPlaceholder . get ( `/posts/ ${ postId } /comments ` ) ; 
30+  return  comments . data ; 
3131 }  catch  ( err )  { 
3232 console . log ( err ) ; 
3333 throw  new  Error ( err ) ; 
                                 You can’t perform that action at this time. 
               
                  
0 commit comments