Skip to content

Commit f9907b9

Browse files
committed
refactor: user entity resolver
1 parent 2614aee commit f9907b9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/resolvers/User.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { fetchUserPosts } from "../utils/fetchPostData";
1+
import { sort } from "../utils/common";
2+
import { fetchUserPosts } from "../utils/fetchUserData";
23

34
export default {
45
posts: async (parent, args, context) => {
56
try {
6-
return await fetchUserPosts(parent.id);
7+
const posts = await fetchUserPosts(parent.id);
8+
return sort(posts, "asc");
79
} catch (err) {
810
console.log(err);
911
throw new Error(err);

src/utils/fetchUserData.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ export const fetchUser = async (userId) => {
2121
throw new Error(err);
2222
}
2323
};
24+
25+
//* fetch User's all Posts
26+
//! returns [] if no data
27+
export const fetchUserPosts = async (userId) => {
28+
try {
29+
const posts = await jsonPlaceholder.get(`/users/${userId}/posts`);
30+
return posts.data;
31+
} catch (err) {
32+
console.log(err);
33+
throw new Error(err);
34+
}
35+
};

0 commit comments

Comments
 (0)