【发布时间】:2020-10-03 22:10:21
【问题描述】:
我只是对客户端使用 React-Native 并在我的服务器中检查验证令牌。
当我的 jwt 令牌过期时,服务器会通知客户端。
然后,apollo-client 捕获 graphQL 错误。
我想导航到注销用户,然后让它进入登录表单。
但是,我不知道如何在我的 graphQL forEach 块中做到这一点,
因为我不能在 NavigationContainer 之外使用反应导航。
谁知道解决这个问题?
...
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 />
)
【问题讨论】:
标签: react-native react-navigation apollo react-apollo