1
1
import React , { Component } from 'react'
2
- import { graphql } from 'react-apollo'
2
+ import { Mutation } from 'react-apollo'
3
3
import gql from 'graphql-tag'
4
4
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
+
5
18
class CreateLink extends Component {
6
19
state = {
7
20
description : '' ,
8
21
url : '' ,
9
22
}
10
23
11
- _createLink = async ( ) => {
12
- const { description, url } = this . state
13
- await this . props . postMutation ( {
14
- variables : {
15
- description,
16
- url
17
- }
18
- } )
19
- }
20
-
21
24
render ( ) {
25
+ const { description, url } = this . state
22
26
return (
23
27
< div >
24
28
< div className = "flex flex-column mt3" >
25
29
< input
26
30
className = "mb2"
27
- value = { this . state . description }
31
+ value = { description }
28
32
onChange = { e => this . setState ( { description : e . target . value } ) }
29
33
type = "text"
30
34
placeholder = "A description for the link"
31
35
/>
32
36
< input
33
37
className = "mb2"
34
- value = { this . state . url }
38
+ value = { url }
35
39
onChange = { e => this . setState ( { url : e . target . value } ) }
36
40
type = "text"
37
41
placeholder = "The URL for the link"
38
42
/>
39
43
</ 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 >
41
47
</ div >
42
48
)
43
49
}
44
50
}
45
51
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
-
59
52
// 3
60
- export default graphql ( POST_MUTATION , { name : 'postMutation' } ) ( CreateLink )
53
+ export default CreateLink ;
0 commit comments