Skip to content

Commit 994e606

Browse files
committed
refactor Mutation and Query components
1 parent 78232be commit 994e606

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

example/src/App.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,22 @@ const resolveFetchMore = (data, state) => {
6363
};
6464

6565
const resolveWatchMutation = (data, state) => {
66-
const { totalCount } = state.data.updateSubscription.subscribable;
66+
const { totalCount } = state.data.repository;
6767
const { viewerSubscription } = data.updateSubscription.subscribable;
6868

6969
return {
70-
updateSubscription: {
71-
subscribable: {
72-
viewerSubscription,
73-
totalCount:
74-
viewerSubscription === 'SUBSCRIBED'
75-
? totalCount + 1
76-
: totalCount - 1,
77-
},
70+
repository: {
71+
viewerSubscription,
72+
totalCount:
73+
viewerSubscription === 'SUBSCRIBED'
74+
? totalCount + 1
75+
: totalCount - 1,
7876
},
7977
};
8078
};
8179

82-
const isWatch = updateSubscription =>
83-
updateSubscription.subscribable.viewerSubscription === 'SUBSCRIBED';
80+
const isWatch = viewerSubscription =>
81+
viewerSubscription === 'SUBSCRIBED';
8482

8583
class App extends Component {
8684
state = {
@@ -191,12 +189,10 @@ const Repositories = ({ repositories, onFetchMoreRepositories }) => (
191189
<Mutation
192190
mutation={WATCH_REPOSITORY}
193191
initial={{
194-
updateSubscription: {
195-
subscribable: {
196-
viewerSubscription:
197-
repository.node.viewerSubscription,
198-
totalCount: repository.node.watchers.totalCount,
199-
},
192+
repository: {
193+
viewerSubscription:
194+
repository.node.viewerSubscription,
195+
totalCount: repository.node.watchers.totalCount,
200196
},
201197
}}
202198
resolveMutation={resolveWatchMutation}
@@ -209,16 +205,16 @@ const Repositories = ({ repositories, onFetchMoreRepositories }) => (
209205
variables: {
210206
id: repository.node.id,
211207
viewerSubscription: isWatch(
212-
data.updateSubscription,
208+
data.repository.viewerSubscription,
213209
)
214210
? 'UNSUBSCRIBED'
215211
: 'SUBSCRIBED',
216212
},
217213
})
218214
}
219215
>
220-
{data.updateSubscription.subscribable.totalCount}
221-
{isWatch(data.updateSubscription)
216+
{data.repository.totalCount}
217+
{isWatch(data.repository.viewerSubscription)
222218
? ' Unwatch'
223219
: ' Watch'}
224220
</button>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-graphql-client",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"main": "lib/index.js",
55
"peerDependencies": {
66
"react": "^16.3.0",

src/my-client-react/Mutation.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Mutation extends React.Component {
88
super(props);
99

1010
this.state = {
11-
data: this.props.initial || null,
11+
data: this.props.initial,
1212
loading: null,
1313
errors: null,
1414
};
@@ -29,19 +29,11 @@ class Mutation extends React.Component {
2929
client
3030
.mutate({ mutation, variables })
3131
.then(result => {
32-
if (resolveMutation) {
33-
this.setState(state => ({
34-
data: resolveMutation(result.data.data, state),
35-
errors: result.data.errors,
36-
loading: false,
37-
}));
38-
} else {
39-
this.setState({
40-
data: result.data.data,
41-
errors: result.data.errors,
42-
loading: false,
43-
});
44-
}
32+
this.setState(state => ({
33+
data: resolveMutation(result.data.data, state),
34+
errors: result.data.errors,
35+
loading: false,
36+
}));
4537
})
4638
.catch(error =>
4739
this.setState({

src/my-client-react/Query.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ class Query extends React.Component {
4343
);
4444
};
4545

46-
fetchMore = ({ query, variables }) => {
47-
this.queryMore({ query, variables });
48-
};
49-
5046
queryMore = ({ query, variables }) => {
5147
this.props.client
5248
.query({ query, variables })
@@ -68,7 +64,7 @@ class Query extends React.Component {
6864
render() {
6965
return this.props.children({
7066
...this.state,
71-
fetchMore: this.fetchMore,
67+
fetchMore: this.queryMore,
7268
});
7369
}
7470
}

0 commit comments

Comments
 (0)