Created
February 20, 2019 19:44
-
-
Save alobato/d87c64fc831a147a677c2dbfa77f5a95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 React from 'react' | |
import ReactDOM from 'react-dom' | |
import App from './containers/App' | |
import * as serviceWorker from './serviceWorker' | |
import { ApolloClient } from 'apollo-client' | |
import { ApolloProvider } from 'react-apollo' | |
import { InMemoryCache } from 'apollo-cache-inmemory' | |
import { HttpLink } from 'apollo-link-http' | |
import { ApolloLink } from 'apollo-link' | |
import { setContext } from 'apollo-link-context' | |
import { onError } from 'apollo-link-error' | |
import { ApolloProvider as ApolloHooksProvider } from './hooks/useApollo' | |
import ApolloLinkTimeout from './utils/apolloLinkTimeout' | |
import BASE_API from './constants/baseApi' | |
const timeoutLink = new ApolloLinkTimeout(10000) | |
const httpLink = new HttpLink({uri: `${BASE_API}/graphql`}) | |
const logout = client => { | |
localStorage.removeItem('checkinExpressPatientAuthToken') | |
client.resetStore() | |
window.location.href = '/login' | |
} | |
const errorLink = onError(({ graphQLErrors, networkError }) => { | |
if (graphQLErrors) { | |
graphQLErrors.map(({ message, locations, path }) => console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)) | |
graphQLErrors.forEach(({ message, locations, path }) => { | |
if (message === 'Not authenticated as user.') logout(client) | |
}) | |
} | |
if (networkError && networkError.statusCode === 401) { | |
console.log(`[Network error]: ${networkError}`) | |
logout(client) | |
} | |
}) | |
const authLink = setContext((_, { headers }) => { | |
const token = localStorage.getItem('checkinExpressPatientAuthToken') | |
return { headers: {...headers, authorization: token ? `Bearer ${token}` : ''} } | |
}) | |
const cache = new InMemoryCache() | |
const defaultOptions = {} | |
const link = ApolloLink.from([timeoutLink, authLink, errorLink, httpLink]) | |
const client = new ApolloClient({link, cache, defaultOptions}) | |
ReactDOM.render( | |
<ApolloProvider client={client}> | |
<ApolloHooksProvider client={client}> | |
<App logout={logout} client={client} /> | |
</ApolloHooksProvider> | |
</ApolloProvider>, | |
document.getElementById('root') | |
) | |
// If you want your app to work offline and load faster, you can change | |
// unregister() to register() below. Note this comes with some pitfalls. | |
// Learn more about service workers: http://bit.ly/CRA-PWA | |
serviceWorker.unregister() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment