我的应用程序似乎仅在通过身份验证后立即从服务器中获取数据一次,此后它不会向服务器发送任何新请求,即使我重新加载了该应用程序,也仅注销并再次登录一次。
在新屏幕上只有一次,这似乎是缓存问题?但是我尝试停止使用缓存,但仍然无法正常工作。
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import { InMemoryCache } from 'apollo-cache-inmemory'
import AsyncStorage from '@react-native-community/async-storage'
import { refresh } from 'react-native-app-auth'
import { KC_CONFIG } from '../../config/env'
const httpLink = createHttpLink({
uri: 'http://192.168.178.130:4000/',
})
const authLink = setContext(async (_, { headers }) => {
const refreshToken = await AsyncStorage.getItem('refreshToken')
const unwrappedRefresh = JSON.parse(refreshToken)
const result = await refresh(KC_CONFIG, {
refreshToken: unwrappedRefresh,
})
await AsyncStorage.setItem('refreshToken', result.refreshToken)
return {
headers: {
...headers,
authorization: result ? `Bearer ${result.accessToken}` : "",
}
}
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
defaultOptions: { watchQuery: { fetchPolicy: 'no-cache' }, query: { fetchPolicy: 'no-cache' } }
})
export default client