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'
}
})
}