React native asyncstorage and apollo-link console log not being called in promise

272 Views Asked by At

I'm trying to console.log in the authLink function, but don't see any console log output. It's as if the function itself is never being called.

Anyone have any ideas why I can't see any console.log output when calling it inside the authLink?

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;
0

There are 0 best solutions below