1
votes

I just use React-Native for Client and check validate token in my server.

When my jwt token is expired, the server notices it to the client.

Then, the apollo-client catchs graphQL Error.

I want to navigate to logout user and to get it to login form at that time.

However, I don't know how to do that in my graphQL forEach block,

because I can't use react navigation out side of the NavigationContainer.

anyone who know solve this problem?

...
const cache = new InMemoryCache()
        await persistCache({
            cache,
            storage: AsyncStorage,
        })  
const clientState = (cache) =>
new ApolloClient({
    link: ApolloLink.from([
        ...
        onError(({ graphQLErrors, networkError }) => {
            if (graphQLErrors)
                graphQLErrors.forEach(
                    async ({ message, locations, path }) => {
                        console.log(
                            `[GraphQL error]: Message: ${message}, Location: ${JSON.stringify(
                                locations
                            )}, Path: ${path}`
                        )
                        if (
                            message ===
                            "You need to log in to perform this action"
                        ) {
                            logout() >>>>>>>>>>>> I want to redirect!
                        }
                    }
                )
            if (networkError)
                console.log(`[Network error]: ${networkError}`)
        }), 
     ...
]})
const client = clientState(cache)
return loaded && client ? (
    <ApolloHooksProvider client={client}>
        <SafeAreaProvider>
            <AuthProvider isLoggedIn={isLoggedIn} userEmail={userEmail}>
                <NavigationContainer style={{ backgroundColor: "white" }}>
                    <TopNav />
                </NavigationContainer>
            </AuthProvider>
        </SafeAreaProvider>
    </ApolloHooksProvider>
) : (
    <AppLoading />
)
1

1 Answers

0
votes

You can just use state update where you are sending request. And remove the invalid token here on onError (). That's one way of doing it.