question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting Apollo client header dynamically is not working

See original GitHub issue

I am trying to set the header of Apollo client dynamically in React Native 0.62 according official doc, but I am getting error:

TypeError: (0 , _apollo.default) is not a function

This is my apollo.js

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

const httpLink = createHttpLink({
  uri: 'http://192.168.2.4:8000/api/',
});

const authLink = setContext((_, { headers }) => {
  const token = AsyncStorage.getItem('token');

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

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

export default client; 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
JClackettcommented, Apr 30, 2020

yeah makeApolloClient isnt a function, the file just exports an instance of the apollo client. Just import it as if its a variable.

import client from './app/config/apollo'

export default function App() {
  return (
    <ApolloProvider client={client}>
      <Routes />
    </ApolloProvider>);
}
1reaction
JClackettcommented, Jan 22, 2021

Hi @alexakasanjeev yeah exactly, I don’t think the time for getting the token from storage should make a significant difference, I’ve been doing it like this for years 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting Apollo client header dynamically is not working
Apollo usequery has a context option that allows you to dynamically change or update the values of the header object. import { ApolloClient ......
Read more >
Advanced HTTP networking - Apollo GraphQL Docs
The following example demonstrates adding a custom link to Apollo Client. This link adds an Authorization header to every HTTP request before the...
Read more >
Setting Apollo client header dynamically is not working-React ...
Coding example for the question Setting Apollo client header dynamically is not working-React Native.
Read more >
Apollo Client with dynamic headers ( Java, Spring Boot)
This is a story about how I integrated Apollo client into an existing Spring Boot application. Reason to write this article is to...
Read more >
Apollo - Dynamically change authorization token (link) - Reddit
const client = new ApolloClient({ cache: new InMemoryCache(), ... But this is not working as the authorization header is undefined in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found