Skip to content

Commit 0606773

Browse files
committed
03-React GraphQL Pagination
1 parent c08126a commit 0606773

File tree

1 file changed

+83
-15
lines changed

1 file changed

+83
-15
lines changed

src/App.js

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,77 @@ const axiosGitHubGraphQL = axios.create({
1313
});
1414

1515
const GET_ISSUES_OF_REPOSITORY = `
16-
query ($organization: String!, $repository: String!) {
16+
query ($organization: String!, $repository: String!, $cursor: String) {
1717
organization(login: $organization) {
1818
name
1919
url
2020
repository(name: $repository) {
2121
name
2222
url
23-
issues(last: 5) {
23+
issues(first: 5, after: $cursor, states: [OPEN]) {
2424
edges {
2525
node {
2626
id
2727
title
2828
url
29+
reactions(last: 3) {
30+
edges {
31+
node {
32+
id
33+
content
34+
}
35+
}
36+
}
2937
}
3038
}
39+
totalCount
40+
pageInfo {
41+
endCursor
42+
hasNextPage
43+
}
3144
}
3245
}
3346
}
3447
}
3548
`;
3649

37-
const getIssuesOfRepository = path => {
50+
const getIssuesOfRepository = (path, cursor) => {
3851
const [organization, repository] = path.split('/');
3952

4053
return axiosGitHubGraphQL.post('', {
4154
query: GET_ISSUES_OF_REPOSITORY,
42-
variables: { organization, repository },
55+
variables: { organization, repository, cursor },
4356
});
4457
};
4558

46-
const resolveIssuesQuery = queryResult => () => ({
47-
organization: queryResult.data.data.organization,
48-
errors: queryResult.data.errors,
49-
});
59+
const resolveIssuesQuery = (queryResult, cursor) => state => {
60+
const { data, errors } = queryResult.data;
61+
62+
if (!cursor) {
63+
return {
64+
organization: data.organization,
65+
errors,
66+
};
67+
}
68+
69+
const { edges: oldIssues } = state.organization.repository.issues;
70+
const { edges: newIssues } = data.organization.repository.issues;
71+
const updatedIssues = [...oldIssues, ...newIssues];
72+
73+
return {
74+
organization: {
75+
...data.organization,
76+
repository: {
77+
...data.organization.repository,
78+
issues: {
79+
...data.organization.repository.issues,
80+
edges: updatedIssues,
81+
},
82+
},
83+
},
84+
errors,
85+
};
86+
};
5087

5188
class App extends Component {
5289
state = {
@@ -69,12 +106,20 @@ class App extends Component {
69106
event.preventDefault();
70107
};
71108

72-
onFetchFromGitHub = path => {
73-
getIssuesOfRepository(path).then(queryResult =>
74-
this.setState(resolveIssuesQuery(queryResult)),
109+
onFetchFromGitHub = (path, cursor) => {
110+
getIssuesOfRepository(path, cursor).then(queryResult =>
111+
this.setState(resolveIssuesQuery(queryResult, cursor)),
75112
);
76113
};
77114

115+
onFetchMoreIssues = () => {
116+
const {
117+
endCursor,
118+
} = this.state.organization.repository.issues.pageInfo;
119+
120+
this.onFetchFromGitHub(this.state.path, endCursor);
121+
};
122+
78123
render() {
79124
const { path, organization, errors } = this.state;
80125

@@ -99,7 +144,11 @@ class App extends Component {
99144
<hr />
100145

101146
{organization ? (
102-
<Organization organization={organization} errors={errors} />
147+
<Organization
148+
organization={organization}
149+
errors={errors}
150+
onFetchMoreIssues={this.onFetchMoreIssues}
151+
/>
103152
) : (
104153
<p>No information yet ...</p>
105154
)}
@@ -108,7 +157,11 @@ class App extends Component {
108157
}
109158
}
110159

111-
const Organization = ({ organization, errors }) => {
160+
const Organization = ({
161+
organization,
162+
errors,
163+
onFetchMoreIssues,
164+
}) => {
112165
if (errors) {
113166
return (
114167
<p>
@@ -124,12 +177,15 @@ const Organization = ({ organization, errors }) => {
124177
<strong>Issues from Organization:</strong>
125178
<a href={organization.url}>{organization.name}</a>
126179
</p>
127-
<Repository repository={organization.repository} />
180+
<Repository
181+
repository={organization.repository}
182+
onFetchMoreIssues={onFetchMoreIssues}
183+
/>
128184
</div>
129185
);
130186
};
131187

132-
const Repository = ({ repository }) => (
188+
const Repository = ({ repository, onFetchMoreIssues }) => (
133189
<div>
134190
<p>
135191
<strong>In Repository:</strong>
@@ -140,9 +196,21 @@ const Repository = ({ repository }) => (
140196
{repository.issues.edges.map(issue => (
141197
<li key={issue.node.id}>
142198
<a href={issue.node.url}>{issue.node.title}</a>
199+
200+
<ul>
201+
{issue.node.reactions.edges.map(reaction => (
202+
<li key={reaction.node.id}>{reaction.node.content}</li>
203+
))}
204+
</ul>
143205
</li>
144206
))}
145207
</ul>
208+
209+
<hr />
210+
211+
{repository.issues.pageInfo.hasNextPage && (
212+
<button onClick={onFetchMoreIssues}>More</button>
213+
)}
146214
</div>
147215
);
148216

0 commit comments

Comments
 (0)