Skip to content

Commit 0f5a6ab

Browse files
committed
Update /drafts to postConnection
1 parent 6b9fcdc commit 0f5a6ab

File tree

3 files changed

+79
-14
lines changed

3 files changed

+79
-14
lines changed

backend/src/query.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Query = prismaObjectType({
2222
})
2323
t.field('drafts', {
2424
type: 'PostConnection',
25+
args: t.prismaType.postsConnection.args,
2526
resolve: async (_, args, ctx: GraphQLServerContext) => {
2627
const user = await ctx.user
2728
if (!user) throw Error('Not auth.')

frontend/src/components/post/Drafts.tsx

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ import { useQuery } from 'react-apollo-hooks'
33
import useReactRouter from 'use-react-router'
44

55
import gql from 'graphql-tag'
6+
import get from 'lodash.get'
67

78
import PostType from './../../types/Post'
89

910
import PostList from './PostList'
1011

1112
interface FeedQueryResponse {
12-
drafts: [PostType]
13+
drafts: {
14+
pageInfo: {
15+
hasNextPage: boolean
16+
endCursor: string
17+
}
18+
edges: {
19+
node: PostType
20+
}[]
21+
aggregate: {
22+
count: number
23+
}
24+
}
1325
}
1426

1527
const Drafts: React.FC = () => {
@@ -19,6 +31,31 @@ const Drafts: React.FC = () => {
1931

2032
console.log('draftsQuery: ', draftsQuery)
2133

34+
const handleLoadMore = () => {
35+
if (!draftsQuery || !draftsQuery.data || !draftsQuery.data.drafts) return
36+
draftsQuery.fetchMore({
37+
variables: {
38+
after: draftsQuery.data.drafts.pageInfo.endCursor,
39+
},
40+
updateQuery: (previousResult, { fetchMoreResult }) => {
41+
if (!fetchMoreResult) {
42+
return previousResult
43+
}
44+
return {
45+
drafts: {
46+
__typename: 'PostConnection',
47+
aggregate: fetchMoreResult.drafts.aggregate,
48+
pageInfo: fetchMoreResult.drafts.pageInfo,
49+
edges: [
50+
...previousResult.drafts.edges,
51+
...fetchMoreResult.drafts.edges,
52+
],
53+
},
54+
}
55+
},
56+
})
57+
}
58+
2259
return (
2360
<div>
2461
<button
@@ -29,21 +66,48 @@ const Drafts: React.FC = () => {
2966
>
3067
logout
3168
</button>
32-
{draftsQuery.data && draftsQuery.data.drafts && (
33-
<PostList posts={draftsQuery.data.drafts} />
69+
{draftsQuery.data &&
70+
// @ts-ignore
71+
draftsQuery.data.drafts && (
72+
<PostList posts={draftsQuery.data.drafts.edges.map((e) => e.node)} />
73+
)}
74+
{get(draftsQuery, 'data.drafts.pageInfo.hasNextPage') && (
75+
<button onClick={handleLoadMore}>loadMore</button>
3476
)}
3577
</div>
3678
)
3779
}
3880

3981
const DRAFTS_QUERY = gql`
40-
query Drafts {
41-
drafts {
42-
id
43-
title
44-
author {
45-
id
46-
name
82+
query Drafts(
83+
$after: String
84+
$orderBy: PostOrderByInput
85+
$where: PostWhereInput
86+
$skip: Int
87+
) {
88+
drafts(
89+
after: $after
90+
orderBy: $orderBy
91+
where: $where
92+
first: 5
93+
skip: $skip
94+
) {
95+
pageInfo {
96+
hasNextPage
97+
endCursor
98+
}
99+
edges {
100+
node {
101+
id
102+
title
103+
author {
104+
id
105+
name
106+
}
107+
}
108+
}
109+
aggregate {
110+
count
47111
}
48112
}
49113
}

frontend/src/components/post/Feed.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { useQuery } from 'react-apollo-hooks'
33
import useReactRouter from 'use-react-router'
44

55
import gql from 'graphql-tag'
6+
import get from 'lodash.get'
67

78
import PostType from './../../types/Post'
89

910
import PostList from './PostList'
1011

1112
interface FeedQueryResponse {
12-
// data: {
1313
feed: {
1414
pageInfo: {
1515
hasNextPage: boolean
@@ -22,8 +22,6 @@ interface FeedQueryResponse {
2222
count: number
2323
}
2424
}
25-
// }
26-
// fetchMore: () => void
2725
}
2826

2927
const Feed: React.FC = () => {
@@ -72,7 +70,9 @@ const Feed: React.FC = () => {
7270
{feedQuery.data && feedQuery.data.feed && (
7371
<PostList posts={feedQuery.data.feed.edges.map((e) => e.node)} />
7472
)}
75-
<button onClick={handleLoadMore}>loadMore</button>
73+
{get(feedQuery, 'data.feed.pageInfo.hasNextPage') && (
74+
<button onClick={handleLoadMore}>loadMore</button>
75+
)}
7676
</div>
7777
)
7878
}

0 commit comments

Comments
 (0)