React native 阿波罗客户端React Native的登录状态管理和缓存

React native 阿波罗客户端React Native的登录状态管理和缓存,react-native,graphql,apollo,React Native,Graphql,Apollo,我正在使用React native和apollo客户端学习graphQL。我正在试验一些具有简单登录屏幕的代码,并试图检查我对缓存的理解。下面是我的graphql客户端代码。通过打开persistCache的debug,我看到了使用CMD+R重新加载带有expo的iOS模拟器时的一行代码。这表明缓存正在工作 [apollo-cache-persist] Restored cache of size 29 我的问题是,完成不需要再次登录的整个过程还需要什么?我假设我需要维护是否登录的状态,而不显

我正在使用React native和apollo客户端学习graphQL。我正在试验一些具有简单登录屏幕的代码,并试图检查我对缓存的理解。下面是我的graphql客户端代码。通过打开persistCache的debug,我看到了使用CMD+R重新加载带有expo的iOS模拟器时的一行代码。这表明缓存正在工作

[apollo-cache-persist] Restored cache of size 29
我的问题是,完成不需要再次登录的整个过程还需要什么?我假设我需要维护是否登录的状态,而不显示登录屏幕。我想找一些例子来说明这一点

const retryLink = new RetryLink({
  delay: {
    initial: 300,
    max: 5000,
    jitter: true
  },
  attempts: {
    max: Infinity,
    retryIf: (error = {}) => {
      return error.statusCode > 299 || !error.statusCode
    }
  }
});

const formatObject = data => _.isObject(data) ? JSON.stringify(data) : data;

const formatGraphQLError = err =>
  `Message: ${err.message}, Location: ${formatObject(
    err.locations
  )}`;

const errorLink = onError(({ networkError = "", graphQLErrors = [] } = {}) => {
  if (networkError)
    console.log(`[Network Error]: ${networkError}`);

  if (graphQLErrors.length)
    graphQLErrors.map(formatGraphQLError).forEach(err => console.log(`[GraphQL Error]: ${err}`))
});

const authLink = setContext(async (_, { headers }) => {
  const token = await Auth.token();
  if (token)
    return {
      headers: {
        ...headers,
        authorization: `Bearer ${token}`
      }
    };
  else return { headers };
});

const httpLink = new HttpLink({
  uri: Config.apiUrl
});

const cache = new InMemoryCache();

// Set up cache persistence.
persistCache({
  cache,
  storage: AsyncStorage,
  trigger: 'background',
  debug: true
});

const logLink = new ApolloLink((operation, forward) => {
  console.log("Running GraphQL query or mutation");
  return forward(operation);
});

//--
//-- Combine the links in your required order.
//--

let _notifications = 42;

const client = new ApolloClient({
  resolvers: {
    Query: {
      permission: async (_, { type }) => await Permissions.askAsync(type),
      token: async () => await Auth.token(),
      notifications: () => _notifications
    },
    Mutation: {
      login: async (_, { email, password }) => {
        return await Auth.login(email, password)
      },
      updateNotifications: async (_, { notifications }) => _notifications = notifications
    }
  },
  link: ApolloLink.from([
    logLink,
    retryLink,
    errorLink,
    authLink,
    httpLink
  ]),
  cache
});

export default client;