Created
September 2, 2022 14:06
-
-
Save Yasir5247/58866e4fc269b124bcf11c8aa3cd4921 to your computer and use it in GitHub Desktop.
apolloClient
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, HttpLink, from, fromPromise} from '@apollo/client'; | |
import {setContext} from '@apollo/client/link/context'; | |
import {RetryLink} from '@apollo/client/link/retry'; | |
import {onError} from '@apollo/client/link/error'; | |
import {refresh} from 'react-native-app-auth'; | |
import AsyncStorage from '@react-native-async-storage/async-storage'; | |
import {getTokenSync, getToken} from '../../utils/getToken'; | |
import {test_config, prod_config} from '../../utils/efaasConfig'; | |
import {cache} from './cache'; | |
getToken(); | |
//httpLink | |
const httpLink = new HttpLink({ | |
uri: 'https://efaas.mv/graphql', | |
credentials: 'include', | |
}); | |
//authLink | |
const authLink = setContext(async (_, {headers}) => { | |
const token = await AsyncStorage.getItem('@token'); | |
return { | |
headers: { | |
...headers, | |
authorization: token ? `Bearer ${token}` : null, | |
}, | |
}; | |
}); | |
//retryLink | |
const retryLink = new RetryLink({ | |
attempts: (count, operation, error) => { | |
const isMutation = | |
operation && | |
operation.query && | |
operation.query.definitions && | |
Array.isArray(operation.query.definitions) && | |
operation.query.definitions.some(def => def.kind === 'OperationDefinition' && def.operation === 'mutation'); | |
if (isMutation) { | |
return !!error && count < 25; | |
} | |
return !!error && count < 6; | |
}, | |
}); | |
//refresh token | |
const getRefreshToken = async () => { | |
try { | |
const refreshToken = await AsyncStorage.getItem('@refreshToken'); | |
console.log('refreshToken', refreshToken); | |
if (refreshToken) { | |
console.log('im here'); | |
const result = await refresh(test_config, { | |
refreshToken: refreshToken, | |
}); | |
console.log('result', result); | |
console.log('new---AccessToken', result.accessToken); | |
return result.accessToken; | |
} | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
//errorLink | |
const errorLink = onError(({graphQLErrors, networkError, operation, forward}) => { | |
// console.log('hellow'); | |
if (graphQLErrors) { | |
for (let err of graphQLErrors) { | |
console.log('errorExtentionCode', err.extensions.code); | |
switch (err.extensions.code) { | |
case 'INTERNAL_SERVER_ERROR': | |
return fromPromise( | |
getRefreshToken().catch(error => { | |
// console.log('error', error); | |
// Handle token refresh errors e.g clear stored tokens, redirect to login | |
return; | |
}), | |
) | |
.filter(value => Boolean(value)) | |
.flatMap(accessToken => { | |
const oldHeaders = operation.getContext().headers; | |
// modify the operation context with a new token | |
operation.setContext({ | |
headers: { | |
...oldHeaders, | |
authorization: `Bearer ${accessToken}`, | |
}, | |
}); | |
//retry the request, returning the new observable | |
return forward(operation); | |
}); | |
} | |
} | |
} | |
}); | |
export const client = new ApolloClient({ | |
cache, | |
link: from([errorLink, authLink, retryLink, httpLink]), | |
assumeImmutableResults: true, | |
connectToDevTools: true, | |
defaultOptions: { | |
watchQuery: { | |
fetchPolicy: 'cache-and-network', | |
errorPolicy: 'none', | |
}, | |
query: { | |
fetchPolicy: 'cache-first', | |
errorPolicy: 'all', | |
}, | |
}, | |
}); |
EhsanHerai
commented
Sep 3, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment