Setting Apollo client header dynamically is not working
See original GitHub issueI 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:
- Created 3 years ago
- Comments:8
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yeah makeApolloClient isnt a function, the file just exports an instance of the apollo client. Just import it as if its a variable.
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 😃