Skip to content

Instantly share code, notes, and snippets.

@felisio
Created July 11, 2018 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felisio/875b7cc92d1baca618954d4acbe8f5c4 to your computer and use it in GitHub Desktop.
Save felisio/875b7cc92d1baca618954d4acbe8f5c4 to your computer and use it in GitHub Desktop.
Default connect for Apollo GraphQl
import ApolloClient, {InMemoryCache, ApolloLink} from "apollo-boost";
const PATH_URL = 'http://URL'
const TOKEN = 'TOKEN'
const getApolloClient = () => {
const cache = new InMemoryCache()
return new ApolloClient({
uri: `${PATH_URL}graphql`,
cache,
request: async operation => {
const token = await getToken()
operation.setContext({
headers: {
'X-Auth-Token2': TOKEN || null,
}
});
},
onError: ({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
console.log('ERRO GRAPHQL', graphQLErrors);
// insert generic error for graphQl Error
// redirect for router
}
if (networkError) {
// insert generic error for http Error
// redirect for router
}
},
});
}
const apolloClient = getApolloClient()
export default apolloClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment