Skip to content

Commit e2d14d8

Browse files
author
Carlos Rufo Jimenez
committed
8.4-end-subscriptions-v2.1
1 parent 1d32d1a commit e2d14d8

File tree

6 files changed

+188
-8
lines changed

6 files changed

+188
-8
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
"dependencies": {
66
"apollo-client-preset": "^1.0.8",
77
"apollo-link-context": "^1.0.7",
8+
"apollo-link-ws": "^1.0.7",
89
"graphql": "^0.13.1",
910
"graphql-tag": "^2.8.0",
1011
"react": "^16.2.0",
1112
"react-apollo": "^2.1.0",
1213
"react-dom": "^16.2.0",
1314
"react-router": "^4.2.0",
1415
"react-router-dom": "^4.2.2",
15-
"react-scripts": "1.1.1"
16+
"react-scripts": "1.1.1",
17+
"subscriptions-transport-ws": "^0.9.7"
1618
},
1719
"scripts": {
1820
"start": "react-scripts start",

src/components/CreateLink.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const POST_MUTATION = gql`
1010
createdAt
1111
url
1212
description
13+
postedBy {
14+
id
15+
name
16+
}
17+
votes {
18+
id
19+
user {
20+
id
21+
}
22+
}
1323
}
1424
}
1525
`

src/components/LinkList.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import Link from './Link'
33
import { Query } from 'react-apollo'
44
import gql from 'graphql-tag'
5+
import LinkListSubscriptions from './LinkListSubscriptions'
56

67
export const FEED_QUERY = gql`
78
{
@@ -38,14 +39,14 @@ const updateCacheAfterVote = (store, createVote, linkId) => {
3839
export default () => {
3940
return (
4041
<Query query={FEED_QUERY}>
41-
{({ loading, error, data }) => {
42+
{({ loading, error, data, subscribeToMore }) => {
4243
if (loading) return <div>Fetching</div>
4344
if (error) return <div>Error</div>
4445

4546
const linksToRender = data.feed.links
4647

4748
return (
48-
<div>
49+
<LinkListSubscriptions subscribeToMore={subscribeToMore}>
4950
{linksToRender.map((link, index) => (
5051
<Link
5152
key={link.id}
@@ -54,7 +55,7 @@ export default () => {
5455
updateStoreAfterVote={updateCacheAfterVote}
5556
/>
5657
))}
57-
</div>
58+
</LinkListSubscriptions>
5859
)
5960
}}
6061
</Query>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { Component } from 'react'
2+
import gql from 'graphql-tag'
3+
4+
const NEW_LINKS_SUBSCRIPTION = gql`
5+
subscription {
6+
newLink {
7+
node {
8+
id
9+
url
10+
description
11+
createdAt
12+
postedBy {
13+
id
14+
name
15+
}
16+
votes {
17+
id
18+
user {
19+
id
20+
}
21+
}
22+
}
23+
}
24+
}
25+
`
26+
27+
const NEW_VOTES_SUBSCRIPTION = gql`
28+
subscription {
29+
newVote {
30+
node {
31+
id
32+
link {
33+
id
34+
url
35+
description
36+
createdAt
37+
postedBy {
38+
id
39+
name
40+
}
41+
votes {
42+
id
43+
user {
44+
id
45+
}
46+
}
47+
}
48+
user {
49+
id
50+
}
51+
}
52+
}
53+
}
54+
`
55+
56+
class LinkListSubscriptions extends Component {
57+
componentDidMount() {
58+
this._subscribeToNewLinks()
59+
this._subscribeToNewVotes()
60+
}
61+
62+
_subscribeToNewLinks = () => {
63+
this.props.subscribeToMore({
64+
document: NEW_LINKS_SUBSCRIPTION,
65+
updateQuery: (prev, { subscriptionData }) => {
66+
if (!subscriptionData.data) return prev
67+
const newLink = subscriptionData.data.newLink.node
68+
69+
return Object.assign({}, prev, {
70+
feed: {
71+
links: [newLink, ...prev.feed.links],
72+
__typename: prev.feed.__typename
73+
}
74+
})
75+
}
76+
})
77+
}
78+
79+
_subscribeToNewVotes = () => {
80+
this.props.subscribeToMore({
81+
document: NEW_VOTES_SUBSCRIPTION
82+
})
83+
}
84+
85+
render() {
86+
return this.props.children
87+
}
88+
}
89+
90+
export default LinkListSubscriptions

src/index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import { InMemoryCache } from 'apollo-cache-inmemory'
1010
import { BrowserRouter } from 'react-router-dom'
1111
import { AUTH_TOKEN } from './constants'
1212
import { setContext } from 'apollo-link-context'
13+
import { split } from 'apollo-link'
14+
import { WebSocketLink } from 'apollo-link-ws'
15+
import { getMainDefinition } from 'apollo-utilities'
1316

1417
const httpLink = createHttpLink({
15-
uri: 'http://localhost:4000',
18+
uri: 'http://localhost:4000'
1619
})
1720

1821
const authLink = setContext((_, { headers }) => {
@@ -25,8 +28,27 @@ const authLink = setContext((_, { headers }) => {
2528
}
2629
})
2730

31+
const wsLink = new WebSocketLink({
32+
uri: `ws://localhost:4000`,
33+
options: {
34+
reconnect: true,
35+
connectionParams: {
36+
authToken: localStorage.getItem(AUTH_TOKEN)
37+
}
38+
}
39+
})
40+
41+
const link = split(
42+
({ query }) => {
43+
const { kind, operation } = getMainDefinition(query)
44+
return kind === 'OperationDefinition' && operation === 'subscription'
45+
},
46+
wsLink,
47+
authLink.concat(httpLink)
48+
)
49+
2850
const client = new ApolloClient({
29-
link: authLink.concat(httpLink),
51+
link,
3052
cache: new InMemoryCache()
3153
})
3254

