Skip to content

Commit 3dcd95e

Browse files
committed
04-React GraphQL Mutation
1 parent 0606773 commit 3dcd95e

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

src/App.js

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ const GET_ISSUES_OF_REPOSITORY = `
1818
name
1919
url
2020
repository(name: $repository) {
21+
id
2122
name
2223
url
24+
stargazers {
25+
totalCount
26+
}
27+
viewerHasStarred
2328
issues(first: 5, after: $cursor, states: [OPEN]) {
2429
edges {
2530
node {
@@ -47,6 +52,26 @@ const GET_ISSUES_OF_REPOSITORY = `
4752
}
4853
`;
4954

55+
const ADD_STAR = `
56+
mutation ($repositoryId: ID!) {
57+
addStar(input:{starrableId:$repositoryId}) {
58+
starrable {
59+
viewerHasStarred
60+
}
61+
}
62+
}
63+
`;
64+
65+
const REMOVE_STAR = `
66+
mutation ($repositoryId: ID!) {
67+
removeStar(input:{starrableId:$repositoryId}) {
68+
starrable {
69+
viewerHasStarred
70+
}
71+
}
72+
}
73+
`;
74+
5075
const getIssuesOfRepository = (path, cursor) => {
5176
const [organization, repository] = path.split('/');
5277

@@ -85,6 +110,64 @@ const resolveIssuesQuery = (queryResult, cursor) => state => {
85110
};
86111
};
87112

113+
const addStarToRepository = repositoryId => {
114+
return axiosGitHubGraphQL.post('', {
115+
query: ADD_STAR,
116+
variables: { repositoryId },
117+
});
118+
};
119+
120+
const resolveAddStarMutation = mutationResult => state => {
121+
const {
122+
viewerHasStarred,
123+
} = mutationResult.data.data.addStar.starrable;
124+
125+
const { totalCount } = state.organization.repository.stargazers;
126+
127+
return {
128+
...state,
129+
organization: {
130+
...state.organization,
131+
repository: {
132+
...state.organization.repository,
133+
viewerHasStarred,
134+
stargazers: {
135+
totalCount: totalCount + 1,
136+
},
137+
},
138+
},
139+
};
140+
};
141+
142+
const removeStarFromRepository = repositoryId => {
143+
return axiosGitHubGraphQL.post('', {
144+
query: REMOVE_STAR,
145+
variables: { repositoryId },
146+
});
147+
};
148+
149+
const resolveRemoveStarMutation = mutationResult => state => {
150+
const {
151+
viewerHasStarred,
152+
} = mutationResult.data.data.removeStar.starrable;
153+
154+
const { totalCount } = state.organization.repository.stargazers;
155+
156+
return {
157+
...state,
158+
organization: {
159+
...state.organization,
160+
repository: {
161+
...state.organization.repository,
162+
viewerHasStarred,
163+
stargazers: {
164+
totalCount: totalCount - 1,
165+
},
166+
},
167+
},
168+
};
169+
};
170+
88171
class App extends Component {
89172
state = {
90173
path: 'the-road-to-learn-react/the-road-to-learn-react',
@@ -120,6 +203,18 @@ class App extends Component {
120203
this.onFetchFromGitHub(this.state.path, endCursor);
121204
};
122205

206+
onStarRepository = (repositoryId, viewerHasStarred) => {
207+
if (viewerHasStarred) {
208+
removeStarFromRepository(repositoryId).then(mutationResult =>
209+
this.setState(resolveRemoveStarMutation(mutationResult)),
210+
);
211+
} else {
212+
addStarToRepository(repositoryId).then(mutationResult =>
213+
this.setState(resolveAddStarMutation(mutationResult)),
214+
);
215+
}
216+
};
217+
123218
render() {
124219
const { path, organization, errors } = this.state;
125220

@@ -148,6 +243,7 @@ class App extends Component {
148243
organization={organization}
149244
errors={errors}
150245
onFetchMoreIssues={this.onFetchMoreIssues}
246+
onStarRepository={this.onStarRepository}
151247
/>
152248
) : (
153249
<p>No information yet ...</p>
@@ -161,6 +257,7 @@ const Organization = ({
161257
organization,
162258
errors,
163259
onFetchMoreIssues,
260+
onStarRepository,
164261
}) => {
165262
if (errors) {
166263
return (
@@ -180,18 +277,33 @@ const Organization = ({
180277
<Repository
181278
repository={organization.repository}
182279
onFetchMoreIssues={onFetchMoreIssues}
280+
onStarRepository={onStarRepository}
183281
/>
184282
</div>
185283
);
186284
};
187285

188-
const Repository = ({ repository, onFetchMoreIssues }) => (
286+
const Repository = ({
287+
repository,
288+
onFetchMoreIssues,
289+
onStarRepository,
290+
}) => (
189291
<div>
190292
<p>
191293
<strong>In Repository:</strong>
192294
<a href={repository.url}>{repository.name}</a>
193295
</p>
194296

297+
<button
298+
type="button"
299+
onClick={() =>
300+
onStarRepository(repository.id, repository.viewerHasStarred)
301+
}
302+
>
303+
{repository.stargazers.totalCount}
304+
{repository.viewerHasStarred ? ' Unstar' : ' Star'}
305+
</button>
306+
195307
<ul>
196308
{repository.issues.edges.map(issue => (
197309
<li key={issue.node.id}>

0 commit comments

Comments
 (0)