Skip to content

Commit

Permalink
feat(rn): setup relay subscriptions
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Aug 22, 2018
1 parent 89f0f9f commit fde13d5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
18 changes: 17 additions & 1 deletion client/react-native/common/relay.js
@@ -1,4 +1,5 @@
import { Environment, Network, RecordSource, Store } from 'relay-runtime'
import { SubscriptionClient } from 'subscriptions-transport-ws'
import fetch from 'isomorphic-fetch'

// Define a function that fetches the results of an operation (query/mutation/etc)
Expand All @@ -22,8 +23,23 @@ const fetchQuery = async (operation, variables) => {
}
}

// eslint-disable-next-line
const setupSubscription = (config, variables, cacheConfig, observer) => {
const query = config.text

const subscriptionClient = new SubscriptionClient('ws://localhost:8700', {
reconnect: true,
})
subscriptionClient.subscribe({ query, variables }, (error, result) => {
if (error != null) {
console.error(error)
}
observer.onNext({ data: result })
})
}

// Create a network layer from the fetch function
const network = Network.create(fetchQuery)
const network = Network.create(fetchQuery, setupSubscription)
const store = new Store(new RecordSource())

const environment = new Environment({
Expand Down
3 changes: 2 additions & 1 deletion client/react-native/package.json
Expand Up @@ -13,7 +13,8 @@
"react-native-vector-icons": "^5.0.0",
"react-native-web": "^0.8.9",
"react-relay": "^1.6.2",
"relay-compiler": "^1.6.2"
"relay-compiler": "^1.6.2",
"subscriptions-transport-ws": "^0.9.14"
},
"devDependencies": {
"@babel/core": "7.0.0-rc.1",
Expand Down
26 changes: 25 additions & 1 deletion client/react-native/yarn.lock
Expand Up @@ -2390,6 +2390,10 @@ babylon@^6.18.0:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3"

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

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
Expand Down Expand Up @@ -3283,7 +3287,7 @@ event-target-shim@^1.0.5:
version "1.1.1"
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-1.1.1.tgz#a86e5ee6bdaa16054475da797ccddf0c55698491"

eventemitter3@^3.0.0:
eventemitter3@^3.0.0, eventemitter3@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.0.tgz#090b4d6cdbd645ed10bf750d4b5407942d7ba163"

Expand Down Expand Up @@ -6057,6 +6061,16 @@ strip-json-comments@^2.0.1, strip-json-comments@~2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"

subscriptions-transport-ws@^0.9.14:
version "0.9.14"
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.14.tgz#a39e08edba89ee4cfd95f30484c55d865f5d8451"
dependencies:
backo2 "^1.0.2"
eventemitter3 "^3.1.0"
iterall "^1.2.1"
symbol-observable "^1.0.4"
ws "^5.2.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 @@ -6077,6 +6091,10 @@ symbol-observable@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.1.tgz#8340fc4702c3122df5d22288f88283f513d3fdd4"

symbol-observable@^1.0.4:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"

table@4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/table/-/table-4.0.2.tgz#a33447375391e766ad34d3486e6e2aedc84d2e36"
Expand Down Expand Up @@ -6393,6 +6411,12 @@ ws@^3.3.1:
safe-buffer "~5.1.0"
ultron "~1.1.0"

ws@^5.2.0:
version "5.2.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-5.2.2.tgz#dffef14866b8e8dc9133582514d1befaf96e980f"
dependencies:
async-limiter "~1.0.0"

xcode@^0.9.1:
version "0.9.3"
resolved "https://registry.yarnpkg.com/xcode/-/xcode-0.9.3.tgz#910a89c16aee6cc0b42ca805a6d0b4cf87211cf3"
Expand Down

0 comments on commit fde13d5

Please sign in to comment.