yarn.lock

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ apollo-link-http@^1.3.1:
224224
apollo-link "^1.2.1"
225225
apollo-link-http-common "^0.2.2"
226226

227+
apollo-link-ws@^1.0.7:
228+
version "1.0.7"
229+
resolved "https://registry.yarnpkg.com/apollo-link-ws/-/apollo-link-ws-1.0.7.tgz#6cc3903cbfbbefe213ea9fc8121f5fed3cef1823"
230+
dependencies:
231+
apollo-link "^1.2.1"
232+
227233
apollo-link@^1.0.0, apollo-link@^1.0.6, apollo-link@^1.2.1:
228234
version "1.2.1"
229235
resolved "https://registry.yarnpkg.com/apollo-link/-/apollo-link-1.2.1.tgz#c120b16059f9bd93401b9f72b94d2f80f3f305d2"
@@ -383,6 +389,10 @@ async-each@^1.0.0:
383389
version "1.0.1"
384390
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"
385391

392+
async-limiter@~1.0.0:
393+
version "1.0.0"
394+
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
395+
386396
async@^1.4.0, async@^1.5.2:
387397
version "1.5.2"
388398
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
@@ -1076,6 +1086,10 @@ babylon@^6.17.0, babylon@^6.18.0:
10761086
version "6.18.0"
10771087
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3"
10781088

1089+
backo2@^1.0.2:
1090+
version "1.0.2"
1091+
resolved "https://registry.yarnpkg.com/backo2/-/backo2-1.0.2.tgz#31ab1ac8b129363463e35b3ebb69f4dfcfba7947"
1092+
10791093
balanced-match@^0.4.2:
10801094
version "0.4.2"
10811095
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838"
@@ -2547,6 +2561,10 @@ [email protected]:
25472561
version "1.2.0"
25482562
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-1.2.0.tgz#1c86991d816ad1e504750e73874224ecf3bec508"
25492563

2564+
eventemitter3@^2.0.3:
2565+
version "2.0.3"
2566+
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-2.0.3.tgz#b5e1079b59fb5e1ba2771c0a993be060a58c99ba"
2567+
25502568
events@^1.0.0:
25512569
version "1.1.1"
25522570
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
@@ -3910,7 +3928,7 @@ istanbul-reports@^1.1.4:
39103928
dependencies:
39113929
handlebars "^4.0.3"
39123930

3913-
iterall@^1.2.0:
3931+
iterall@^1.2.0, iterall@^1.2.1:
39143932
version "1.2.2"
39153933
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7"
39163934

@@ -4369,6 +4387,10 @@ lodash._reinterpolate@~3.0.0:
43694387
version "3.0.0"
43704388
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
43714389

4390+
lodash.assign@^4.2.0:
4391+
version "4.2.0"
4392+
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"
4393+
43724394
lodash.camelcase@^4.3.0:
43734395
version "4.3.0"
43744396
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
@@ -4381,6 +4403,14 @@ lodash.defaults@^4.2.0:
43814403
version "4.2.0"
43824404
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
43834405

4406+
lodash.isobject@^3.0.2:
4407+
version "3.0.2"
4408+
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"
4409+
4410+
lodash.isstring@^4.0.1:
4411+
version "4.0.1"
4412+
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
4413+
43844414
lodash.memoize@^4.1.2:
43854415
version "4.1.2"
43864416
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
@@ -6579,6 +6609,19 @@ [email protected]:
65796609
loader-utils "^1.0.2"
65806610
schema-utils "^0.3.0"
65816611

6612+
subscriptions-transport-ws@^0.9.7:
6613+
version "0.9.7"
6614+
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.7.tgz#9be456d6e188c96a53c91f570850e59bee5840ac"
6615+
dependencies:
6616+
backo2 "^1.0.2"
6617+
eventemitter3 "^2.0.3"
6618+
iterall "^1.2.1"
6619+
lodash.assign "^4.2.0"
6620+
lodash.isobject "^3.0.2"
6621+
lodash.isstring "^4.0.1"
6622+
symbol-observable "^1.0.4"
6623+
ws "^3.0.0"
6624+
65826625
supports-color@^2.0.0:
65836626
version "2.0.0"
65846627
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
@@ -6643,7 +6686,7 @@ sw-toolbox@^3.4.0:
66436686
path-to-regexp "^1.0.1"
66446687
serviceworker-cache-polyfill "^4.0.0"
66456688

6646-
symbol-observable@^1.0.2:
6689+
symbol-observable@^1.0.2, symbol-observable@^1.0.4:
66476690
version "1.2.0"
66486691
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
66496692

@@ -6862,6 +6905,10 @@ uid-number@^0.0.6:
68626905
version "0.0.6"
68636906
resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"
68646907

6908+
ultron@~1.1.0:
6909+
version "1.1.1"
6910+
resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c"
6911+
68656912
union-value@^1.0.0:
68666913
version "1.0.0"
68676914
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4"
@@ -7273,6 +7320,14 @@ write@^0.2.1:
72737320
dependencies:
72747321
mkdirp "^0.5.1"
72757322

7323+
ws@^3.0.0:
7324+
version "3.3.3"
7325+
resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.3.tgz#f1cf84fe2d5e901ebce94efaece785f187a228f2"
7326+
dependencies:
7327+
async-limiter "~1.0.0"
7328+
safe-buffer "~5.1.0"
7329+
ultron "~1.1.0"
7330+
72767331
xdg-basedir@^3.0.0:
72777332
version "3.0.0"
72787333
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"

0 commit comments

Comments
 (0)