Skip to content

Instantly share code, notes, and snippets.

@guzmonne
Created March 27, 2017 01:16
Show Gist options
  • Select an option

  • Save guzmonne/f25a4a7e607bb91c62ae02b93f90c9f0 to your computer and use it in GitHub Desktop.

Select an option

Save guzmonne/f25a4a7e607bb91c62ae02b93f90c9f0 to your computer and use it in GitHub Desktop.
Setting up Apollo and Redux
// ---------------------------
// Apollo Client Configuration
// ---------------------------
import {ApolloClient, createNetworkInterface} from 'react-apollo'
export const client = new ApolloClient({
networkInterface: createNetworkInterface({
uri: 'http://localhost:3000/graphql'
}),
addTypename: true,
dataIdFromObject: (result) => (
result._id && result.__typename
? result.__typename + result._id
: null
),
})
// ------------------
// Main reducers file
// ------------------
import {createStore, applyMiddleware, compose} from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './reducers.js'
import {client} from './apollo.js'
const configureStore = preloadedState => {
const composeEnhancers = (
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
)
const store = createStore(
rootReducer,
preloadedState,
composeEnhancers(
applyMiddleware(client.middleware(), thunk)
)
)
if (module.hot) {
// Enable Webpack hot module replacement for reducers.
module.hot.accept('./reducers.js', () => {
const nextRootReducer = require('./reducers.js').default
store.replaceReducer(nextRootReducer)
})
}
return store
}
export default configureStore
// -----------------------
// configureStore function
// -----------------------
import {createStore, applyMiddleware, compose} from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './reducers.js'
import {client} from './apollo.js'
const configureStore = preloadedState => {
const composeEnhancers = (
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
)
const store = createStore(
rootReducer,
preloadedState,
composeEnhancers(
applyMiddleware(client.middleware(), thunk)
)
)
if (module.hot) {
// Enable Webpack hot module replacement for reducers.
module.hot.accept('./reducers.js', () => {
const nextRootReducer = require('./reducers.js').default
store.replaceReducer(nextRootReducer)
})
}
return store
}
export default configureStore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment