Skip to content

Instantly share code, notes, and snippets.

@kevotovar
Created September 6, 2019 13:28
Show Gist options
  • Save kevotovar/a6f101edc63d15b6e15537d83c8faad1 to your computer and use it in GitHub Desktop.
Save kevotovar/a6f101edc63d15b6e15537d83c8faad1 to your computer and use it in GitHub Desktop.
import { HttpLink, ApolloClient, InMemoryCache } from "apollo-boost";
import { AsyncStorage } from "react-native";
import { setContext } from "apollo-link-context";
const httpLink = new HttpLink({ uri: "http://localhost:8000/graphql/" });
const authLink = setContext(async (req, { headers }) => {
const token = await AsyncStorage.getItem("authToken");
return {
...headers,
headers: {
authorization: token ? `JWT ${token}` : null
}
};
});
const link = authLink.concat(httpLink);
const client = new ApolloClient({
link,
cache: new InMemoryCache()
});
export default client;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment