Skip to content

Instantly share code, notes, and snippets.

@HERRKIN
Created February 3, 2017 16:03
Show Gist options
  • Save HERRKIN/78f0b65afd05d93a0a228d2d9467c3b3 to your computer and use it in GitHub Desktop.
Save HERRKIN/78f0b65afd05d93a0a228d2d9467c3b3 to your computer and use it in GitHub Desktop.
apollo configuration file
import ApolloClient, {createNetworkInterface} from 'apollo-client'
import {addTypenameToSelectionSet} from 'apollo-client/queries/queryTransform'
import baseURL from './url' // we could use a string url here for example localhost:port/
const getLoginToken = function () {
return window.localStorage['Meteor.loginToken']
}
export const createMeteorNetworkInterface = () => {
let uri = baseURL + '/graphql'
const networkInterface = createNetworkInterface({uri})
networkInterface.use([{
applyMiddleware (request, next) {
if (!request.options.headers) {
request.options.headers = new window.Headers()
}
const currentUserToken = getLoginToken()
request.options.headers.Authorization = currentUserToken
next()
}
}])
return networkInterface
}
// set the configuration for the apollo client
const config = {
networkInterface: createMeteorNetworkInterface(),
queryTransformer: addTypenameToSelectionSet,
dataIdFromObject: (result) => {
if (result._id && result.__typename) {
return result.__typename + ':' + result._id
} else if (result.key && result.__typename) {
return result.__typename + ':' + result.key
}
},
shouldBatch: false
}
// create the instance
const apollo = new ApolloClient(config)
export default apollo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment