Skip to content

Commit

Permalink
add subscriptions functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaash committed Sep 26, 2017
1 parent 9885b76 commit 1310797
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -58,6 +58,7 @@
"react-router-dom": "^4.1.1",
"react-toolbox": "^2.0.0-beta.12",
"style-loader": "^0.13.1",
"subscriptions-transport-ws": "0.8.3",
"tslib": "^1.7.1",
"typescript": "^2.2.1",
"url-loader": "^0.5.7",
Expand Down
24 changes: 21 additions & 3 deletions src/app/app.store.ts
@@ -1,18 +1,36 @@
import { observable, action } from 'mobx'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'

const PROJECT_ID = 'cj3bf7docbo5w0147sj4e66ik'

const wsClient = new SubscriptionClient(`wss://subscriptions.graph.cool/v1/${PROJECT_ID}`, {
reconnect: true,
connectionParams: {
// Pass any arguments you want for initialization
}
})

const networkInterface = createNetworkInterface({
uri: `https://api.graph.cool/simple/v1/${PROJECT_ID}`
})

const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient
)

export class AppStore {
static instance: AppStore
@observable username = 'Mr. User'
apolloClient = new ApolloClient({
networkInterface: createNetworkInterface({
uri: 'https://api.graph.cool/simple/v1/cj3bf7docbo5w0147sj4e66ik'
})
networkInterface: networkInterfaceWithSubscriptions
})

static getInstance() {
return AppStore.instance || (AppStore.instance = new AppStore())
}

@action onUsernameChange = (val) => {
this.username = val
}
Expand Down
4 changes: 4 additions & 0 deletions src/app/posts/posts.component.tsx
Expand Up @@ -18,6 +18,10 @@ export class Posts extends React.Component<any, any> {
this.postsStore.initializePosts()
}

componentWillUnmount() {
this.postsStore.destructor()
}

render() {
return <Provider postsStore={this.postsStore}>
<PostsComponent />
Expand Down
34 changes: 34 additions & 0 deletions src/app/posts/posts.store.ts
Expand Up @@ -14,6 +14,24 @@ const PostsQuery = gql`
}
`

const PostsSubscription = gql`
subscription PostsSubscription($name: String!){
Post(filter: {
mutation_in: [CREATED],
node: {
name: $name
}
}) {
node {
id,
name,
title,
message
}
}
}
`

interface Post {
id: string
name: string
Expand All @@ -27,11 +45,27 @@ interface PostsQueryResult {

export class PostsStore {
appStore: AppStore
postSubscription

@observable posts: Array<Post> = []

constructor() {
let self = this
this.appStore = AppStore.getInstance()
this.postSubscription = this.appStore.apolloClient.subscribe({
query: PostsSubscription,
// This way realtime updates will work only when both posting and reading users have the same name. Proof of concept.
variables: { name: this.appStore.username }
}).subscribe({
next(data) {
self.posts.unshift(data.Post.node)
},
error(err) { console.error('err', err) },
})
}

destructor() {
this.postSubscription.unsubscribe()
}

async initializePosts() {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -13,6 +13,7 @@
"removeComments": true,
"strictNullChecks": false,
"importHelpers": true,
"skipLibCheck": true,
"outDir": "build",
"lib": [
"es6",
Expand Down
75 changes: 71 additions & 4 deletions yarn.lock
Expand Up @@ -10,7 +10,7 @@
version "0.0.32"
resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-0.0.32.tgz#449abcd9a826807811ef101e58df9f83cfc61713"

"@types/graphql@^0.9.0":
"@types/graphql@^0.9.0", "@types/graphql@^0.9.1":
version "0.9.1"
resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-0.9.1.tgz#b04ebe84bc997cc60dbea2ed4d0d4342c737f99d"

Expand Down Expand Up @@ -247,6 +247,10 @@ async-each@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"

async-limiter@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"

async@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
Expand Down Expand Up @@ -304,6 +308,10 @@ babel-code-frame@^6.11.0, babel-code-frame@^6.22.0:
esutils "^2.0.2"
js-tokens "^3.0.0"

backo2@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/backo2/-/backo2-1.0.2.tgz#31ab1ac8b129363463e35b3ebb69f4dfcfba7947"

balanced-match@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.1.0.tgz#b504bd05869b39259dd0c5efc35d843176dccc4a"
Expand Down Expand Up @@ -1083,6 +1091,10 @@ error-ex@^1.2.0:
dependencies:
is-arrayish "^0.2.1"

es6-promise@^4.0.5:
version "4.1.1"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.1.1.tgz#8811e90915d9a0dba36274f0b242dbda78f9c92a"

es6-templates@^0.2.2:
version "0.2.3"
resolved "https://registry.yarnpkg.com/es6-templates/-/es6-templates-0.2.3.tgz#5cb9ac9fb1ded6eb1239342b81d792bbb4078ee4"
Expand Down Expand Up @@ -1122,6 +1134,10 @@ eventemitter3@1.x.x:
version "1.2.0"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-1.2.0.tgz#1c86991d816ad1e504750e73874224ecf3bec508"

eventemitter3@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-2.0.3.tgz#b5e1079b59fb5e1ba2771c0a993be060a58c99ba"

events@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
Expand Down Expand Up @@ -1411,10 +1427,22 @@ graphql-anywhere@^3.0.1:
version "3.1.0"
resolved "https://registry.yarnpkg.com/graphql-anywhere/-/graphql-anywhere-3.1.0.tgz#3ea0d8e8646b5cee68035016a9a7557c15c21e96"

graphql-subscriptions@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-0.4.4.tgz#39cff32d08dd3c990113864bab77154403727e9b"
dependencies:
"@types/graphql" "^0.9.1"
es6-promise "^4.0.5"
iterall "^1.1.1"

graphql-tag@^2.0.0, graphql-tag@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.2.2.tgz#2b16175a4d0b3ed43ae8d53d94b9b6e01bbb4905"

graphql-tag@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.4.2.tgz#6a63297d8522d03a2b72d26f1b239aab343840cd"

graphql@^0.10.0:
version "0.10.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.10.1.tgz#75c93c2ce73aeb5bae2eefb555a8e9e39c36027d"
Expand Down Expand Up @@ -1816,7 +1844,7 @@ isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"

iterall@^1.1.0:
iterall@^1.1.0, iterall@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.1.tgz#f7f0af11e9a04ec6426260f5019d9fcca4d50214"

Expand Down Expand Up @@ -1950,10 +1978,22 @@ lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"

lodash.assign@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"

lodash.camelcase@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"

lodash.isobject@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"

lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"

lodash.memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
Expand Down Expand Up @@ -3446,7 +3486,7 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^2.0.0"
inherits "^2.0.1"

safe-buffer@^5.0.1, safe-buffer@^5.1.0:
safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@~5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.0.tgz#fe4c8460397f9eaaaa58e73be46273408a45e223"

Expand Down Expand Up @@ -3725,6 +3765,21 @@ style-loader@^0.13.1:
dependencies:
loader-utils "^1.0.2"

subscriptions-transport-ws@0.8.3:
version "0.8.3"
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.8.3.tgz#d4b22db7f5951bd30e7c6f4dc4774d34ed9f1751"
dependencies:
backo2 "^1.0.2"
eventemitter3 "^2.0.3"
graphql-subscriptions "^0.4.4"
graphql-tag "^2.4.2"
iterall "^1.1.1"
lodash.assign "^4.2.0"
lodash.isobject "^3.0.2"
lodash.isstring "^4.0.1"
symbol-observable "^1.0.4"
ws "^3.0.0"

supports-color@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
Expand All @@ -3747,7 +3802,7 @@ svgo@^0.7.0:
sax "~1.2.1"
whet.extend "~0.9.9"

symbol-observable@^1.0.2:
symbol-observable@^1.0.2, symbol-observable@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.4.tgz#29bf615d4aa7121bdd898b22d4b3f9bc4e2aa03d"

Expand Down Expand Up @@ -3906,6 +3961,10 @@ uid-number@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"

ultron@~1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.0.tgz#b07a2e6a541a815fc6a34ccd4533baec307ca864"

uniq@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/uniq/-/uniq-1.0.1.tgz#b31c5ae8254844a3a8281541ce2b04b865a734ff"
Expand Down Expand Up @@ -4179,6 +4238,14 @@ wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

ws@^3.0.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-3.2.0.tgz#d5d3d6b11aff71e73f808f40cc69d52bb6d4a185"
dependencies:
async-limiter "~1.0.0"
safe-buffer "~5.1.0"
ultron "~1.1.0"

xml-char-classes@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/xml-char-classes/-/xml-char-classes-1.0.0.tgz#64657848a20ffc5df583a42ad8a277b4512bbc4d"
Expand Down

0 comments on commit 1310797

Please sign in to comment.