Skip to content

Commit 1705889

Browse files
committed
Add STARS_SUBSCRIPTION
1 parent 681907b commit 1705889

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

src/components/StarCount.js

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,44 @@ import gql from 'graphql-tag'
55
import classNames from 'classnames'
66
import Odometer from 'react-odometerjs'
77

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+
}
1740
}
1841

1942
StarCount.propTypes = {
2043
githubStars: PropTypes.number,
21-
loading: PropTypes.bool.isRequired
44+
loading: PropTypes.bool.isRequired,
45+
subscribeToMore: PropTypes.func.isRequired
2246
}
2347

2448
const STARS_QUERY = gql`
@@ -27,10 +51,20 @@ const STARS_QUERY = gql`
2751
}
2852
`
2953

54+
const STARS_SUBSCRIPTION = gql`
55+
subscription StarsSubscription {
56+
githubStars
57+
}
58+
`
59+
3060
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+
/>
3468
)}
3569
</Query>
3670
)

src/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@ import registerServiceWorker from './registerServiceWorker'
66
import { ApolloClient } from 'apollo-client'
77
import { ApolloProvider } from 'react-apollo'
88
import { InMemoryCache } from 'apollo-cache-inmemory'
9+
import { split } from 'apollo-link'
10+
import { WebSocketLink } from 'apollo-link-ws'
911
import { createHttpLink } from 'apollo-link-http'
12+
import { getMainDefinition } from 'apollo-utilities'
1013

11-
const link = createHttpLink({
14+
const httpLink = createHttpLink({
1215
uri: 'https://api.graphql.guide/graphql'
1316
})
1417

18+
const wsLink = new WebSocketLink({
19+
uri: `wss://api.graphql.guide/subscriptions`,
20+
options: {
21+
reconnect: true
22+
}
23+
})
24+
25+
const link = split(
26+
({ query }) => {
27+
const { kind, operation } = getMainDefinition(query)
28+
return kind === 'OperationDefinition' && operation === 'subscription'
29+
},
30+
wsLink,
31+
httpLink
32+
)
33+
1534
const cache = new InMemoryCache()
1635

1736
const client = new ApolloClient({ link, cache })

0 commit comments

Comments
 (0)