0

我正在尝试console.log使用 authLink 函数,但没有看到任何控制台日志输出。就好像函数本身从未被调用过。

任何人都知道为什么我在 authLink 中调用它时看不到任何 console.log 输出?

import { ApolloClient } from "apollo-client";
import { useQuery } from "@apollo/react-hooks";
import { InMemoryCache } from "apollo-cache-inmemory";
import { createHttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";

import AsyncStorage from "@react-native-community/async-storage";

import { resolvers, typeDefs } from "./resolvers";

const cache = new InMemoryCache();

const httpLink = createHttpLink({
  uri: "https://foobar"
});

const authLink = setContext(async (_, { headers }) => {
  console.log("In here!");
  let authToken = await AsyncStorage.getItem("authToken");
  if (authToken !== null) {
    cache.writeData({
      data: {
        isLoggedIn: true,
        authToken: authToken,
      },
    });
  } else {
    cache.writeData({
      data: {
        isLoggedIn: false,
        authToken: "",
      },
    });
  }

  return {
    headers: {
      ...headers,
      authorization: authToken ? `Bearer ${authToken}` : "",
    },
  };
});

const client = new ApolloClient({
  cache: cache,
  link: authLink.concat(httpLink),
  typeDefs: typeDefs,
  resolvers: resolvers,
});

export default client;
4

0 回答 0