This is how I define the apollo client with an upload link in my react native application.
I would like to add some header with a token value, which gets send with every request. But unfortunately I did not find an example for react native.
import { AsyncStorage } from 'react-native'
import { ApolloClient } from 'apollo-client'
import { createUploadLink } from 'apollo-upload-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
const client = new ApolloClient({
link: createUploadLink({
uri: 'http://localhost:3000/graphql'
}),
cache: new InMemoryCache()
})
I would like to send this value in the header:
const token = await AsyncStorage.getItem('auth.token')
Update
I don't know how to insert the token from a AsyncStorage to the header. Await
can't work here as it is not used in an async function:
const token = await AsyncStorage.getItem('auth.token') // await can't work here
// Initiate apollo client
const client = new ApolloClient({
link: createUploadLink({
uri: 'http://localhost:3000/graphql',
headers: {
authorization: token
}
}),
cache: new InMemoryCache()
})
// Wrap apollo provider
const withProvider = (Component, client) => {
return class extends React.Component {
render () {
return (
<ApolloProvider client={client}>
<Component {...this.props} client={client} />
</ApolloProvider>
)
}
}
}
export default async () => {
Navigation.registerComponent('MainScreen', () => withProvider(MainScreen, client))
Navigation.startSingleScreenApp({
screen: {
screen: 'MainScreen'
}
})
}
createUploadLink
has a headers property which matches to createHttpLink
headers property.
headers: an object representing values to be sent as headers on the request
Sample
const token = await AsyncStorage.getItem('auth.token')
const client = new ApolloClient({
link: createUploadLink({
uri: 'http://localhost:3000/graphql',
headers: {
"Some-Custom-Header": token
}
}),
cache: new InMemoryCache()
})
UPDATE
const getToken = async () => {
const token = await AsyncStorage.getItem('auth.token')
return token
}
const token = getToken()
// Initiate apollo client
const client = new ApolloClient({
link: createUploadLink({
uri: 'http://localhost:3000/graphql',
headers: {
authorization: token
}
}),
cache: new InMemoryCache()
})
// Wrap apollo provider
const withProvider = (Component, client) => {
return class extends React.Component {
render () {
return (
<ApolloProvider client={client}>
<Component {...this.props} client={client} />
</ApolloProvider>
)
}
}
}
export default async () => {
Navigation.registerComponent('MainScreen', () => withProvider(MainScreen, client))
Navigation.startSingleScreenApp({
screen: {
screen: 'MainScreen'
}
})
}
I am using apollo boost, What I did was,
import ApolloClient from 'apollo-boost';
const client = new ApolloClient({
uri: 'http://localhost:3000/graphql',
request: async (operation) => {
const token = await AsyncStorage.getItem('token');
operation.setContext({
headers: {
authorization: token
}
});
}
}
Using callback on getItem
also works
import { ApolloClient, HttpLink, ApolloLink, InMemoryCache } from 'apollo-boost'
const httpLink = new HttpLink({
uri: 'http://localhost:4000/graphql',
})
const authLink = new ApolloLink((operation, forward) => {
AsyncStorage.getItem('AuthToken').then(token => {
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : '',
},
})
})
return forward(operation)
})
const apolloClient = new ApolloClient({
cache: new InMemoryCache(),
link: authLink.concat(httpLink),
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With