Skip to content

Commit 9ae7455

Browse files
author
Carlos Rufo Jimenez
committed
3.3-end-mutations-v2.1
1 parent c9f8b0b commit 9ae7455

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

src/components/CreateLink.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,53 @@
11
import React, { Component } from 'react'
2-
import { graphql } from 'react-apollo'
2+
import { Mutation } from 'react-apollo'
33
import gql from 'graphql-tag'
44

5+
// 1
6+
const POST_MUTATION = gql`
7+
# 2
8+
mutation PostMutation($description: String!, $url: String!) {
9+
post(description: $description, url: $url) {
10+
id
11+
createdAt
12+
url
13+
description
14+
}
15+
}
16+
`
17+
518
class CreateLink extends Component {
619
state = {
720
description: '',
821
url: '',
922
}
1023

11-
_createLink = async () => {
12-
const { description, url } = this.state
13-
await this.props.postMutation({
14-
variables: {
15-
description,
16-
url
17-
}
18-
})
19-
}
20-
2124
render() {
25+
const { description, url } = this.state
2226
return (
2327
<div>
2428
<div className="flex flex-column mt3">
2529
<input
2630
className="mb2"
27-
value={this.state.description}
31+
value={description}
2832
onChange={e => this.setState({ description: e.target.value })}
2933
type="text"
3034
placeholder="A description for the link"
3135
/>
3236
<input
3337
className="mb2"
34-
value={this.state.url}
38+
value={url}
3539
onChange={e => this.setState({ url: e.target.value })}
3640
type="text"
3741
placeholder="The URL for the link"
3842
/>
3943
</div>
40-
<button onClick={() => this._createLink()}>Submit</button>
44+
<Mutation mutation={POST_MUTATION} variables={{ description, url }}>
45+
{postMutation => <button onClick={postMutation}>Submit</button>}
46+
</Mutation>
4147
</div>
4248
)
4349
}
4450
}
4551

46-
// 1
47-
const POST_MUTATION = gql`
48-
# 2
49-
mutation PostMutation($description: String!, $url: String!) {
50-
post(description: $description, url: $url) {
51-
id
52-
createdAt
53-
url
54-
description
55-
}
56-
}
57-
`
58-
5952
// 3
60-
export default graphql(POST_MUTATION, { name: 'postMutation' })(CreateLink)
53+
export default CreateLink;

0 commit comments

Comments
 (0)