Skip to content

Commit 2614aee

Browse files
committed
feat: GraphQL resolver for comment entity
1 parent 81f9c15 commit 2614aee

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

src/resolvers/Comment.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
commentId: (parent, args, context) => {
3+
return parent.id;
4+
},
5+
};

src/resolvers/Post.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import { sort } from "../utils/common";
2+
import { fetchPostComments } from "../utils/fetchPostData";
3+
14
export 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
};

src/resolvers/Query.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { 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
611
export 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
};

src/resolvers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import Query from "./Query";
22
import Mutation from "./Mutation";
33
import User from "./User";
44
import Post from "./Post";
5+
import Comment from "./Comment";
56

67
export default {
78
Query,
89
Mutation,
910
User,
1011
Post,
12+
Comment,
1113
};

src/utils/fetchCommentData.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
};

src/utils/fetchPostData.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)