Skip to content

Commit feb2b79

Browse files
committed
05-GraphQL Mutation with Apollo Client in React
1 parent fa06945 commit feb2b79

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

src/Button/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
3+
import './style.css';
4+
5+
const Button = ({
6+
children,
7+
className,
8+
color = 'black',
9+
type = 'button',
10+
...props
11+
}) => (
12+
<button
13+
className={`${className} Button Button_${color}`}
14+
type={type}
15+
{...props}
16+
>
17+
{children}
18+
</button>
19+
);
20+
21+
export default Button;

src/Repository/RepositoryItem/index.js

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import React from 'react';
2+
import { Mutation } from 'react-apollo';
23

34
import Link from '../../Link';
5+
import Button from '../../Button';
46

57
import '../style.css';
68

9+
import {
10+
STAR_REPOSITORY,
11+
UNSTAR_REPOSITORY,
12+
WATCH_REPOSITORY,
13+
} from '../mutations';
14+
15+
const VIEWER_SUBSCRIPTIONS = {
16+
SUBSCRIBED: 'SUBSCRIBED',
17+
UNSUBSCRIBED: 'UNSUBSCRIBED',
18+
};
19+
20+
const isWatch = viewerSubscription =>
21+
viewerSubscription === VIEWER_SUBSCRIPTIONS.SUBSCRIBED;
22+
723
const RepositoryItem = ({
24+
id,
825
name,
926
url,
1027
descriptionHTML,
@@ -21,8 +38,53 @@ const RepositoryItem = ({
2138
<Link href={url}>{name}</Link>
2239
</h2>
2340

24-
<div className="RepositoryItem-title-action">
25-
{stargazers.totalCount} Stars
41+
<div>
42+
<Mutation
43+
mutation={WATCH_REPOSITORY}
44+
variables={{
45+
id,
46+
viewerSubscription: isWatch(viewerSubscription)
47+
? VIEWER_SUBSCRIPTIONS.UNSUBSCRIBED
48+
: VIEWER_SUBSCRIPTIONS.SUBSCRIBED,
49+
}}
50+
>
51+
{(updateSubscription, { data, loading, error }) => (
52+
<Button
53+
className="RepositoryItem-title-action"
54+
data-test-id="updateSubscription"
55+
onClick={updateSubscription}
56+
>
57+
{watchers.totalCount}{' '}
58+
{isWatch(viewerSubscription) ? 'Unwatch' : 'Watch'}
59+
</Button>
60+
)}
61+
</Mutation>
62+
63+
{!viewerHasStarred ? (
64+
<Mutation mutation={STAR_REPOSITORY} variables={{ id }}>
65+
{(addStar, { data, loading, error }) => (
66+
<Button
67+
className={'RepositoryItem-title-action'}
68+
onClick={addStar}
69+
>
70+
{stargazers.totalCount} Star
71+
</Button>
72+
)}
73+
</Mutation>
74+
) : (
75+
<Mutation mutation={UNSTAR_REPOSITORY} variables={{ id }}>
76+
{(removeStar, { data, loading, error }) => (
77+
<Button
78+
className="RepositoryItem-title-action"
79+
onClick={removeStar}
80+
>
81+
{stargazers.totalCount} Unstar
82+
</Button>
83+
)}
84+
</Mutation>
85+
)}
86+
87+
{/* Here comes your updateSubscription mutation */}
2688
</div>
2789
</div>
2890

src/Repository/mutations.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import gql from 'graphql-tag';
2+
3+
export const STAR_REPOSITORY = gql`
4+
mutation($id: ID!) {
5+
addStar(input: { starrableId: $id }) {
6+
starrable {
7+
id
8+
viewerHasStarred
9+
}
10+
}
11+
}
12+
`;
13+
14+
export const UNSTAR_REPOSITORY = gql`
15+
mutation($id: ID!) {
16+
removeStar(input: { starrableId: $id }) {
17+
starrable {
18+
id
19+
viewerHasStarred
20+
}
21+
}
22+
}
23+
`;
24+
25+
export const WATCH_REPOSITORY = gql`
26+
mutation($id: ID!, $viewerSubscription: SubscriptionState!) {
27+
updateSubscription(
28+
input: { state: $viewerSubscription, subscribableId: $id }
29+
) {
30+
subscribable {
31+
id
32+
viewerSubscription
33+
}
34+
}
35+
}
36+
`;

0 commit comments

Comments
 (0)