Skip to content

Instantly share code, notes, and snippets.

@gHashTag
Created December 20, 2017 19:15
Show Gist options
  • Save gHashTag/10af5b54a6ab299b5bddfafa3f08badd to your computer and use it in GitHub Desktop.
Save gHashTag/10af5b54a6ab299b5bddfafa3f08badd to your computer and use it in GitHub Desktop.
/* eslint-disable no-param-reassign */
import { createStore, applyMiddleware } from 'redux'
import { AsyncStorage } from 'react-native'
import { composeWithDevTools } from 'redux-devtools-extension'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
import thunk from 'redux-thunk'
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'
import reducers from './reducers'
const wsClient = new SubscriptionClient('ws://myserver.herokuapp.com/subscriptions', {
reconnect: true,
connectionParams: {}
})
const networkInterface = createNetworkInterface({
uri: 'https://myserver.herokuapp.com/graphql'
})
networkInterface.use([{
async applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {}
}
try {
const token = await AsyncStorage.getItem('@secretsecret')
if (token != null) {
req.options.headers.authorization = `Bearer ${token}` || null
}
} catch (error) {
throw error
}
return next()
}
}])
const networkInterfaceWithSubs = addGraphQLSubscriptions(
networkInterface,
wsClient
)
export const client = new ApolloClient({
networkInterface: networkInterfaceWithSubs
})
const middlewares = [client.middleware(), thunk]
export const store = createStore(
reducers(client),
undefined,
composeWithDevTools(applyMiddleware(...middlewares)),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment