@@ -5,20 +5,44 @@ import gql from 'graphql-tag'
5
5
import classNames from 'classnames'
6
6
import Odometer from 'react-odometerjs'
7
7
8
- const StarCount = ( { githubStars, loading } ) => {
9
- return (
10
- < a
11
- className = { classNames ( 'StarCount' , { loading } ) }
12
- href = "https://github.com/GraphQLGuide/guide"
13
- >
14
- { githubStars && < Odometer value = { githubStars } /> }
15
- </ a >
16
- )
8
+ class StarCount extends React . Component {
9
+ constructor ( props ) {
10
+ super ( props )
11
+ this . state = { }
12
+ }
13
+
14
+ componentDidMount ( ) {
15
+ this . props . subscribeToMore ( {
16
+ document : STARS_SUBSCRIPTION ,
17
+ updateQuery : (
18
+ previousResult ,
19
+ {
20
+ subscriptionData : {
21
+ data : { githubStars }
22
+ }
23
+ }
24
+ ) => ( { githubStars } )
25
+ } )
26
+ }
27
+
28
+ render ( ) {
29
+ const { githubStars, loading } = this . props
30
+
31
+ return (
32
+ < a
33
+ className = { classNames ( 'StarCount' , { loading } ) }
34
+ href = "https://github.com/GraphQLGuide/guide"
35
+ >
36
+ { githubStars && < Odometer value = { githubStars } /> }
37
+ </ a >
38
+ )
39
+ }
17
40
}
18
41
19
42
StarCount . propTypes = {
20
43
githubStars : PropTypes . number ,
21
- loading : PropTypes . bool . isRequired
44
+ loading : PropTypes . bool . isRequired ,
45
+ subscribeToMore : PropTypes . func . isRequired
22
46
}
23
47
24
48
const STARS_QUERY = gql `
@@ -27,10 +51,20 @@ const STARS_QUERY = gql`
27
51
}
28
52
`
29
53
54
+ const STARS_SUBSCRIPTION = gql `
55
+ subscription StarsSubscription {
56
+ githubStars
57
+ }
58
+ `
59
+
30
60
export default ( ) => (
31
- < Query query = { STARS_QUERY } pollInterval = { 5 * 1000 } >
32
- { ( { data : { githubStars } , loading } ) => (
33
- < StarCount githubStars = { githubStars } loading = { loading } />
61
+ < Query query = { STARS_QUERY } >
62
+ { ( { data : { githubStars } , loading, subscribeToMore } ) => (
63
+ < StarCount
64
+ githubStars = { githubStars }
65
+ loading = { loading }
66
+ subscribeToMore = { subscribeToMore }
67
+ />
34
68
) }
35
69
</ Query >
36
70
)
0 commit comments