Created
July 11, 2018 17:28
-
-
Save felisio/875b7cc92d1baca618954d4acbe8f5c4 to your computer and use it in GitHub Desktop.
Default connect for Apollo GraphQl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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