Last active
May 8, 2020 11:01
-
-
Save ahkohd/7b7b49130113713597689e4866e71e08 to your computer and use it in GitHub Desktop.
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 from 'apollo-client'; | |
import { HttpLink } from 'apollo-link-http'; | |
import { concat } from 'apollo-link'; | |
import { GRAPHQL_API_URL } from 'config/consts'; | |
import { InMemoryCache } from 'apollo-boost'; | |
import authMiddleware from './apollo/authMiddleware'; | |
import errorMiddleware from './apollo/errorMiddleware'; | |
const defaultOptions = { | |
watchQuery: { | |
fetchPolicy: 'no-cache', | |
errorPolicy: 'ignore', | |
}, | |
query: { | |
fetchPolicy: 'no-cache', | |
errorPolicy: 'all', | |
}, | |
}; | |
const httpLink = new HttpLink({ uri: GRAPHQL_API_URL }); | |
const setUpApolloClient = (fetch = false) => { | |
return new ApolloClient({ | |
link: errorMiddleware.concat(concat(authMiddleware, httpLink)), | |
cache: new InMemoryCache(), | |
defaultOptions: defaultOptions, | |
...(fetch ? { fetch } : {}), | |
}); | |
}; | |
export default setUpApolloClient; |
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 { ApolloLink } from 'apollo-link'; | |
import { getToken } from 'utils/localStorage'; | |
const authMiddleware = new ApolloLink((operation, forward) => { | |
if (getToken()) { | |
operation.setContext({ | |
headers: { | |
Authorization: `Bearer ${getToken()}`, | |
}, | |
}); | |
} | |
return forward(operation); | |
}); | |
export default authMiddleware; |
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 { onError } from 'apollo-link-error'; | |
import { clearUserDetails } from 'utils/localStorage'; | |
import { ROUTES } from 'config/consts'; | |
import { showErrorToast } from 'utils/toast'; | |
const errorMiddleware = onError(({ networkError, graphQLErrors }) => { | |
if ( | |
networkError && | |
networkError.statusCode === 401 && | |
networkError.result.returnStatus === 'TOKEN_EXPIRED' | |
) { | |
clearUserDetails(); | |
window.location.href = `${ROUTES.LOGIN}?expired_session=true`; | |
return; | |
} | |
if (graphQLErrors) { | |
showErrorToast({ | |
title: 'Ops! An Error occured', | |
body: graphQLErrors.reduce( | |
(allMsg, error) => (allMsg += error.message), | |
'' | |
), | |
emoji: '😢', | |
}); | |
return; | |
} | |
showErrorToast({ | |
title: 'No Internet Connection', | |
body: 'Please check your internet connectivity and try again!', | |
emoji: '🔌', | |
}); | |
}); | |
export default errorMiddleware; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment