Skip to content

Commit 697a79b

Browse files
committed
refactor: ⚡ Use recetPosts query for caching
1 parent 1029e67 commit 697a79b

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

src/lib/graphql/post.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,8 @@ export const GET_POST_LIST = gql`
178178
`;
179179

180180
export const GET_RECENT_POSTS = gql`
181-
query RecentPosts(
182-
$cursor: ID
183-
$username: String
184-
$temp_only: Boolean
185-
$tag: String
186-
$limit: Int
187-
) {
188-
posts(
189-
cursor: $cursor
190-
username: $username
191-
temp_only: $temp_only
192-
tag: $tag
193-
limit: $limit
194-
) {
181+
query RecentPosts($cursor: ID, $limit: Int) {
182+
recentPosts(cursor: $cursor, limit: $limit) {
195183
id
196184
title
197185
short_description

src/pages/home/RecentPostsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function RecentPostsPage(props: RecentPostsPageProps) {
1818
/>
1919
</Helmet>
2020
<PostCardGrid
21-
posts={data?.posts || []}
21+
posts={data?.recentPosts || []}
2222
forHome
2323
loading={!data || loading}
2424
/>

src/pages/home/hooks/useRecentPosts.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useCallback, useState } from 'react';
44
import useScrollPagination from '../../../lib/hooks/useScrollPagination';
55

66
export default function useRecentPosts() {
7-
const { data, loading, fetchMore } = useQuery<{ posts: PartialPost[] }>(
7+
const { data, loading, fetchMore } = useQuery<{ recentPosts: PartialPost[] }>(
88
GET_RECENT_POSTS,
99
{
1010
variables: {
@@ -25,19 +25,19 @@ export default function useRecentPosts() {
2525
},
2626
updateQuery: (prev, { fetchMoreResult }) => {
2727
if (!fetchMoreResult) return prev;
28-
if (fetchMoreResult.posts.length === 0) {
28+
if (fetchMoreResult.recentPosts.length === 0) {
2929
setIsFinished(true);
3030
}
3131
return {
32-
posts: [...prev.posts, ...fetchMoreResult.posts],
32+
recentPosts: [...prev.recentPosts, ...fetchMoreResult.recentPosts],
3333
};
3434
},
3535
});
3636
},
3737
[fetchMore],
3838
);
3939

40-
const cursor = data?.posts[data?.posts.length - 1]?.id;
40+
const cursor = data?.recentPosts[data?.recentPosts.length - 1]?.id;
4141

4242
useScrollPagination({
4343
cursor,

0 commit comments

Comments
 (0)