Skip to content

Commit 5a90355

Browse files
committed
add fetchMore
1 parent d8c39cd commit 5a90355

File tree

1 file changed

+73
-14
lines changed

1 file changed

+73
-14
lines changed

src/App.js

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const axiosGitHubGraphQL = axios.create({
1313
const title = 'React GraphQL GitHub Client';
1414

1515
const issuesOfRepositoryQuery = `
16-
query ($organization: String!, $repository: String!) {
16+
query ($organization: String!, $repository: String!, $cursor: String) {
1717
organization(login: $organization) {
1818
name
1919
url
@@ -25,9 +25,8 @@ const issuesOfRepositoryQuery = `
2525
totalCount
2626
}
2727
viewerHasStarred
28-
issues(last: 5, states: [OPEN]) {
28+
issues(first: 5, after: $cursor, states: [OPEN]) {
2929
edges {
30-
cursor
3130
node {
3231
id
3332
title
@@ -42,6 +41,11 @@ const issuesOfRepositoryQuery = `
4241
}
4342
}
4443
}
44+
totalCount
45+
pageInfo {
46+
endCursor
47+
hasNextPage
48+
}
4549
}
4650
}
4751
}
@@ -68,15 +72,39 @@ const removeStarToRepositoryMutation = `
6872
}
6973
`;
7074

71-
const getIssuesOfRepository = path => {
75+
const getIssuesOfRepository = (path, cursor) => {
7276
const [organization, repository] = path.split('/');
7377

7478
return axiosGitHubGraphQL.post('', {
7579
query: issuesOfRepositoryQuery,
76-
variables: { organization, repository },
80+
variables: { organization, repository, cursor },
7781
});
7882
};
7983

84+
const resolveIssuesQuery = queryResult => state => {
85+
const oldIssues = state.organization
86+
? state.organization.repository.issues.edges
87+
: [];
88+
89+
const {
90+
edges: newIssues,
91+
} = queryResult.data.data.organization.repository.issues;
92+
93+
return {
94+
organization: {
95+
...queryResult.data.data.organization,
96+
repository: {
97+
...queryResult.data.data.organization.repository,
98+
issues: {
99+
...queryResult.data.data.organization.repository.issues,
100+
edges: [...oldIssues, ...newIssues],
101+
},
102+
},
103+
},
104+
errors: queryResult.data.errors,
105+
};
106+
};
107+
80108
const addStarToRepository = repositoryId => {
81109
return axiosGitHubGraphQL.post('', {
82110
query: addStarToRepositoryMutation,
@@ -156,12 +184,9 @@ class App extends Component {
156184
event.preventDefault();
157185
};
158186

159-
onFetchFromGitHub = path => {
160-
getIssuesOfRepository(path).then(result =>
161-
this.setState(() => ({
162-
organization: result.data.data.organization,
163-
errors: result.data.errors,
164-
})),
187+
onFetchFromGitHub = (path, cursor) => {
188+
getIssuesOfRepository(path, cursor).then(queryResult =>
189+
this.setState(resolveIssuesQuery(queryResult)),
165190
);
166191
};
167192

@@ -177,6 +202,14 @@ class App extends Component {
177202
}
178203
};
179204

205+
onFetchMoreIssues = () => {
206+
const {
207+
endCursor,
208+
} = this.state.organization.repository.issues.pageInfo;
209+
210+
this.onFetchFromGitHub(this.state.path, endCursor);
211+
};
212+
180213
render() {
181214
const { path, organization, errors } = this.state;
182215

@@ -205,6 +238,7 @@ class App extends Component {
205238
organization={organization}
206239
errors={errors}
207240
onStarRepository={this.onStarRepository}
241+
onFetchMoreIssues={this.onFetchMoreIssues}
208242
/>
209243
) : (
210244
<p>No information yet ...</p>
@@ -214,7 +248,12 @@ class App extends Component {
214248
}
215249
}
216250

217-
const Organization = ({ organization, errors, onStarRepository }) => {
251+
const Organization = ({
252+
organization,
253+
errors,
254+
onStarRepository,
255+
onFetchMoreIssues,
256+
}) => {
218257
if (errors) {
219258
return (
220259
<p>
@@ -233,12 +272,17 @@ const Organization = ({ organization, errors, onStarRepository }) => {
233272
<Repository
234273
repository={organization.repository}
235274
onStarRepository={onStarRepository}
275+
onFetchMoreIssues={onFetchMoreIssues}
236276
/>
237277
</div>
238278
);
239279
};
240280

241-
const Repository = ({ repository, onStarRepository }) => (
281+
const Repository = ({
282+
repository,
283+
onStarRepository,
284+
onFetchMoreIssues,
285+
}) => (
242286
<div>
243287
<p>
244288
<strong>In Repository:</strong>
@@ -255,8 +299,17 @@ const Repository = ({ repository, onStarRepository }) => (
255299
{repository.viewerHasStarred ? ' Unstar' : ' Star'}
256300
</button>
257301

302+
<Issues
303+
issues={repository.issues}
304+
onFetchMoreIssues={onFetchMoreIssues}
305+
/>
306+
</div>
307+
);
308+
309+
const Issues = ({ issues, onFetchMoreIssues }) => (
310+
<div>
258311
<ul>
259-
{repository.issues.edges.map(issue => (
312+
{issues.edges.map(issue => (
260313
<li key={issue.node.id}>
261314
<a href={issue.node.url}>{issue.node.title}</a>
262315

@@ -268,6 +321,12 @@ const Repository = ({ repository, onStarRepository }) => (
268321
</li>
269322
))}
270323
</ul>
324+
325+
<hr />
326+
327+
{issues.pageInfo.hasNextPage && (
328+
<button onClick={onFetchMoreIssues}>More</button>
329+
)}
271330
</div>
272331
);
273332

0 commit comments

Comments
 (0)